2 * Copyright (C) 2005 David Xu <davidxu@freebsd.org>.
3 * Copyright (C) 2000 Jason Evans <jasone@freebsd.org>.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice(s), this list of conditions and the following disclaimer as
11 * the first lines of this file unmodified other than the possible
12 * addition of one or more copyright notices.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice(s), this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
19 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
25 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
27 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include "namespace.h"
34 #include <sys/types.h>
35 #include <sys/queue.h>
39 #include <semaphore.h>
42 #include <_semaphore.h>
43 #include "un-namespace.h"
45 #include "thr_private.h"
48 __weak_reference(_sem_init, sem_init);
49 __weak_reference(_sem_destroy, sem_destroy);
50 __weak_reference(_sem_getvalue, sem_getvalue);
51 __weak_reference(_sem_trywait, sem_trywait);
52 __weak_reference(_sem_wait, sem_wait);
53 __weak_reference(_sem_timedwait, sem_timedwait);
54 __weak_reference(_sem_post, sem_post);
58 sem_check_validity(sem_t *sem)
61 if ((sem != NULL) && ((*sem)->magic == SEM_MAGIC))
70 sem_alloc(unsigned int value, semid_t semid, int system_sem)
74 if (value > SEM_VALUE_MAX) {
79 sem = (sem_t)malloc(sizeof(struct sem));
84 bzero(sem, sizeof(*sem));
86 * Fortunatly count and nwaiters are adjacency, so we can
87 * use umtx_wait to wait on it, umtx_wait needs an address
88 * can be accessed as a long interger.
90 sem->count = (u_int32_t)value;
92 sem->magic = SEM_MAGIC;
94 sem->syssem = system_sem;
99 _sem_init(sem_t *sem, int pshared, unsigned int value)
103 semid = (semid_t)SEM_USER;
104 if ((pshared != 0) && (ksem_init(&semid, value) != 0))
107 (*sem) = sem_alloc(value, semid, pshared);
108 if ((*sem) == NULL) {
117 _sem_destroy(sem_t *sem)
121 if (sem_check_validity(sem) != 0)
125 * If this is a system semaphore let the kernel track it otherwise
126 * make sure there are no waiters.
128 if ((*sem)->syssem != 0)
129 retval = ksem_destroy((*sem)->semid);
140 _sem_getvalue(sem_t * __restrict sem, int * __restrict sval)
144 if (sem_check_validity(sem) != 0)
147 if ((*sem)->syssem != 0)
148 retval = ksem_getvalue((*sem)->semid, sval);
150 *sval = (int)(*sem)->count;
157 _sem_trywait(sem_t *sem)
161 if (sem_check_validity(sem) != 0)
164 if ((*sem)->syssem != 0)
165 return (ksem_trywait((*sem)->semid));
167 while ((val = (*sem)->count) > 0) {
168 if (atomic_cmpset_acq_int(&(*sem)->count, val, val - 1))
176 sem_cancel_handler(void *arg)
180 atomic_add_int(&(*sem)->nwaiters, -1);
184 _sem_wait(sem_t *sem)
186 struct pthread *curthread;
189 if (sem_check_validity(sem) != 0)
192 curthread = _get_curthread();
193 if ((*sem)->syssem != 0) {
194 _thr_cancel_enter(curthread);
195 retval = ksem_wait((*sem)->semid);
196 _thr_cancel_leave(curthread);
200 _pthread_testcancel();
202 while ((val = (*sem)->count) > 0) {
203 if (atomic_cmpset_acq_int(&(*sem)->count, val, val - 1))
206 atomic_add_int(&(*sem)->nwaiters, 1);
207 THR_CLEANUP_PUSH(curthread, sem_cancel_handler, sem);
208 _thr_cancel_enter(curthread);
209 retval = _thr_umtx_wait_uint(&(*sem)->count, 0, NULL);
210 _thr_cancel_leave(curthread);
211 THR_CLEANUP_POP(curthread, 0);
212 atomic_add_int(&(*sem)->nwaiters, -1);
213 } while (retval == 0);
219 _sem_timedwait(sem_t * __restrict sem,
220 const struct timespec * __restrict abstime)
222 struct timespec ts, ts2;
223 struct pthread *curthread;
226 if (sem_check_validity(sem) != 0)
229 curthread = _get_curthread();
230 if ((*sem)->syssem != 0) {
231 _thr_cancel_enter(curthread);
232 retval = ksem_timedwait((*sem)->semid, abstime);
233 _thr_cancel_leave(curthread);
238 * The timeout argument is only supposed to
239 * be checked if the thread would have blocked.
241 _pthread_testcancel();
243 while ((val = (*sem)->count) > 0) {
244 if (atomic_cmpset_acq_int(&(*sem)->count, val, val - 1))
247 if (abstime == NULL) {
251 clock_gettime(CLOCK_REALTIME, &ts);
252 TIMESPEC_SUB(&ts2, abstime, &ts);
253 atomic_add_int(&(*sem)->nwaiters, 1);
254 THR_CLEANUP_PUSH(curthread, sem_cancel_handler, sem);
255 _thr_cancel_enter(curthread);
256 retval = _thr_umtx_wait_uint(&(*sem)->count, 0, &ts2);
257 _thr_cancel_leave(curthread);
258 THR_CLEANUP_POP(curthread, 0);
259 atomic_add_int(&(*sem)->nwaiters, -1);
260 } while (retval == 0);
266 * sem_post() is required to be safe to call from within
267 * signal handlers, these code should work as that.
271 _sem_post(sem_t *sem)
275 if (sem_check_validity(sem) != 0)
278 if ((*sem)->syssem != 0)
279 return (ksem_post((*sem)->semid));
281 atomic_add_rel_int(&(*sem)->count, 1);
283 if ((*sem)->nwaiters) {
284 retval = _thr_umtx_wake(&(*sem)->count, 1);