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.
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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
23 #include <lowlevellock.h>
27 #include <shlib-compat.h>
30 struct _condvar_cleanup_buffer
34 pthread_mutex_t
*mutex
;
40 __attribute__ ((visibility ("hidden")))
41 __condvar_cleanup (void *arg
)
43 struct _condvar_cleanup_buffer
*cbuffer
=
44 (struct _condvar_cleanup_buffer
*) arg
;
45 unsigned int destroying
;
47 /* We are going to modify shared data. */
48 lll_mutex_lock (cbuffer
->cond
->__data
.__lock
);
50 if (cbuffer
->bc_seq
== cbuffer
->cond
->__data
.__broadcast_seq
)
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
)
59 ++cbuffer
->cond
->__data
.__wakeup_seq
;
60 ++cbuffer
->cond
->__data
.__futex
;
62 ++cbuffer
->cond
->__data
.__woken_seq
;
65 cbuffer
->cond
->__data
.__nwaiters
-= 1 << COND_CLOCK_BITS
;
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. */
71 if (cbuffer
->cond
->__data
.__total_seq
== -1ULL
72 && cbuffer
->cond
->__data
.__nwaiters
< (1 << COND_CLOCK_BITS
))
74 lll_futex_wake (&cbuffer
->cond
->__data
.__nwaiters
, 1);
79 lll_mutex_unlock (cbuffer
->cond
->__data
.__lock
);
81 /* Wake everybody to make sure no condvar signal gets lost. */
83 lll_futex_wake (&cbuffer
->cond
->__data
.__futex
, INT_MAX
);
85 /* Get the mutex before returning unless asynchronous cancellation
87 __pthread_mutex_cond_lock (cbuffer
->mutex
);
92 __pthread_cond_wait (cond
, mutex
)
94 pthread_mutex_t
*mutex
;
96 struct _pthread_cleanup_buffer buffer
;
97 struct _condvar_cleanup_buffer cbuffer
;
100 /* Make sure we are along. */
101 lll_mutex_lock (cond
->__data
.__lock
);
103 /* Now we can release the mutex. */
104 err
= __pthread_mutex_unlock_usercnt (mutex
, 0);
105 if (__builtin_expect (err
, 0))
107 lll_mutex_unlock (cond
->__data
.__lock
);
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
;
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
;
122 /* Prepare structure passed to cancellation handler. */
124 cbuffer
.mutex
= mutex
;
126 /* Before we block we enable cancellation. Therefore we have to
127 install a cancellation handler. */
128 __pthread_cleanup_push (&buffer
, __condvar_cleanup
, &cbuffer
);
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
;
140 unsigned int futex_val
= cond
->__data
.__futex
;
142 /* Prepare to wait. Release the condvar futex. */
143 lll_mutex_unlock (cond
->__data
.__lock
);
145 /* Enable asynchronous cancellation. Required by the standard. */
146 cbuffer
.oldtype
= __pthread_enable_asynccancel ();
148 /* Wait until woken by signal or broadcast. */
149 lll_futex_wait (&cond
->__data
.__futex
, futex_val
);
151 /* Disable asynchronous cancellation. */
152 __pthread_disable_asynccancel (cbuffer
.oldtype
);
154 /* We are going to look at shared data again, so get the lock. */
155 lll_mutex_lock (cond
->__data
.__lock
);
157 /* If a broadcast happened, we are done. */
158 if (cbuffer
.bc_seq
!= cond
->__data
.__broadcast_seq
)
161 /* Check whether we are eligible for wakeup. */
162 val
= cond
->__data
.__wakeup_seq
;
164 while (val
== seq
|| cond
->__data
.__woken_seq
== val
);
166 /* Another thread woken up. */
167 ++cond
->__data
.__woken_seq
;
171 cond
->__data
.__nwaiters
-= 1 << COND_CLOCK_BITS
;
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);
180 /* We are done with the condvar. */
181 lll_mutex_unlock (cond
->__data
.__lock
);
183 /* The cancellation handling is back to normal, remove the handler. */
184 __pthread_cleanup_pop (&buffer
, 0);
186 /* Get the mutex before returning. */
187 return __pthread_mutex_cond_lock (mutex
);
190 versioned_symbol (libpthread
, __pthread_cond_wait
, pthread_cond_wait
,