Update.
[glibc.git] / linuxthreads / semaphore.c
blob175302188eace54a36ed40ab4048017c58687c8e
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 = thread_self();
45 __pthread_lock((struct _pthread_fastlock *) &sem->__sem_lock, self);
46 if (sem->__sem_value > 0) {
47 sem->__sem_value--;
48 __pthread_unlock((struct _pthread_fastlock *) &sem->__sem_lock);
49 return 0;
51 enqueue(&sem->__sem_waiting, self);
52 /* Wait for sem_post or cancellation */
53 __pthread_unlock((struct _pthread_fastlock *) &sem->__sem_lock);
54 suspend_with_cancellation(self);
55 /* This is a cancellation point */
56 if (THREAD_GETMEM(self, p_canceled)
57 && THREAD_GETMEM(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, self);
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, NULL);
73 if (sem->__sem_value == 0) {
74 errno = EAGAIN;
75 retval = -1;
76 } else {
77 sem->__sem_value--;
78 retval = 0;
80 __pthread_unlock((struct _pthread_fastlock *) &sem->__sem_lock);
81 return retval;
84 int sem_post(sem_t * sem)
86 pthread_descr self = thread_self();
87 pthread_descr th;
88 struct pthread_request request;
90 if (THREAD_GETMEM(self, p_in_sighandler) == NULL) {
91 __pthread_lock((struct _pthread_fastlock *) &sem->__sem_lock, self);
92 if (sem->__sem_waiting == NULL) {
93 if (sem->__sem_value >= SEM_VALUE_MAX) {
94 /* Overflow */
95 errno = ERANGE;
96 __pthread_unlock((struct _pthread_fastlock *) &sem->__sem_lock);
97 return -1;
99 sem->__sem_value++;
100 __pthread_unlock((struct _pthread_fastlock *) &sem->__sem_lock);
101 } else {
102 th = dequeue(&sem->__sem_waiting);
103 __pthread_unlock((struct _pthread_fastlock *) &sem->__sem_lock);
104 restart(th);
106 } else {
107 /* If we're in signal handler, delegate post operation to
108 the thread manager. */
109 if (__pthread_manager_request < 0) {
110 if (__pthread_initialize_manager() < 0) {
111 errno = EAGAIN;
112 return -1;
115 request.req_kind = REQ_POST;
116 request.req_args.post = sem;
117 __libc_write(__pthread_manager_request,
118 (char *) &request, sizeof(request));
120 return 0;
123 int sem_getvalue(sem_t * sem, int * sval)
125 *sval = sem->__sem_value;
126 return 0;
129 int sem_destroy(sem_t * sem)
131 if (sem->__sem_waiting != NULL) {
132 __set_errno (EBUSY);
133 return -1;
135 return 0;
138 sem_t *sem_open(const char *name, int oflag, ...)
140 __set_errno (ENOSYS);
141 return SEM_FAILED;
144 int sem_close(sem_t *sem)
146 __set_errno (ENOSYS);
147 return -1;
150 int sem_unlink(const char *name)
152 __set_errno (ENOSYS);
153 return -1;