Add symbol versioning. The compatibility versions are the same as the
[kopensolaris-gnu/glibc.git] / linuxthreads / condvar.c
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.                 */
15
16 /* Condition variables */
17
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>
28
29 int __pthread_cond_init(pthread_cond_t *cond,
30                         const pthread_condattr_t *cond_attr)
31 {
32   __pthread_init_lock(&cond->__c_lock);
33   cond->__c_waiting = NULL;
34   return 0;
35 }
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
43
44 int __pthread_cond_destroy(pthread_cond_t *cond)
45 {
46   if (cond->__c_waiting != NULL) return EBUSY;
47   return 0;
48 }
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
56
57 /* Function called by pthread_cancel to remove the thread from
58    waiting on a condition variable queue. */
59
60 static int cond_extricate_func(void *obj, pthread_descr th)
61 {
62   volatile pthread_descr self = thread_self();
63   pthread_cond_t *cond = obj;
64   int did_remove = 0;
65
66   __pthread_lock(&cond->__c_lock, self);
67   did_remove = remove_from_queue(&cond->__c_waiting, th);
68   __pthread_unlock(&cond->__c_lock);
69
70   return did_remove;
71 }
72
73 int __pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
74 {
75   volatile pthread_descr self = thread_self();
76   pthread_extricate_if extr;
77   int already_canceled = 0;
78   int spurious_wakeup_count;
79
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;
85
86   /* Set up extrication interface */
87   extr.pu_object = cond;
88   extr.pu_extricate_func = cond_extricate_func;
89
90   /* Register extrication interface */
91   THREAD_SETMEM(self, p_condvar_avail, 0);
92   __pthread_set_own_extricate_if(self, &extr);
93
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. */
100
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);
108
109   if (already_canceled) {
110     __pthread_set_own_extricate_if(self, 0);
111     __pthread_do_exit(PTHREAD_CANCELED, CURRENT_STACK_FRAME);
112   }
113
114   pthread_mutex_unlock(mutex);
115
116   spurious_wakeup_count = 0;
117   while (1)
118     {
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))
123         {
124           /* Count resumes that don't belong to us. */
125           spurious_wakeup_count++;
126           continue;
127         }
128       break;
129     }
130
131   __pthread_set_own_extricate_if(self, 0);
132
133   /* Check for cancellation again, to provide correct cancellation
134      point behavior */
135
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);
141   }
142
143   /* Put back any resumes we caught that don't belong to us. */
144   while (spurious_wakeup_count--)
145     restart(self);
146
147   pthread_mutex_lock(mutex);
148   return 0;
149 }
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
157
158 static int
159 pthread_cond_timedwait_relative(pthread_cond_t *cond,
160                                 pthread_mutex_t *mutex,
161                                 const struct timespec * abstime)
162 {
163   volatile pthread_descr self = thread_self();
164   int already_canceled = 0;
165   pthread_extricate_if extr;
166   int spurious_wakeup_count;
167
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;
173
174   /* Set up extrication interface */
175   extr.pu_object = cond;
176   extr.pu_extricate_func = cond_extricate_func;
177
178   /* Register extrication interface */
179   THREAD_SETMEM(self, p_condvar_avail, 0);
180   __pthread_set_own_extricate_if(self, &extr);
181
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);
190
191   if (already_canceled) {
192     __pthread_set_own_extricate_if(self, 0);
193     __pthread_do_exit(PTHREAD_CANCELED, CURRENT_STACK_FRAME);
194   }
195
196   pthread_mutex_unlock(mutex);
197
198   spurious_wakeup_count = 0;
199   while (1)
200     {
201       if (!timedsuspend(self, abstime)) {
202         int was_on_queue;
203
204         /* __pthread_lock will queue back any spurious restarts that
205            may happen to it. */
206
207         __pthread_lock(&cond->__c_lock, self);
208         was_on_queue = remove_from_queue(&cond->__c_waiting, self);
209         __pthread_unlock(&cond->__c_lock);
210
211         if (was_on_queue) {
212           __pthread_set_own_extricate_if(self, 0);
213           pthread_mutex_lock(mutex);
214           return ETIMEDOUT;
215         }
216
217         /* Eat the outstanding restart() from the signaller */
218         suspend(self);
219       }
220
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))
224         {
225           /* Count resumes that don't belong to us. */
226           spurious_wakeup_count++;
227           continue;
228         }
229       break;
230     }
231
232   __pthread_set_own_extricate_if(self, 0);
233
234   /* The remaining logic is the same as in other cancellable waits,
235      such as pthread_join sem_wait or pthread_cond wait. */
236
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);
242   }
243
244   /* Put back any resumes we caught that don't belong to us. */
245   while (spurious_wakeup_count--)
246     restart(self);
247
248   pthread_mutex_lock(mutex);
249   return 0;
250 }
251
252 int __pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
253                              const struct timespec * abstime)
254 {
255   /* Indirect call through pointer! */
256   return pthread_cond_timedwait_relative(cond, mutex, abstime);
257 }
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
265
266 int __pthread_cond_signal(pthread_cond_t *cond)
267 {
268   pthread_descr th;
269
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);
277   }
278   return 0;
279 }
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
287
288 int __pthread_cond_broadcast(pthread_cond_t *cond)
289 {
290   pthread_descr tosignal, th;
291
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);
302   }
303   return 0;
304 }
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
312
313 int __pthread_condattr_init(pthread_condattr_t *attr)
314 {
315   return 0;
316 }
317 strong_alias (__pthread_condattr_init, pthread_condattr_init)
318
319 int __pthread_condattr_destroy(pthread_condattr_t *attr)
320 {
321   return 0;
322 }
323 strong_alias (__pthread_condattr_destroy, pthread_condattr_destroy)
324
325 int pthread_condattr_getpshared (const pthread_condattr_t *attr, int *pshared)
326 {
327   *pshared = PTHREAD_PROCESS_PRIVATE;
328   return 0;
329 }
330
331 int pthread_condattr_setpshared (pthread_condattr_t *attr, int pshared)
332 {
333   if (pshared != PTHREAD_PROCESS_PRIVATE && pshared != PTHREAD_PROCESS_SHARED)
334     return EINVAL;
335
336   /* For now it is not possible to shared a conditional variable.  */
337   if (pshared != PTHREAD_PROCESS_PRIVATE)
338     return ENOSYS;
339
340   return 0;
341 }