(INTERNAL_SYSCALL): Add LOAD_REGS_##nr. (LOAD_ARGS_0, LOAD_ARGS_1, LOAD_ARGS_2, LOAD_...
[glibc.git] / linuxthreads / semaphore.c
blobe0dac4120020b911bbd0f47b72d03306d05eb120
1 /* Linuxthreads - a simple clone()-based implementation of Posix */
2 /* threads for Linux. */
3 /* Copyright (C) 1996 Xavier Leroy (Xavier.Leroy@inria.fr) */
4 /* */
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. */
9 /* */
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 */
17 #include <errno.h>
18 #include "pthread.h"
19 #include "semaphore.h"
20 #include "internals.h"
21 #include "spinlock.h"
22 #include "restart.h"
23 #include "queue.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) {
29 errno = EINVAL;
30 return -1;
32 if (pshared) {
33 errno = ENOSYS;
34 return -1;
36 __pthread_init_lock(&sem->__sem_lock);
37 sem->__sem_value = value;
38 sem->__sem_waiting = NULL;
39 return 0;
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();
48 sem_t *sem = obj;
49 int did_remove = 0;
51 __pthread_lock(&sem->__sem_lock, self);
52 did_remove = remove_from_queue(&sem->__sem_waiting, th);
53 __pthread_unlock(&sem->__sem_lock);
55 return did_remove;
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 */
66 extr.pu_object = sem;
67 extr.pu_extricate_func = new_sem_extricate_func;
69 __pthread_lock(&sem->__sem_lock, self);
70 if (sem->__sem_value > 0) {
71 sem->__sem_value--;
72 __pthread_unlock(&sem->__sem_lock);
73 return 0;
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);
82 else
83 already_canceled = 1;
84 __pthread_unlock(&sem->__sem_lock);
86 if (already_canceled) {
87 __pthread_set_own_extricate_if(self, 0);
88 __pthread_do_exit(PTHREAD_CANCELED, CURRENT_STACK_FRAME);
91 /* Wait for sem_post or cancellation, or fall through if already canceled */
92 spurious_wakeup_count = 0;
93 while (1)
95 suspend(self);
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++;
102 continue;
104 break;
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_do_exit(PTHREAD_CANCELED, CURRENT_STACK_FRAME);
116 /* We got the semaphore */
117 return 0;
120 int __new_sem_trywait(sem_t * sem)
122 int retval;
124 __pthread_lock(&sem->__sem_lock, NULL);
125 if (sem->__sem_value == 0) {
126 errno = EAGAIN;
127 retval = -1;
128 } else {
129 sem->__sem_value--;
130 retval = 0;
132 __pthread_unlock(&sem->__sem_lock);
133 return retval;
136 int __new_sem_post(sem_t * sem)
138 pthread_descr self = thread_self();
139 pthread_descr th;
140 struct pthread_request request;
142 if (THREAD_GETMEM(self, p_in_sighandler) == NULL) {
143 __pthread_lock(&sem->__sem_lock, self);
144 if (sem->__sem_waiting == NULL) {
145 if (sem->__sem_value >= SEM_VALUE_MAX) {
146 /* Overflow */
147 errno = ERANGE;
148 __pthread_unlock(&sem->__sem_lock);
149 return -1;
151 sem->__sem_value++;
152 __pthread_unlock(&sem->__sem_lock);
153 } else {
154 th = dequeue(&sem->__sem_waiting);
155 __pthread_unlock(&sem->__sem_lock);
156 th->p_sem_avail = 1;
157 WRITE_MEMORY_BARRIER();
158 restart(th);
160 } else {
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) {
165 errno = EAGAIN;
166 return -1;
169 request.req_kind = REQ_POST;
170 request.req_args.post = sem;
171 TEMP_FAILURE_RETRY(__libc_write(__pthread_manager_request,
172 (char *) &request, sizeof(request)));
174 return 0;
177 int __new_sem_getvalue(sem_t * sem, int * sval)
179 *sval = sem->__sem_value;
180 return 0;
183 int __new_sem_destroy(sem_t * sem)
185 if (sem->__sem_waiting != NULL) {
186 __set_errno (EBUSY);
187 return -1;
189 return 0;
192 sem_t *sem_open(const char *name, int oflag, ...)
194 __set_errno (ENOSYS);
195 return SEM_FAILED;
198 int sem_close(sem_t *sem)
200 __set_errno (ENOSYS);
201 return -1;
204 int sem_unlink(const char *name)
206 __set_errno (ENOSYS);
207 return -1;
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(&sem->__sem_lock, self);
218 if (sem->__sem_value > 0) {
219 --sem->__sem_value;
220 __pthread_unlock(&sem->__sem_lock);
221 return 0;
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(&sem->__sem_lock);
228 return EINVAL;
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);
242 else
243 already_canceled = 1;
244 __pthread_unlock(&sem->__sem_lock);
246 if (already_canceled) {
247 __pthread_set_own_extricate_if(self, 0);
248 __pthread_do_exit(PTHREAD_CANCELED, CURRENT_STACK_FRAME);
251 spurious_wakeup_count = 0;
252 while (1)
254 if (timedsuspend(self, abstime) == 0) {
255 int was_on_queue;
257 /* __pthread_lock will queue back any spurious restarts that
258 may happen to it. */
260 __pthread_lock(&sem->__sem_lock, self);
261 was_on_queue = remove_from_queue(&sem->__sem_waiting, self);
262 __pthread_unlock(&sem->__sem_lock);
264 if (was_on_queue) {
265 __pthread_set_own_extricate_if(self, 0);
266 return ETIMEDOUT;
269 /* Eat the outstanding restart() from the signaller */
270 suspend(self);
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++;
279 continue;
281 break;
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_do_exit(PTHREAD_CANCELED, CURRENT_STACK_FRAME);
294 /* We got the semaphore */
295 return 0;
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);