Back to index

glibc  2.9
pthread_cond_wait.c
Go to the documentation of this file.
00001 /* Copyright (C) 2003, 2004, 2006, 2007 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Martin Schwidefsky <schwidefsky@de.ibm.com>, 2003.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <endian.h>
00021 #include <errno.h>
00022 #include <sysdep.h>
00023 #include <lowlevellock.h>
00024 #include <pthread.h>
00025 #include <pthreadP.h>
00026 
00027 #include <shlib-compat.h>
00028 
00029 
00030 struct _condvar_cleanup_buffer
00031 {
00032   int oldtype;
00033   pthread_cond_t *cond;
00034   pthread_mutex_t *mutex;
00035   unsigned int bc_seq;
00036 };
00037 
00038 
00039 void
00040 __attribute__ ((visibility ("hidden")))
00041 __condvar_cleanup (void *arg)
00042 {
00043   struct _condvar_cleanup_buffer *cbuffer =
00044     (struct _condvar_cleanup_buffer *) arg;
00045   unsigned int destroying;
00046   int pshared = (cbuffer->cond->__data.__mutex == (void *) ~0l)
00047               ? LLL_SHARED : LLL_PRIVATE;
00048 
00049   /* We are going to modify shared data.  */
00050   lll_lock (cbuffer->cond->__data.__lock, pshared);
00051 
00052   if (cbuffer->bc_seq == cbuffer->cond->__data.__broadcast_seq)
00053     {
00054       /* This thread is not waiting anymore.  Adjust the sequence counters
00055         appropriately.  We do not increment WAKEUP_SEQ if this would
00056         bump it over the value of TOTAL_SEQ.  This can happen if a thread
00057         was woken and then canceled.  */
00058       if (cbuffer->cond->__data.__wakeup_seq
00059          < cbuffer->cond->__data.__total_seq)
00060        {
00061          ++cbuffer->cond->__data.__wakeup_seq;
00062          ++cbuffer->cond->__data.__futex;
00063        }
00064       ++cbuffer->cond->__data.__woken_seq;
00065     }
00066 
00067   cbuffer->cond->__data.__nwaiters -= 1 << COND_NWAITERS_SHIFT;
00068 
00069   /* If pthread_cond_destroy was called on this variable already,
00070      notify the pthread_cond_destroy caller all waiters have left
00071      and it can be successfully destroyed.  */
00072   destroying = 0;
00073   if (cbuffer->cond->__data.__total_seq == -1ULL
00074       && cbuffer->cond->__data.__nwaiters < (1 << COND_NWAITERS_SHIFT))
00075     {
00076       lll_futex_wake (&cbuffer->cond->__data.__nwaiters, 1, pshared);
00077       destroying = 1;
00078     }
00079 
00080   /* We are done.  */
00081   lll_unlock (cbuffer->cond->__data.__lock, pshared);
00082 
00083   /* Wake everybody to make sure no condvar signal gets lost.  */
00084   if (! destroying)
00085     lll_futex_wake (&cbuffer->cond->__data.__futex, INT_MAX, pshared);
00086 
00087   /* Get the mutex before returning unless asynchronous cancellation
00088      is in effect.  */
00089   __pthread_mutex_cond_lock (cbuffer->mutex);
00090 }
00091 
00092 
00093 int
00094 __pthread_cond_wait (cond, mutex)
00095      pthread_cond_t *cond;
00096      pthread_mutex_t *mutex;
00097 {
00098   struct _pthread_cleanup_buffer buffer;
00099   struct _condvar_cleanup_buffer cbuffer;
00100   int err;
00101   int pshared = (cond->__data.__mutex == (void *) ~0l)
00102               ? LLL_SHARED : LLL_PRIVATE;
00103 
00104   /* Make sure we are along.  */
00105   lll_lock (cond->__data.__lock, pshared);
00106 
00107   /* Now we can release the mutex.  */
00108   err = __pthread_mutex_unlock_usercnt (mutex, 0);
00109   if (__builtin_expect (err, 0))
00110     {
00111       lll_unlock (cond->__data.__lock, pshared);
00112       return err;
00113     }
00114 
00115   /* We have one new user of the condvar.  */
00116   ++cond->__data.__total_seq;
00117   ++cond->__data.__futex;
00118   cond->__data.__nwaiters += 1 << COND_NWAITERS_SHIFT;
00119 
00120   /* Remember the mutex we are using here.  If there is already a
00121      different address store this is a bad user bug.  Do not store
00122      anything for pshared condvars.  */
00123   if (cond->__data.__mutex != (void *) ~0l)
00124     cond->__data.__mutex = mutex;
00125 
00126   /* Prepare structure passed to cancellation handler.  */
00127   cbuffer.cond = cond;
00128   cbuffer.mutex = mutex;
00129 
00130   /* Before we block we enable cancellation.  Therefore we have to
00131      install a cancellation handler.  */
00132   __pthread_cleanup_push (&buffer, __condvar_cleanup, &cbuffer);
00133 
00134   /* The current values of the wakeup counter.  The "woken" counter
00135      must exceed this value.  */
00136   unsigned long long int val;
00137   unsigned long long int seq;
00138   val = seq = cond->__data.__wakeup_seq;
00139   /* Remember the broadcast counter.  */
00140   cbuffer.bc_seq = cond->__data.__broadcast_seq;
00141 
00142   do
00143     {
00144       unsigned int futex_val = cond->__data.__futex;
00145 
00146       /* Prepare to wait.  Release the condvar futex.  */
00147       lll_unlock (cond->__data.__lock, pshared);
00148 
00149       /* Enable asynchronous cancellation.  Required by the standard.  */
00150       cbuffer.oldtype = __pthread_enable_asynccancel ();
00151 
00152       /* Wait until woken by signal or broadcast.  */
00153       lll_futex_wait (&cond->__data.__futex, futex_val, pshared);
00154 
00155       /* Disable asynchronous cancellation.  */
00156       __pthread_disable_asynccancel (cbuffer.oldtype);
00157 
00158       /* We are going to look at shared data again, so get the lock.  */
00159       lll_lock (cond->__data.__lock, pshared);
00160 
00161       /* If a broadcast happened, we are done.  */
00162       if (cbuffer.bc_seq != cond->__data.__broadcast_seq)
00163        goto bc_out;
00164 
00165       /* Check whether we are eligible for wakeup.  */
00166       val = cond->__data.__wakeup_seq;
00167     }
00168   while (val == seq || cond->__data.__woken_seq == val);
00169 
00170   /* Another thread woken up.  */
00171   ++cond->__data.__woken_seq;
00172 
00173  bc_out:
00174 
00175   cond->__data.__nwaiters -= 1 << COND_NWAITERS_SHIFT;
00176 
00177   /* If pthread_cond_destroy was called on this varaible already,
00178      notify the pthread_cond_destroy caller all waiters have left
00179      and it can be successfully destroyed.  */
00180   if (cond->__data.__total_seq == -1ULL
00181       && cond->__data.__nwaiters < (1 << COND_NWAITERS_SHIFT))
00182     lll_futex_wake (&cond->__data.__nwaiters, 1, pshared);
00183 
00184   /* We are done with the condvar.  */
00185   lll_unlock (cond->__data.__lock, pshared);
00186 
00187   /* The cancellation handling is back to normal, remove the handler.  */
00188   __pthread_cleanup_pop (&buffer, 0);
00189 
00190   /* Get the mutex before returning.  */
00191   return __pthread_mutex_cond_lock (mutex);
00192 }
00193 
00194 versioned_symbol (libpthread, __pthread_cond_wait, pthread_cond_wait,
00195                 GLIBC_2_3_2);