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). */
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. */
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 */
23 #include "internals.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
;
36 versioned_symbol (libpthread
, __pthread_cond_init
, pthread_cond_init
,
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
,
44 int __pthread_cond_destroy(pthread_cond_t
*cond
)
46 if (cond
->__c_waiting
!= NULL
) return EBUSY
;
49 versioned_symbol (libpthread
, __pthread_cond_destroy
, pthread_cond_destroy
,
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
,
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
;
66 __pthread_lock(&cond
->__c_lock
, self
);
67 did_remove
= remove_from_queue(&cond
->__c_waiting
, th
);
68 __pthread_unlock(&cond
->__c_lock
);
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
)
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
);
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;
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
++;
131 __pthread_set_own_extricate_if(self
, 0);
133 /* Check for cancellation again, to provide correct cancellation
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
--)
147 pthread_mutex_lock(mutex
);
150 versioned_symbol (libpthread
, __pthread_cond_wait
, pthread_cond_wait
,
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
,
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
)
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
);
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;
201 if (!timedsuspend(self
, abstime
)) {
204 /* __pthread_lock will queue back any spurious restarts that
207 __pthread_lock(&cond
->__c_lock
, self
);
208 was_on_queue
= remove_from_queue(&cond
->__c_waiting
, self
);
209 __pthread_unlock(&cond
->__c_lock
);
212 __pthread_set_own_extricate_if(self
, 0);
213 pthread_mutex_lock(mutex
);
217 /* Eat the outstanding restart() from the signaller */
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
++;
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
--)
248 pthread_mutex_lock(mutex
);
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
,
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
);
266 int __pthread_cond_signal(pthread_cond_t
*cond
)
270 __pthread_lock(&cond
->__c_lock
, NULL
);
271 th
= dequeue(&cond
->__c_waiting
);
272 __pthread_unlock(&cond
->__c_lock
);
274 th
->p_condvar_avail
= 1;
275 WRITE_MEMORY_BARRIER();
280 versioned_symbol (libpthread
, __pthread_cond_signal
, pthread_cond_signal
,
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
,
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();
305 versioned_symbol (libpthread
, __pthread_cond_broadcast
, pthread_cond_broadcast
,
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
);
313 int __pthread_condattr_init(pthread_condattr_t
*attr
)
317 strong_alias (__pthread_condattr_init
, pthread_condattr_init
)
319 int __pthread_condattr_destroy(pthread_condattr_t
*attr
)
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
;
331 int pthread_condattr_setpshared (pthread_condattr_t
*attr
, int pshared
)
333 if (pshared
!= PTHREAD_PROCESS_PRIVATE
&& pshared
!= PTHREAD_PROCESS_SHARED
)
336 /* For now it is not possible to shared a conditional variable. */
337 if (pshared
!= PTHREAD_PROCESS_PRIVATE
)