blob: 392c37bfa9db54e0fafed8288d4b55737f3ee4a4 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/* 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. */
14
15/* Semaphores a la POSIX 1003.1b */
16
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 <not-cancel.h>
25
26int sem_init(sem_t *sem, int pshared, unsigned int value)
27{
28 if (value > SEM_VALUE_MAX) {
29 errno = EINVAL;
30 return -1;
31 }
32 if (pshared) {
33 errno = ENOSYS;
34 return -1;
35 }
36 __pthread_init_lock(&sem->__sem_lock);
37 sem->__sem_value = value;
38 sem->__sem_waiting = NULL;
39 return 0;
40}
41
42/* Function called by pthread_cancel to remove the thread from
43 waiting inside sem_wait. */
44
45static int new_sem_extricate_func(void *obj, pthread_descr th)
46{
47 __volatile__ pthread_descr self = thread_self();
48 sem_t *sem = obj;
49 int did_remove = 0;
50
51 __pthread_lock(&sem->__sem_lock, self);
52 did_remove = remove_from_queue(&sem->__sem_waiting, th);
53 __pthread_unlock(&sem->__sem_lock);
54
55 return did_remove;
56}
57
58int sem_wait(sem_t * sem)
59{
60 __volatile__ pthread_descr self = thread_self();
61 pthread_extricate_if extr;
62 int already_canceled = 0;
63 int spurious_wakeup_count;
64
65 /* Set up extrication interface */
66 extr.pu_object = sem;
67 extr.pu_extricate_func = new_sem_extricate_func;
68
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;
74 }
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);
85
86 if (already_canceled) {
87 __pthread_set_own_extricate_if(self, 0);
88 __pthread_do_exit(PTHREAD_CANCELED, CURRENT_STACK_FRAME);
89 }
90
91 /* Wait for sem_post or cancellation, or fall through if already canceled */
92 spurious_wakeup_count = 0;
93 while (1)
94 {
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))
99 {
100 /* Count resumes that don't belong to us. */
101 spurious_wakeup_count++;
102 continue;
103 }
104 break;
105 }
106 __pthread_set_own_extricate_if(self, 0);
107
108 /* Terminate only if the wakeup came from cancellation. */
109 /* Otherwise ignore cancellation because we got the semaphore. */
110
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);
115 }
116 /* We got the semaphore */
117 return 0;
118}
119
120int sem_trywait(sem_t * sem)
121{
122 int retval;
123
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;
131 }
132 __pthread_unlock(&sem->__sem_lock);
133 return retval;
134}
135
136int sem_post(sem_t * sem)
137{
138 pthread_descr self = thread_self();
139 pthread_descr th;
140 struct pthread_request request;
141
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;
150 }
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);
159 }
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;
167 }
168 }
169 request.req_kind = REQ_POST;
170 request.req_args.post = sem;
171 TEMP_FAILURE_RETRY(write_not_cancel(__pthread_manager_request,
172 (char *) &request, sizeof(request)));
173 }
174 return 0;
175}
176
177int sem_getvalue(sem_t * sem, int * sval)
178{
179 *sval = sem->__sem_value;
180 return 0;
181}
182
183int sem_destroy(sem_t * sem)
184{
185 if (sem->__sem_waiting != NULL) {
186 __set_errno (EBUSY);
187 return -1;
188 }
189 return 0;
190}
191
192sem_t *sem_open(const char *name, int oflag, ...)
193{
194 __set_errno (ENOSYS);
195 return SEM_FAILED;
196}
197
198int sem_close(sem_t *sem)
199{
200 __set_errno (ENOSYS);
201 return -1;
202}
203
204int sem_unlink(const char *name)
205{
206 __set_errno (ENOSYS);
207 return -1;
208}
209
210int sem_timedwait(sem_t *sem, const struct timespec *abstime)
211{
212 pthread_descr self = thread_self();
213 pthread_extricate_if extr;
214 int already_canceled = 0;
215 int spurious_wakeup_count;
216
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;
222 }
223
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 __set_errno (EINVAL);
229 return -1;
230 }
231
232 /* Set up extrication interface */
233 extr.pu_object = sem;
234 extr.pu_extricate_func = new_sem_extricate_func;
235
236 /* Register extrication interface */
237 THREAD_SETMEM(self, p_sem_avail, 0);
238 __pthread_set_own_extricate_if(self, &extr);
239 /* Enqueue only if not already cancelled. */
240 if (!(THREAD_GETMEM(self, p_canceled)
241 && THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE))
242 enqueue(&sem->__sem_waiting, self);
243 else
244 already_canceled = 1;
245 __pthread_unlock(&sem->__sem_lock);
246
247 if (already_canceled) {
248 __pthread_set_own_extricate_if(self, 0);
249 __pthread_do_exit(PTHREAD_CANCELED, CURRENT_STACK_FRAME);
250 }
251
252 spurious_wakeup_count = 0;
253 while (1)
254 {
255 if (timedsuspend(self, abstime) == 0) {
256 int was_on_queue;
257
258 /* __pthread_lock will queue back any spurious restarts that
259 may happen to it. */
260
261 __pthread_lock(&sem->__sem_lock, self);
262 was_on_queue = remove_from_queue(&sem->__sem_waiting, self);
263 __pthread_unlock(&sem->__sem_lock);
264
265 if (was_on_queue) {
266 __pthread_set_own_extricate_if(self, 0);
267 __set_errno (ETIMEDOUT);
268 return -1;
269 }
270
271 /* Eat the outstanding restart() from the signaller */
272 suspend(self);
273 }
274
275 if (THREAD_GETMEM(self, p_sem_avail) == 0
276 && (THREAD_GETMEM(self, p_woken_by_cancel) == 0
277 || THREAD_GETMEM(self, p_cancelstate) != PTHREAD_CANCEL_ENABLE))
278 {
279 /* Count resumes that don't belong to us. */
280 spurious_wakeup_count++;
281 continue;
282 }
283 break;
284 }
285
286 __pthread_set_own_extricate_if(self, 0);
287
288 /* Terminate only if the wakeup came from cancellation. */
289 /* Otherwise ignore cancellation because we got the semaphore. */
290
291 if (THREAD_GETMEM(self, p_woken_by_cancel)
292 && THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE) {
293 THREAD_SETMEM(self, p_woken_by_cancel, 0);
294 __pthread_do_exit(PTHREAD_CANCELED, CURRENT_STACK_FRAME);
295 }
296 /* We got the semaphore */
297 return 0;
298}