[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Hurd_condition_wait
From: |
Vicente Hernando Ara |
Subject: |
Hurd_condition_wait |
Date: |
16 Nov 2002 20:58:26 +0100 |
Hi all!
The following is a hurd_condition_wait replacement with pthread code.
This would work assuming I can freely replace __spin_lock and
__spin_unlock glibc functions by pthread_spin_lock and
pthread_spin_unlock, and they are compatible.
hurd_pthread_cond_wait function:
/* Just like pthread_condition_wait, but cancellable. Returns true if
cancelled. */
int
hurd_pthread_cond_wait (pthread_cond_t c, pthread_mutex_t m)
{
/* This function will be called by hurd_thread_cancel while we
are blocked in the pthread_cond_wait. We wake up all threads blocked
on C,so our thread will wake up and notice the cancellation flag. */
void cancel_me (void)
{
pthread_cond_broadcast (c);
}
struct hurd_sigstate *ss = _hurd_self_sigstate ();
int cancel;
assert (ss->intr_port == MACH_PORT_NULL); /* Sanity check for signal
bugs. */
pthread_spin_lock (&ss->lock); /* With this lock we avoid being
suspended. */
cancel = ss->cancel;
if (cancel)
/* We were cancelled before doing anything. Don't block at all. */
ss->cancel = 0;
else
{
/* Tell hurd_thread_cancel how to unblock us. */
ss->cancel_hook = &cancel_me;
}
pthread_spin_unlock (&ss->lock);
if (cancel)
{
/* Cancelled on entry. Just leave the mutex locked. */
m = NULL;
}
else
{
/* Now unlock the mutex and block until woken. */
pthread_mutex_unlock (m);
pthread_cond_wait(c, m);
}
pthread_spin_lock (&ss->lock);
/* Clear the hook, now that we are done blocking. */
ss->cancel_hook = NULL;
/* Check the cancellation flag; we might have unblocked due to
cancellation rather than a normal condition_signal or
condition_broadcast (or we might have just happened to get cancelled
right after waking up). */
cancel |= ss->cancel;
ss->cancel = 0;
pthread_spin_unlock (&ss->lock);
if (m)
/* Reacquire the mutex and return. */
pthread_mutex_lock (m);
return cancel;
}
I think there is a race condition in this implementation.
After setting the hook function and unlocking ss->lock, we can receive
a signal broadcast before we have done a pthread_cond_wait.
I see several solutions:
a) Including pthread_cond_wait before we unlock ss->lock spinlock.
Can this have side effects with the thread suspension?
b) Add a new mutex or spin_lock to hurd_thread_cancel before calling the
hook function.
c) A different implementation using __pthread_enqueue function.
Can you advice me? I think c is the best, but not sure.
At the end there is the original hurd_condition_wait function attached.
--
Thanks,
Vicente.
hurd_condition_wait.c
Description: Text Data
- Hurd_condition_wait,
Vicente Hernando Ara <=
- Re: Hurd_condition_wait, Neal H. Walfield, 2002/11/16
- Re: Hurd_condition_wait, Vicente Hernando Ara, 2002/11/17
- Re: Hurd_condition_wait, Neal H. Walfield, 2002/11/17
- Re: Hurd_condition_wait, Vicente Hernando Ara, 2002/11/17
- Re: Hurd_condition_wait, Neal H. Walfield, 2002/11/17
- Re: Hurd_condition_wait, Marcus Brinkmann, 2002/11/17
- Re: Hurd_condition_wait, Neal H. Walfield, 2002/11/17
- Re: Hurd_condition_wait, Neal H. Walfield, 2002/11/17
- Re: Hurd_condition_wait, Neal H. Walfield, 2002/11/17
- Re: Hurd_condition_wait, Thomas Bushnell, BSG, 2002/11/19