1 /* Linuxthreads - a simple clone()-based implementation of Posix */
2 /* threads for Linux. */
3 /* Copyright (C) 1996 Xavier Leroy (Xavier.Leroy@inria.fr) */
5 /* This program is free software; you can redistribute it and/or */
6 /* modify it under the terms of the GNU Library General Public License */
7 /* as published by the Free Software Foundation; either version 2 */
8 /* of the License, or (at your option) any later version. */
10 /* This program 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 */
13 /* GNU Library General Public License for more details. */
15 /* Semaphores a la POSIX 1003.1b */
19 #include "semaphore.h"
20 #include "internals.h"
24 #include <shlib-compat.h>
26 int __new_sem_init(sem_t
*sem
, int pshared
, unsigned int value
)
28 if (value
> SEM_VALUE_MAX
) {
36 __pthread_init_lock((struct _pthread_fastlock
*) &sem
->__sem_lock
);
37 sem
->__sem_value
= value
;
38 sem
->__sem_waiting
= NULL
;
42 /* Function called by pthread_cancel to remove the thread from
43 waiting inside __new_sem_wait. */
45 static int new_sem_extricate_func(void *obj
, pthread_descr th
)
47 volatile pthread_descr self
= thread_self();
51 __pthread_lock((struct _pthread_fastlock
*) &sem
->__sem_lock
, self
);
52 did_remove
= remove_from_queue(&sem
->__sem_waiting
, th
);
53 __pthread_unlock((struct _pthread_fastlock
*) &sem
->__sem_lock
);
58 int __new_sem_wait(sem_t
* sem
)
60 volatile pthread_descr self
= thread_self();
61 pthread_extricate_if extr
;
62 int already_canceled
= 0;
63 int spurious_wakeup_count
;
65 /* Set up extrication interface */
67 extr
.pu_extricate_func
= new_sem_extricate_func
;
69 __pthread_lock((struct _pthread_fastlock
*) &sem
->__sem_lock
, self
);
70 if (sem
->__sem_value
> 0) {
72 __pthread_unlock((struct _pthread_fastlock
*) &sem
->__sem_lock
);
75 /* Register extrication interface */
76 THREAD_SETMEM(self
, p_sem_avail
, 0);
77 __pthread_set_own_extricate_if(self
, &extr
);
78 /* Enqueue only if not already cancelled. */
79 if (!(THREAD_GETMEM(self
, p_canceled
)
80 && THREAD_GETMEM(self
, p_cancelstate
) == PTHREAD_CANCEL_ENABLE
))
81 enqueue(&sem
->__sem_waiting
, self
);
84 __pthread_unlock((struct _pthread_fastlock
*) &sem
->__sem_lock
);
86 if (already_canceled
) {
87 __pthread_set_own_extricate_if(self
, 0);
88 pthread_exit(PTHREAD_CANCELED
);
91 /* Wait for sem_post or cancellation, or fall through if already canceled */
92 spurious_wakeup_count
= 0;
96 if (THREAD_GETMEM(self
, p_sem_avail
) == 0
97 && (THREAD_GETMEM(self
, p_woken_by_cancel
) == 0
98 || THREAD_GETMEM(self
, p_cancelstate
) != PTHREAD_CANCEL_ENABLE
))
100 /* Count resumes that don't belong to us. */
101 spurious_wakeup_count
++;
106 __pthread_set_own_extricate_if(self
, 0);
108 /* Terminate only if the wakeup came from cancellation. */
109 /* Otherwise ignore cancellation because we got the semaphore. */
111 if (THREAD_GETMEM(self
, p_woken_by_cancel
)
112 && THREAD_GETMEM(self
, p_cancelstate
) == PTHREAD_CANCEL_ENABLE
) {
113 THREAD_SETMEM(self
, p_woken_by_cancel
, 0);
114 pthread_exit(PTHREAD_CANCELED
);
116 /* We got the semaphore */
120 int __new_sem_trywait(sem_t
* sem
)
124 __pthread_lock((struct _pthread_fastlock
*) &sem
->__sem_lock
, NULL
);
125 if (sem
->__sem_value
== 0) {
132 __pthread_unlock((struct _pthread_fastlock
*) &sem
->__sem_lock
);
136 int __new_sem_post(sem_t
* sem
)
138 pthread_descr self
= thread_self();
140 struct pthread_request request
;
142 if (THREAD_GETMEM(self
, p_in_sighandler
) == NULL
) {
143 __pthread_lock((struct _pthread_fastlock
*) &sem
->__sem_lock
, self
);
144 if (sem
->__sem_waiting
== NULL
) {
145 if (sem
->__sem_value
>= SEM_VALUE_MAX
) {
148 __pthread_unlock((struct _pthread_fastlock
*) &sem
->__sem_lock
);
152 __pthread_unlock((struct _pthread_fastlock
*) &sem
->__sem_lock
);
154 th
= dequeue(&sem
->__sem_waiting
);
155 __pthread_unlock((struct _pthread_fastlock
*) &sem
->__sem_lock
);
157 WRITE_MEMORY_BARRIER();
161 /* If we're in signal handler, delegate post operation to
162 the thread manager. */
163 if (__pthread_manager_request
< 0) {
164 if (__pthread_initialize_manager() < 0) {
169 request
.req_kind
= REQ_POST
;
170 request
.req_args
.post
= sem
;
171 __libc_write(__pthread_manager_request
,
172 (char *) &request
, sizeof(request
));
177 int __new_sem_getvalue(sem_t
* sem
, int * sval
)
179 *sval
= sem
->__sem_value
;
183 int __new_sem_destroy(sem_t
* sem
)
185 if (sem
->__sem_waiting
!= NULL
) {
192 sem_t
*sem_open(const char *name
, int oflag
, ...)
194 __set_errno (ENOSYS
);
198 int sem_close(sem_t
*sem
)
200 __set_errno (ENOSYS
);
204 int sem_unlink(const char *name
)
206 __set_errno (ENOSYS
);
210 int sem_timedwait(sem_t
*sem
, const struct timespec
*abstime
)
212 pthread_descr self
= thread_self();
213 pthread_extricate_if extr
;
214 int already_canceled
= 0;
215 int spurious_wakeup_count
;
217 __pthread_lock((struct _pthread_fastlock
*) &sem
->__sem_lock
, self
);
218 if (sem
->__sem_value
> 0) {
220 __pthread_unlock((struct _pthread_fastlock
*) &sem
->__sem_lock
);
224 if (abstime
->tv_nsec
< 0 || abstime
->tv_nsec
>= 1000000000) {
225 /* The standard requires that if the function would block and the
226 time value is illegal, the function returns with an error. */
227 __pthread_unlock((struct _pthread_fastlock
*) &sem
->__sem_lock
);
231 /* Set up extrication interface */
232 extr
.pu_object
= sem
;
233 extr
.pu_extricate_func
= new_sem_extricate_func
;
235 /* Register extrication interface */
236 THREAD_SETMEM(self
, p_sem_avail
, 0);
237 __pthread_set_own_extricate_if(self
, &extr
);
238 /* Enqueue only if not already cancelled. */
239 if (!(THREAD_GETMEM(self
, p_canceled
)
240 && THREAD_GETMEM(self
, p_cancelstate
) == PTHREAD_CANCEL_ENABLE
))
241 enqueue(&sem
->__sem_waiting
, self
);
243 already_canceled
= 1;
244 __pthread_unlock((struct _pthread_fastlock
*) &sem
->__sem_lock
);
246 if (already_canceled
) {
247 __pthread_set_own_extricate_if(self
, 0);
248 pthread_exit(PTHREAD_CANCELED
);
251 spurious_wakeup_count
= 0;
254 if (timedsuspend(self
, abstime
) == 0) {
257 /* __pthread_lock will queue back any spurious restarts that
260 __pthread_lock((struct _pthread_fastlock
*)&sem
->__sem_lock
, self
);
261 was_on_queue
= remove_from_queue(&sem
->__sem_waiting
, self
);
262 __pthread_unlock((struct _pthread_fastlock
*)&sem
->__sem_lock
);
265 __pthread_set_own_extricate_if(self
, 0);
269 /* Eat the outstanding restart() from the signaller */
273 if (THREAD_GETMEM(self
, p_sem_avail
) == 0
274 && (THREAD_GETMEM(self
, p_woken_by_cancel
) == 0
275 || THREAD_GETMEM(self
, p_cancelstate
) != PTHREAD_CANCEL_ENABLE
))
277 /* Count resumes that don't belong to us. */
278 spurious_wakeup_count
++;
284 __pthread_set_own_extricate_if(self
, 0);
286 /* Terminate only if the wakeup came from cancellation. */
287 /* Otherwise ignore cancellation because we got the semaphore. */
289 if (THREAD_GETMEM(self
, p_woken_by_cancel
)
290 && THREAD_GETMEM(self
, p_cancelstate
) == PTHREAD_CANCEL_ENABLE
) {
291 THREAD_SETMEM(self
, p_woken_by_cancel
, 0);
292 pthread_exit(PTHREAD_CANCELED
);
294 /* We got the semaphore */
299 versioned_symbol (libpthread
, __new_sem_init
, sem_init
, GLIBC_2_1
);
300 versioned_symbol (libpthread
, __new_sem_wait
, sem_wait
, GLIBC_2_1
);
301 versioned_symbol (libpthread
, __new_sem_trywait
, sem_trywait
, GLIBC_2_1
);
302 versioned_symbol (libpthread
, __new_sem_post
, sem_post
, GLIBC_2_1
);
303 versioned_symbol (libpthread
, __new_sem_getvalue
, sem_getvalue
, GLIBC_2_1
);
304 versioned_symbol (libpthread
, __new_sem_destroy
, sem_destroy
, GLIBC_2_1
);