2.5-18.1
[jlayton/glibc.git] / nptl / sysdeps / pthread / pthread_cond_wait.c
1 /* Copyright (C) 2003, 2004, 2006 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3    Contributed by Martin Schwidefsky <schwidefsky@de.ibm.com>, 2003.
4
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, write to the Free
17    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18    02111-1307 USA.  */
19
20 #include <endian.h>
21 #include <errno.h>
22 #include <sysdep.h>
23 #include <lowlevellock.h>
24 #include <pthread.h>
25 #include <pthreadP.h>
26
27 #include <shlib-compat.h>
28
29
30 struct _condvar_cleanup_buffer
31 {
32   int oldtype;
33   pthread_cond_t *cond;
34   pthread_mutex_t *mutex;
35   unsigned int bc_seq;
36 };
37
38
39 void
40 __attribute__ ((visibility ("hidden")))
41 __condvar_cleanup (void *arg)
42 {
43   struct _condvar_cleanup_buffer *cbuffer =
44     (struct _condvar_cleanup_buffer *) arg;
45   unsigned int destroying;
46
47   /* We are going to modify shared data.  */
48   lll_mutex_lock (cbuffer->cond->__data.__lock);
49
50   if (cbuffer->bc_seq == cbuffer->cond->__data.__broadcast_seq)
51     {
52       /* This thread is not waiting anymore.  Adjust the sequence counters
53          appropriately.  We do not increment WAKEUP_SEQ if this would
54          bump it over the value of TOTAL_SEQ.  This can happen if a thread
55          was woken and then canceled.  */
56       if (cbuffer->cond->__data.__wakeup_seq
57           < cbuffer->cond->__data.__total_seq)
58         {
59           ++cbuffer->cond->__data.__wakeup_seq;
60           ++cbuffer->cond->__data.__futex;
61         }
62       ++cbuffer->cond->__data.__woken_seq;
63     }
64
65   cbuffer->cond->__data.__nwaiters -= 1 << COND_CLOCK_BITS;
66
67   /* If pthread_cond_destroy was called on this variable already,
68      notify the pthread_cond_destroy caller all waiters have left
69      and it can be successfully destroyed.  */
70   destroying = 0;
71   if (cbuffer->cond->__data.__total_seq == -1ULL
72       && cbuffer->cond->__data.__nwaiters < (1 << COND_CLOCK_BITS))
73     {
74       lll_futex_wake (&cbuffer->cond->__data.__nwaiters, 1);
75       destroying = 1;
76     }
77
78   /* We are done.  */
79   lll_mutex_unlock (cbuffer->cond->__data.__lock);
80
81   /* Wake everybody to make sure no condvar signal gets lost.  */
82   if (! destroying)
83     lll_futex_wake (&cbuffer->cond->__data.__futex, INT_MAX);
84
85   /* Get the mutex before returning unless asynchronous cancellation
86      is in effect.  */
87   __pthread_mutex_cond_lock (cbuffer->mutex);
88 }
89
90
91 int
92 __pthread_cond_wait (cond, mutex)
93      pthread_cond_t *cond;
94      pthread_mutex_t *mutex;
95 {
96   struct _pthread_cleanup_buffer buffer;
97   struct _condvar_cleanup_buffer cbuffer;
98   int err;
99
100   /* Make sure we are along.  */
101   lll_mutex_lock (cond->__data.__lock);
102
103   /* Now we can release the mutex.  */
104   err = __pthread_mutex_unlock_usercnt (mutex, 0);
105   if (__builtin_expect (err, 0))
106     {
107       lll_mutex_unlock (cond->__data.__lock);
108       return err;
109     }
110
111   /* We have one new user of the condvar.  */
112   ++cond->__data.__total_seq;
113   ++cond->__data.__futex;
114   cond->__data.__nwaiters += 1 << COND_CLOCK_BITS;
115
116   /* Remember the mutex we are using here.  If there is already a
117      different address store this is a bad user bug.  Do not store
118      anything for pshared condvars.  */
119   if (cond->__data.__mutex != (void *) ~0l)
120     cond->__data.__mutex = mutex;
121
122   /* Prepare structure passed to cancellation handler.  */
123   cbuffer.cond = cond;
124   cbuffer.mutex = mutex;
125
126   /* Before we block we enable cancellation.  Therefore we have to
127      install a cancellation handler.  */
128   __pthread_cleanup_push (&buffer, __condvar_cleanup, &cbuffer);
129
130   /* The current values of the wakeup counter.  The "woken" counter
131      must exceed this value.  */
132   unsigned long long int val;
133   unsigned long long int seq;
134   val = seq = cond->__data.__wakeup_seq;
135   /* Remember the broadcast counter.  */
136   cbuffer.bc_seq = cond->__data.__broadcast_seq;
137
138   do
139     {
140       unsigned int futex_val = cond->__data.__futex;
141
142       /* Prepare to wait.  Release the condvar futex.  */
143       lll_mutex_unlock (cond->__data.__lock);
144
145       /* Enable asynchronous cancellation.  Required by the standard.  */
146       cbuffer.oldtype = __pthread_enable_asynccancel ();
147
148       /* Wait until woken by signal or broadcast.  */
149       lll_futex_wait (&cond->__data.__futex, futex_val);
150
151       /* Disable asynchronous cancellation.  */
152       __pthread_disable_asynccancel (cbuffer.oldtype);
153
154       /* We are going to look at shared data again, so get the lock.  */
155       lll_mutex_lock (cond->__data.__lock);
156
157       /* If a broadcast happened, we are done.  */
158       if (cbuffer.bc_seq != cond->__data.__broadcast_seq)
159         goto bc_out;
160
161       /* Check whether we are eligible for wakeup.  */
162       val = cond->__data.__wakeup_seq;
163     }
164   while (val == seq || cond->__data.__woken_seq == val);
165
166   /* Another thread woken up.  */
167   ++cond->__data.__woken_seq;
168
169  bc_out:
170
171   cond->__data.__nwaiters -= 1 << COND_CLOCK_BITS;
172
173   /* If pthread_cond_destroy was called on this varaible already,
174      notify the pthread_cond_destroy caller all waiters have left
175      and it can be successfully destroyed.  */
176   if (cond->__data.__total_seq == -1ULL
177       && cond->__data.__nwaiters < (1 << COND_CLOCK_BITS))
178     lll_futex_wake (&cond->__data.__nwaiters, 1);
179
180   /* We are done with the condvar.  */
181   lll_mutex_unlock (cond->__data.__lock);
182
183   /* The cancellation handling is back to normal, remove the handler.  */
184   __pthread_cleanup_pop (&buffer, 0);
185
186   /* Get the mutex before returning.  */
187   return __pthread_mutex_cond_lock (mutex);
188 }
189
190 versioned_symbol (libpthread, __pthread_cond_wait, pthread_cond_wait,
191                   GLIBC_2_3_2);