Updated to fedora-glibc-20050428T0846
[glibc.git] / linuxthreads / condvar.c
blob6ab95b88ae0a9204492c59e9cc6859c07186a240
1 /* Linuxthreads - a simple clone()-based implementation of Posix */
2 /* threads for Linux. */
3 /* Copyright (C) 1996 Xavier Leroy (Xavier.Leroy@inria.fr) */
4 /* and Pavel Krauz (krauz@fsid.cvut.cz). */
5 /* */
6 /* This program is free software; you can redistribute it and/or */
7 /* modify it under the terms of the GNU Library General Public License */
8 /* as published by the Free Software Foundation; either version 2 */
9 /* of the License, or (at your option) any later version. */
10 /* */
11 /* This program is distributed in the hope that it will be useful, */
12 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
13 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
14 /* GNU Library General Public License for more details. */
16 /* Condition variables */
18 #include <errno.h>
19 #include <sched.h>
20 #include <stddef.h>
21 #include <sys/time.h>
22 #include "pthread.h"
23 #include "internals.h"
24 #include "spinlock.h"
25 #include "queue.h"
26 #include "restart.h"
27 #include <shlib-compat.h>
29 int __pthread_cond_init(pthread_cond_t *cond,
30 const pthread_condattr_t *cond_attr)
32 __pthread_init_lock(&cond->__c_lock);
33 cond->__c_waiting = NULL;
34 return 0;
36 versioned_symbol (libpthread, __pthread_cond_init, pthread_cond_init,
37 GLIBC_2_3_2);
38 #if SHLIB_COMPAT(libpthread, GLIBC_2_0, GLIBC_2_3_2)
39 strong_alias (__pthread_cond_init, __old_pthread_cond_init)
40 compat_symbol (libpthread, __old_pthread_cond_init, pthread_cond_init,
41 GLIBC_2_0);
42 #endif
44 int __pthread_cond_destroy(pthread_cond_t *cond)
46 if (cond->__c_waiting != NULL) return EBUSY;
47 return 0;
49 versioned_symbol (libpthread, __pthread_cond_destroy, pthread_cond_destroy,
50 GLIBC_2_3_2);
51 #if SHLIB_COMPAT(libpthread, GLIBC_2_0, GLIBC_2_3_2)
52 strong_alias (__pthread_cond_destroy, __old_pthread_cond_destroy)
53 compat_symbol (libpthread, __old_pthread_cond_destroy, pthread_cond_destroy,
54 GLIBC_2_0);
55 #endif
57 /* Function called by pthread_cancel to remove the thread from
58 waiting on a condition variable queue. */
60 static int cond_extricate_func(void *obj, pthread_descr th)
62 volatile pthread_descr self = thread_self();
63 pthread_cond_t *cond = obj;
64 int did_remove = 0;
66 __pthread_lock(&cond->__c_lock, self);
67 did_remove = remove_from_queue(&cond->__c_waiting, th);
68 __pthread_unlock(&cond->__c_lock);
70 return did_remove;
73 int __pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
75 volatile pthread_descr self = thread_self();
76 pthread_extricate_if extr;
77 int already_canceled = 0;
78 int spurious_wakeup_count;
80 /* Check whether the mutex is locked and owned by this thread. */
81 if (mutex->__m_kind != PTHREAD_MUTEX_TIMED_NP
82 && mutex->__m_kind != PTHREAD_MUTEX_ADAPTIVE_NP
83 && mutex->__m_owner != self)
84 return EINVAL;
86 /* Set up extrication interface */
87 extr.pu_object = cond;
88 extr.pu_extricate_func = cond_extricate_func;
90 /* Register extrication interface */
91 THREAD_SETMEM(self, p_condvar_avail, 0);
92 __pthread_set_own_extricate_if(self, &extr);
94 /* Atomically enqueue thread for waiting, but only if it is not
95 canceled. If the thread is canceled, then it will fall through the
96 suspend call below, and then call pthread_exit without
97 having to worry about whether it is still on the condition variable queue.
98 This depends on pthread_cancel setting p_canceled before calling the
99 extricate function. */
101 __pthread_lock(&cond->__c_lock, self);
102 if (!(THREAD_GETMEM(self, p_canceled)
103 && THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE))
104 enqueue(&cond->__c_waiting, self);
105 else
106 already_canceled = 1;
107 __pthread_unlock(&cond->__c_lock);
109 if (already_canceled) {
110 __pthread_set_own_extricate_if(self, 0);
111 __pthread_do_exit(PTHREAD_CANCELED, CURRENT_STACK_FRAME);
114 pthread_mutex_unlock(mutex);
116 spurious_wakeup_count = 0;
117 while (1)
119 suspend(self);
120 if (THREAD_GETMEM(self, p_condvar_avail) == 0
121 && (THREAD_GETMEM(self, p_woken_by_cancel) == 0
122 || THREAD_GETMEM(self, p_cancelstate) != PTHREAD_CANCEL_ENABLE))
124 /* Count resumes that don't belong to us. */
125 spurious_wakeup_count++;
126 continue;
128 break;
131 __pthread_set_own_extricate_if(self, 0);
133 /* Check for cancellation again, to provide correct cancellation
134 point behavior */
136 if (THREAD_GETMEM(self, p_woken_by_cancel)
137 && THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE) {
138 THREAD_SETMEM(self, p_woken_by_cancel, 0);
139 pthread_mutex_lock(mutex);
140 __pthread_do_exit(PTHREAD_CANCELED, CURRENT_STACK_FRAME);
143 /* Put back any resumes we caught that don't belong to us. */
144 while (spurious_wakeup_count--)
145 restart(self);
147 pthread_mutex_lock(mutex);
148 return 0;
150 versioned_symbol (libpthread, __pthread_cond_wait, pthread_cond_wait,
151 GLIBC_2_3_2);
152 #if SHLIB_COMPAT(libpthread, GLIBC_2_0, GLIBC_2_3_2)
153 strong_alias (__pthread_cond_wait, __old_pthread_cond_wait)
154 compat_symbol (libpthread, __old_pthread_cond_wait, pthread_cond_wait,
155 GLIBC_2_0);
156 #endif
158 static int
159 pthread_cond_timedwait_relative(pthread_cond_t *cond,
160 pthread_mutex_t *mutex,
161 const struct timespec * abstime)
163 volatile pthread_descr self = thread_self();
164 int already_canceled = 0;
165 pthread_extricate_if extr;
166 int spurious_wakeup_count;
168 /* Check whether the mutex is locked and owned by this thread. */
169 if (mutex->__m_kind != PTHREAD_MUTEX_TIMED_NP
170 && mutex->__m_kind != PTHREAD_MUTEX_ADAPTIVE_NP
171 && mutex->__m_owner != self)
172 return EINVAL;
174 /* Set up extrication interface */
175 extr.pu_object = cond;
176 extr.pu_extricate_func = cond_extricate_func;
178 /* Register extrication interface */
179 THREAD_SETMEM(self, p_condvar_avail, 0);
180 __pthread_set_own_extricate_if(self, &extr);
182 /* Enqueue to wait on the condition and check for cancellation. */
183 __pthread_lock(&cond->__c_lock, self);
184 if (!(THREAD_GETMEM(self, p_canceled)
185 && THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE))
186 enqueue(&cond->__c_waiting, self);
187 else
188 already_canceled = 1;
189 __pthread_unlock(&cond->__c_lock);
191 if (already_canceled) {
192 __pthread_set_own_extricate_if(self, 0);
193 __pthread_do_exit(PTHREAD_CANCELED, CURRENT_STACK_FRAME);
196 pthread_mutex_unlock(mutex);
198 spurious_wakeup_count = 0;
199 while (1)
201 if (!timedsuspend(self, abstime)) {
202 int was_on_queue;
204 /* __pthread_lock will queue back any spurious restarts that
205 may happen to it. */
207 __pthread_lock(&cond->__c_lock, self);
208 was_on_queue = remove_from_queue(&cond->__c_waiting, self);
209 __pthread_unlock(&cond->__c_lock);
211 if (was_on_queue) {
212 __pthread_set_own_extricate_if(self, 0);
213 pthread_mutex_lock(mutex);
214 return ETIMEDOUT;
217 /* Eat the outstanding restart() from the signaller */
218 suspend(self);
221 if (THREAD_GETMEM(self, p_condvar_avail) == 0
222 && (THREAD_GETMEM(self, p_woken_by_cancel) == 0
223 || THREAD_GETMEM(self, p_cancelstate) != PTHREAD_CANCEL_ENABLE))
225 /* Count resumes that don't belong to us. */
226 spurious_wakeup_count++;
227 continue;
229 break;
232 __pthread_set_own_extricate_if(self, 0);
234 /* The remaining logic is the same as in other cancellable waits,
235 such as pthread_join sem_wait or pthread_cond wait. */
237 if (THREAD_GETMEM(self, p_woken_by_cancel)
238 && THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE) {
239 THREAD_SETMEM(self, p_woken_by_cancel, 0);
240 pthread_mutex_lock(mutex);
241 __pthread_do_exit(PTHREAD_CANCELED, CURRENT_STACK_FRAME);
244 /* Put back any resumes we caught that don't belong to us. */
245 while (spurious_wakeup_count--)
246 restart(self);
248 pthread_mutex_lock(mutex);
249 return 0;
252 int __pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
253 const struct timespec * abstime)
255 /* Indirect call through pointer! */
256 return pthread_cond_timedwait_relative(cond, mutex, abstime);
258 versioned_symbol (libpthread, __pthread_cond_timedwait, pthread_cond_timedwait,
259 GLIBC_2_3_2);
260 #if SHLIB_COMPAT(libpthread, GLIBC_2_0, GLIBC_2_3_2)
261 strong_alias (__pthread_cond_timedwait, __old_pthread_cond_timedwait)
262 compat_symbol (libpthread, __old_pthread_cond_timedwait,
263 pthread_cond_timedwait, GLIBC_2_0);
264 #endif
266 int __pthread_cond_signal(pthread_cond_t *cond)
268 pthread_descr th;
270 __pthread_lock(&cond->__c_lock, NULL);
271 th = dequeue(&cond->__c_waiting);
272 __pthread_unlock(&cond->__c_lock);
273 if (th != NULL) {
274 th->p_condvar_avail = 1;
275 WRITE_MEMORY_BARRIER();
276 restart(th);
278 return 0;
280 versioned_symbol (libpthread, __pthread_cond_signal, pthread_cond_signal,
281 GLIBC_2_3_2);
282 #if SHLIB_COMPAT(libpthread, GLIBC_2_0, GLIBC_2_3_2)
283 strong_alias (__pthread_cond_signal, __old_pthread_cond_signal)
284 compat_symbol (libpthread, __old_pthread_cond_signal, pthread_cond_signal,
285 GLIBC_2_0);
286 #endif
288 int __pthread_cond_broadcast(pthread_cond_t *cond)
290 pthread_descr tosignal, th;
292 __pthread_lock(&cond->__c_lock, NULL);
293 /* Copy the current state of the waiting queue and empty it */
294 tosignal = cond->__c_waiting;
295 cond->__c_waiting = NULL;
296 __pthread_unlock(&cond->__c_lock);
297 /* Now signal each process in the queue */
298 while ((th = dequeue(&tosignal)) != NULL) {
299 th->p_condvar_avail = 1;
300 WRITE_MEMORY_BARRIER();
301 restart(th);
303 return 0;
305 versioned_symbol (libpthread, __pthread_cond_broadcast, pthread_cond_broadcast,
306 GLIBC_2_3_2);
307 #if SHLIB_COMPAT(libpthread, GLIBC_2_0, GLIBC_2_3_2)
308 strong_alias (__pthread_cond_broadcast, __old_pthread_cond_broadcast)
309 compat_symbol (libpthread, __old_pthread_cond_broadcast,
310 pthread_cond_broadcast, GLIBC_2_0);
311 #endif
313 int __pthread_condattr_init(pthread_condattr_t *attr)
315 return 0;
317 strong_alias (__pthread_condattr_init, pthread_condattr_init)
319 int __pthread_condattr_destroy(pthread_condattr_t *attr)
321 return 0;
323 strong_alias (__pthread_condattr_destroy, pthread_condattr_destroy)
325 int pthread_condattr_getpshared (const pthread_condattr_t *attr, int *pshared)
327 *pshared = PTHREAD_PROCESS_PRIVATE;
328 return 0;
331 int pthread_condattr_setpshared (pthread_condattr_t *attr, int pshared)
333 if (pshared != PTHREAD_PROCESS_PRIVATE && pshared != PTHREAD_PROCESS_SHARED)
334 return EINVAL;
336 /* For now it is not possible to shared a conditional variable. */
337 if (pshared != PTHREAD_PROCESS_PRIVATE)
338 return ENOSYS;
340 return 0;