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
;
45 __pthread_lock((struct _pthread_fastlock
*) &sem
->sem_lock
);
46 if (sem
->sem_value
> 0) {
48 __pthread_unlock((struct _pthread_fastlock
*) &sem
->sem_lock
);
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 */
68 int sem_trywait(sem_t
* sem
)
72 __pthread_lock((struct _pthread_fastlock
*) &sem
->sem_lock
);
73 if (sem
->sem_value
== 0) {
83 int sem_post(sem_t
* sem
)
85 pthread_descr self
= thread_self();
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
) {
95 __pthread_unlock((struct _pthread_fastlock
*) &sem
->sem_lock
);
99 __pthread_unlock((struct _pthread_fastlock
*) &sem
->sem_lock
);
101 th
= dequeue(&sem
->sem_waiting
);
102 __pthread_unlock((struct _pthread_fastlock
*) &sem
->sem_lock
);
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) {
114 request
.req_kind
= REQ_POST
;
115 request
.req_args
.post
= sem
;
116 __libc_write(__pthread_manager_request
,
117 (char *) &request
, sizeof(request
));
122 int sem_getvalue(sem_t
* sem
, int * sval
)
124 *sval
= sem
->sem_value
;
128 int sem_destroy(sem_t
* sem
)
130 if (sem
->sem_waiting
!= NULL
) {