Update.
[glibc.git] / linuxthreads / semaphore.c
blob6f79dc0138a251c0ac8c82a9318e3781e2b17a7f
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"
25 int sem_init(sem_t *sem, int pshared, unsigned int value)
27 if (value > SEM_VALUE_MAX) {
28 errno = EINVAL;
29 return -1;
31 if (pshared) {
32 errno = ENOSYS;
33 return -1;
35 __pthread_init_lock((struct _pthread_fastlock *) &sem->sem_lock);
36 sem->sem_value = value;
37 sem->sem_waiting = NULL;
38 return 0;
41 int sem_wait(sem_t * sem)
43 volatile pthread_descr self;
45 __pthread_lock((struct _pthread_fastlock *) &sem->sem_lock);
46 if (sem->sem_value > 0) {
47 sem->sem_value--;
48 __pthread_unlock((struct _pthread_fastlock *) &sem->sem_lock);
49 return 0;
51 self = thread_self();
52 enqueue(&sem->sem_waiting, self);
53 /* Wait for sem_post or cancellation */
54 __pthread_unlock((struct _pthread_fastlock *) &sem->sem_lock);
55 suspend_with_cancellation(self);
56 /* This is a cancellation point */
57 if (self->p_canceled && self->p_cancelstate == PTHREAD_CANCEL_ENABLE) {
58 /* Remove ourselves from the waiting list if we're still on it */
59 __pthread_lock((struct _pthread_fastlock *) &sem->sem_lock);
60 remove_from_queue(&sem->sem_waiting, self);
61 __pthread_unlock((struct _pthread_fastlock *) &sem->sem_lock);
62 pthread_exit(PTHREAD_CANCELED);
64 /* We got the semaphore */
65 return 0;
68 int sem_trywait(sem_t * sem)
70 int retval;
72 __pthread_lock((struct _pthread_fastlock *) &sem->sem_lock);
73 if (sem->sem_value == 0) {
74 errno = EAGAIN;
75 retval = -1;
76 } else {
77 sem->sem_value--;
78 retval = 0;
80 return retval;
83 int sem_post(sem_t * sem)
85 pthread_descr self = thread_self();
86 pthread_descr th;
87 struct pthread_request request;
89 if (self->p_in_sighandler == NULL) {
90 __pthread_lock((struct _pthread_fastlock *) &sem->sem_lock);
91 if (sem->sem_waiting == NULL) {
92 if (sem->sem_value >= SEM_VALUE_MAX) {
93 /* Overflow */
94 errno = ERANGE;
95 __pthread_unlock((struct _pthread_fastlock *) &sem->sem_lock);
96 return -1;
98 sem->sem_value++;
99 __pthread_unlock((struct _pthread_fastlock *) &sem->sem_lock);
100 } else {
101 th = dequeue(&sem->sem_waiting);
102 __pthread_unlock((struct _pthread_fastlock *) &sem->sem_lock);
103 restart(th);
105 } else {
106 /* If we're in signal handler, delegate post operation to
107 the thread manager. */
108 if (__pthread_manager_request < 0) {
109 if (__pthread_initialize_manager() < 0) {
110 errno = EAGAIN;
111 return -1;
114 request.req_kind = REQ_POST;
115 request.req_args.post = sem;
116 __libc_write(__pthread_manager_request,
117 (char *) &request, sizeof(request));
119 return 0;
122 int sem_getvalue(sem_t * sem, int * sval)
124 *sval = sem->sem_value;
125 return 0;
128 int sem_destroy(sem_t * sem)
130 if (sem->sem_waiting != NULL) {
131 errno = EBUSY;
132 return -1;
134 return 0;