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"
25 int sem_init(sem_t
*sem
, int pshared
, unsigned int value
)
27 if (value
> SEM_VALUE_MAX
) {
35 __pthread_init_lock((struct _pthread_fastlock
*) &sem
->__sem_lock
);
36 sem
->__sem_value
= value
;
37 sem
->__sem_waiting
= NULL
;
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) {
48 __pthread_unlock((struct _pthread_fastlock
*) &sem
->__sem_lock
);
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 */
68 int sem_trywait(sem_t
* sem
)
72 __pthread_lock((struct _pthread_fastlock
*) &sem
->__sem_lock
, NULL
);
73 if (sem
->__sem_value
== 0) {
80 __pthread_unlock((struct _pthread_fastlock
*) &sem
->__sem_lock
);
84 int sem_post(sem_t
* sem
)
86 pthread_descr self
= thread_self();
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
) {
96 __pthread_unlock((struct _pthread_fastlock
*) &sem
->__sem_lock
);
100 __pthread_unlock((struct _pthread_fastlock
*) &sem
->__sem_lock
);
102 th
= dequeue(&sem
->__sem_waiting
);
103 __pthread_unlock((struct _pthread_fastlock
*) &sem
->__sem_lock
);
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) {
115 request
.req_kind
= REQ_POST
;
116 request
.req_args
.post
= sem
;
117 __libc_write(__pthread_manager_request
,
118 (char *) &request
, sizeof(request
));
123 int sem_getvalue(sem_t
* sem
, int * sval
)
125 *sval
= sem
->__sem_value
;
129 int sem_destroy(sem_t
* sem
)
131 if (sem
->__sem_waiting
!= NULL
) {
138 sem_t
*sem_open(const char *name
, int oflag
, ...)
140 __set_errno (ENOSYS
);
144 int sem_close(sem_t
*sem
)
146 __set_errno (ENOSYS
);
150 int sem_unlink(const char *name
)
152 __set_errno (ENOSYS
);