1 /* Copyright (C) 2003,2004,2006,2007,2011 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Martin Schwidefsky <schwidefsky@de.ibm.com>, 2003.
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.
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.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
22 #include <lowlevellock.h>
26 #include <shlib-compat.h>
29 struct _condvar_cleanup_buffer
33 pthread_mutex_t
*mutex
;
39 __attribute__ ((visibility ("hidden")))
40 __condvar_cleanup (void *arg
)
42 struct _condvar_cleanup_buffer
*cbuffer
=
43 (struct _condvar_cleanup_buffer
*) arg
;
44 unsigned int destroying
;
45 int pshared
= (cbuffer
->cond
->__data
.__mutex
== (void *) ~0l)
46 ? LLL_SHARED
: LLL_PRIVATE
;
48 /* We are going to modify shared data. */
49 lll_lock (cbuffer
->cond
->__data
.__lock
, pshared
);
51 if (cbuffer
->bc_seq
== cbuffer
->cond
->__data
.__broadcast_seq
)
53 /* This thread is not waiting anymore. Adjust the sequence counters
54 appropriately. We do not increment WAKEUP_SEQ if this would
55 bump it over the value of TOTAL_SEQ. This can happen if a thread
56 was woken and then canceled. */
57 if (cbuffer
->cond
->__data
.__wakeup_seq
58 < cbuffer
->cond
->__data
.__total_seq
)
60 ++cbuffer
->cond
->__data
.__wakeup_seq
;
61 ++cbuffer
->cond
->__data
.__futex
;
63 ++cbuffer
->cond
->__data
.__woken_seq
;
66 cbuffer
->cond
->__data
.__nwaiters
-= 1 << COND_NWAITERS_SHIFT
;
68 /* If pthread_cond_destroy was called on this variable already,
69 notify the pthread_cond_destroy caller all waiters have left
70 and it can be successfully destroyed. */
72 if (cbuffer
->cond
->__data
.__total_seq
== -1ULL
73 && cbuffer
->cond
->__data
.__nwaiters
< (1 << COND_NWAITERS_SHIFT
))
75 lll_futex_wake (&cbuffer
->cond
->__data
.__nwaiters
, 1, pshared
);
80 lll_unlock (cbuffer
->cond
->__data
.__lock
, pshared
);
82 /* Wake everybody to make sure no condvar signal gets lost. */
84 lll_futex_wake (&cbuffer
->cond
->__data
.__futex
, INT_MAX
, pshared
);
86 /* Get the mutex before returning unless asynchronous cancellation
88 __pthread_mutex_cond_lock (cbuffer
->mutex
);
93 __pthread_cond_wait (cond
, mutex
)
95 pthread_mutex_t
*mutex
;
97 struct _pthread_cleanup_buffer buffer
;
98 struct _condvar_cleanup_buffer cbuffer
;
100 int pshared
= (cond
->__data
.__mutex
== (void *) ~0l)
101 ? LLL_SHARED
: LLL_PRIVATE
;
103 /* Make sure we are alone. */
104 lll_lock (cond
->__data
.__lock
, pshared
);
106 /* Now we can release the mutex. */
107 err
= __pthread_mutex_unlock_usercnt (mutex
, 0);
108 if (__builtin_expect (err
, 0))
110 lll_unlock (cond
->__data
.__lock
, pshared
);
114 /* We have one new user of the condvar. */
115 ++cond
->__data
.__total_seq
;
116 ++cond
->__data
.__futex
;
117 cond
->__data
.__nwaiters
+= 1 << COND_NWAITERS_SHIFT
;
119 /* Remember the mutex we are using here. If there is already a
120 different address store this is a bad user bug. Do not store
121 anything for pshared condvars. */
122 if (cond
->__data
.__mutex
!= (void *) ~0l)
123 cond
->__data
.__mutex
= mutex
;
125 /* Prepare structure passed to cancellation handler. */
127 cbuffer
.mutex
= mutex
;
129 /* Before we block we enable cancellation. Therefore we have to
130 install a cancellation handler. */
131 __pthread_cleanup_push (&buffer
, __condvar_cleanup
, &cbuffer
);
133 /* The current values of the wakeup counter. The "woken" counter
134 must exceed this value. */
135 unsigned long long int val
;
136 unsigned long long int seq
;
137 val
= seq
= cond
->__data
.__wakeup_seq
;
138 /* Remember the broadcast counter. */
139 cbuffer
.bc_seq
= cond
->__data
.__broadcast_seq
;
143 unsigned int futex_val
= cond
->__data
.__futex
;
145 /* Prepare to wait. Release the condvar futex. */
146 lll_unlock (cond
->__data
.__lock
, pshared
);
148 /* Enable asynchronous cancellation. Required by the standard. */
149 cbuffer
.oldtype
= __pthread_enable_asynccancel ();
151 /* Wait until woken by signal or broadcast. */
152 lll_futex_wait (&cond
->__data
.__futex
, futex_val
, pshared
);
154 /* Disable asynchronous cancellation. */
155 __pthread_disable_asynccancel (cbuffer
.oldtype
);
157 /* We are going to look at shared data again, so get the lock. */
158 lll_lock (cond
->__data
.__lock
, pshared
);
160 /* If a broadcast happened, we are done. */
161 if (cbuffer
.bc_seq
!= cond
->__data
.__broadcast_seq
)
164 /* Check whether we are eligible for wakeup. */
165 val
= cond
->__data
.__wakeup_seq
;
167 while (val
== seq
|| cond
->__data
.__woken_seq
== val
);
169 /* Another thread woken up. */
170 ++cond
->__data
.__woken_seq
;
174 cond
->__data
.__nwaiters
-= 1 << COND_NWAITERS_SHIFT
;
176 /* If pthread_cond_destroy was called on this varaible already,
177 notify the pthread_cond_destroy caller all waiters have left
178 and it can be successfully destroyed. */
179 if (cond
->__data
.__total_seq
== -1ULL
180 && cond
->__data
.__nwaiters
< (1 << COND_NWAITERS_SHIFT
))
181 lll_futex_wake (&cond
->__data
.__nwaiters
, 1, pshared
);
183 /* We are done with the condvar. */
184 lll_unlock (cond
->__data
.__lock
, pshared
);
186 /* The cancellation handling is back to normal, remove the handler. */
187 __pthread_cleanup_pop (&buffer
, 0);
189 /* Get the mutex before returning. */
190 return __pthread_mutex_cond_lock (mutex
);
193 versioned_symbol (libpthread
, __pthread_cond_wait
, pthread_cond_wait
,