/* Modified condition_wait that checks for cancellation. Copyright (C) 1995, 1996, 2002 Free Software Foundation, Inc. This file is part of the GNU C Library. The GNU C Library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. The GNU C Library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with the GNU C Library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #include #include #include #include /* Just like condition_wait, but cancellable. Returns true if cancelled. */ int hurd_condition_wait (pthread_cond_t *c, pthread_mutex_t *m) { /* This function will be called by hurd_thread_cancel while we are blocked in the condition_wait. We wake up all threads blocked on C, so our thread will wake up and notice the cancellation flag. */ struct __pthread *self = _pthread_self (); void cancel_me (void) { __pthread_spin_lock (&c->__lock); /* If self has been dequeued, self->prevp == 0 */ if (self->prevp) { __pthread_dequeue (self); __pthread_spin_unlock (&c->__lock); __pthread_wakeup (self); } else { /* There is no thread to wakeup */ __pthread_spin_unlock (&c->__lock); } } struct hurd_sigstate *ss = _hurd_self_sigstate (); int cancel; assert (ss->intr_port == MACH_PORT_NULL); /* Sanity check for signal bugs. */ /* Atomically enqueue our cproc on the condition variable's queue of waiters, and mark our sigstate to indicate that `cancel_me' must be called to wake us up. We must hold the sigstate lock while acquiring the condition variable's lock and tweaking it, so that hurd_thread_cancel can never suspend us and then deadlock in condition_broadcast waiting for the condition variable's lock. */ __pthread_spin_lock (&ss->lock); __pthread_spin_lock (&c->__lock); cancel = ss->cancel; if (cancel) /* We were cancelled before doing anything. Don't block at all. */ ss->cancel = 0; else { /* Put us on the queue so that condition_broadcast will know to wake us up. */ __pthread_enqueue (&c->__queue, self); /* Tell hurd_thread_cancel how to unblock us. */ ss->cancel_hook = &cancel_me; } __pthread_spin_unlock (&c->__lock); __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_block (self); } __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; }