2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (C) 2010 David Xu <davidxu@freebsd.org>.
5 * Copyright (C) 2000 Jason Evans <jasone@freebsd.org>.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice(s), this list of conditions and the following disclaimer as
13 * the first lines of this file unmodified other than the possible
14 * addition of one or more copyright notices.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice(s), this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
21 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
27 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
29 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 * Some notes about this implementation.
38 * This is mostly a simple implementation of POSIX semaphores that
39 * does not need threading. Any semaphore created is a kernel-based
40 * semaphore regardless of the pshared attribute. This is necessary
41 * because libc's stub for pthread_cond_wait() doesn't really wait,
42 * and it is not worth the effort impose this behavior on libc.
44 * All functions here are designed to be thread-safe so that a
45 * threads library need not provide wrappers except to make
46 * sem_wait() and sem_timedwait() cancellation points or to
47 * provide a faster userland implementation for non-pshared
50 * Also, this implementation of semaphores cannot really support
51 * real pshared semaphores. The sem_t is an allocated object
52 * and can't be seen by other processes when placed in shared
53 * memory. It should work across forks as long as the semaphore
54 * is created before any forks.
56 * The function sem_init() should be overridden by a threads
57 * library if it wants to provide a different userland version
58 * of semaphores. The functions sem_wait() and sem_timedwait()
59 * need to be wrapped to provide cancellation points. The function
60 * sem_post() may need to be wrapped to be signal-safe.
62 #include "namespace.h"
63 #include <sys/types.h>
64 #include <sys/queue.h>
65 #include <machine/atomic.h>
68 #include <sys/_semaphore.h>
75 #include "un-namespace.h"
76 #include "libc_private.h"
79 * Old semaphore definitions.
82 #define SEM_MAGIC ((u_int32_t) 0x09fa4012)
85 pthread_cond_t gtzero;
88 #define SEM_USER (NULL)
89 semid_t semid; /* semaphore id if kernel (shared) semaphore */
90 int syssem; /* 1 if kernel (shared) semaphore */
91 LIST_ENTRY(sem) entry;
92 struct sem **backpointer;
95 typedef struct sem* sem_t;
97 #define SEM_FAILED ((sem_t *)0)
98 #define SEM_VALUE_MAX __INT_MAX
100 #define SYM_FB10(sym) __CONCAT(sym, _fb10)
101 #define WEAK_REF(sym, alias) __weak_reference(sym, alias)
102 #define SYM_COMPAT(sym, impl, ver) __sym_compat(sym, impl, ver)
104 #define FB10_COMPAT(func, sym) \
105 WEAK_REF(func, SYM_FB10(sym)); \
106 SYM_COMPAT(sym, SYM_FB10(sym), FBSD_1.0)
108 static sem_t sem_alloc(unsigned int value, semid_t semid, int system_sem);
109 static void sem_free(sem_t sem);
111 static LIST_HEAD(, sem) named_sems = LIST_HEAD_INITIALIZER(named_sems);
112 static pthread_mutex_t named_sems_mtx = PTHREAD_MUTEX_INITIALIZER;
114 FB10_COMPAT(_libc_sem_init_compat, sem_init);
115 FB10_COMPAT(_libc_sem_destroy_compat, sem_destroy);
116 FB10_COMPAT(_libc_sem_open_compat, sem_open);
117 FB10_COMPAT(_libc_sem_close_compat, sem_close);
118 FB10_COMPAT(_libc_sem_unlink_compat, sem_unlink);
119 FB10_COMPAT(_libc_sem_wait_compat, sem_wait);
120 FB10_COMPAT(_libc_sem_trywait_compat, sem_trywait);
121 FB10_COMPAT(_libc_sem_timedwait_compat, sem_timedwait);
122 FB10_COMPAT(_libc_sem_post_compat, sem_post);
123 FB10_COMPAT(_libc_sem_getvalue_compat, sem_getvalue);
126 sem_check_validity(sem_t *sem)
129 if ((sem != NULL) && ((*sem)->magic == SEM_MAGIC))
146 sem_alloc(unsigned int value, semid_t semid, int system_sem)
150 if (value > SEM_VALUE_MAX) {
155 sem = (sem_t)malloc(sizeof(struct sem));
161 sem->count = (u_int32_t)value;
163 sem->magic = SEM_MAGIC;
165 sem->syssem = system_sem;
170 _libc_sem_init_compat(sem_t *sem, int pshared, unsigned int value)
175 * We always have to create the kernel semaphore if the
176 * threads library isn't present since libc's version of
177 * pthread_cond_wait() is just a stub that doesn't really
180 semid = (semid_t)SEM_USER;
181 if ((pshared != 0) && ksem_init(&semid, value) != 0)
184 *sem = sem_alloc(value, semid, pshared);
185 if ((*sem) == NULL) {
194 _libc_sem_destroy_compat(sem_t *sem)
198 if (sem_check_validity(sem) != 0)
202 * If this is a system semaphore let the kernel track it otherwise
203 * make sure there are no waiters.
205 if ((*sem)->syssem != 0)
206 retval = ksem_destroy((*sem)->semid);
207 else if ((*sem)->nwaiters > 0) {
222 _libc_sem_open_compat(const char *name, int oflag, ...)
233 if ((oflag & O_CREAT) != 0) {
237 mode = va_arg(ap, int);
238 value = va_arg(ap, unsigned int);
242 * we can be lazy and let the kernel handle the "oflag",
243 * we'll just merge duplicate IDs into our list.
245 if (ksem_open(&semid, name, oflag, mode, value) == -1)
248 * search for a duplicate ID, we must return the same sem_t *
251 _pthread_mutex_lock(&named_sems_mtx);
252 LIST_FOREACH(s, &named_sems, entry) {
253 if (s->semid == semid) {
254 sem = s->backpointer;
255 _pthread_mutex_unlock(&named_sems_mtx);
259 sem = (sem_t *)malloc(sizeof(*sem));
262 *sem = sem_alloc(value, semid, 1);
265 LIST_INSERT_HEAD(&named_sems, *sem, entry);
266 (*sem)->backpointer = sem;
267 _pthread_mutex_unlock(&named_sems_mtx);
270 _pthread_mutex_unlock(&named_sems_mtx);
285 _libc_sem_close_compat(sem_t *sem)
288 if (sem_check_validity(sem) != 0)
291 if ((*sem)->syssem == 0) {
296 _pthread_mutex_lock(&named_sems_mtx);
297 if (ksem_close((*sem)->semid) != 0) {
298 _pthread_mutex_unlock(&named_sems_mtx);
301 LIST_REMOVE((*sem), entry);
302 _pthread_mutex_unlock(&named_sems_mtx);
310 _libc_sem_unlink_compat(const char *name)
313 return (ksem_unlink(name));
317 _umtx_wait_uint(volatile unsigned *mtx, unsigned id, const struct timespec *abstime)
319 struct _umtx_time *tm_p, timeout;
322 if (abstime == NULL) {
326 timeout._clockid = CLOCK_REALTIME;
327 timeout._flags = UMTX_ABSTIME;
328 timeout._timeout = *abstime;
330 tm_size = sizeof(timeout);
332 return _umtx_op(__DEVOLATILE(void *, mtx),
333 UMTX_OP_WAIT_UINT_PRIVATE, id,
334 (void *)tm_size, __DECONST(void*, tm_p));
338 _umtx_wake(volatile void *mtx)
340 return _umtx_op(__DEVOLATILE(void *, mtx), UMTX_OP_WAKE_PRIVATE,
344 #define TIMESPEC_SUB(dst, src, val) \
346 (dst)->tv_sec = (src)->tv_sec - (val)->tv_sec; \
347 (dst)->tv_nsec = (src)->tv_nsec - (val)->tv_nsec; \
348 if ((dst)->tv_nsec < 0) { \
350 (dst)->tv_nsec += 1000000000; \
356 sem_cancel_handler(void *arg)
360 atomic_add_int(&(*sem)->nwaiters, -1);
361 if ((*sem)->nwaiters && (*sem)->count)
362 _umtx_wake(&(*sem)->count);
366 _libc_sem_timedwait_compat(sem_t * __restrict sem,
367 const struct timespec * __restrict abstime)
371 if (sem_check_validity(sem) != 0)
374 if ((*sem)->syssem != 0) {
375 _pthread_cancel_enter(1);
376 retval = ksem_wait((*sem)->semid); /* XXX no timeout */
377 _pthread_cancel_leave(retval == -1);
382 _pthread_testcancel();
384 while ((val = (*sem)->count) > 0) {
385 if (atomic_cmpset_acq_int(&(*sem)->count, val, val - 1))
389 _pthread_testcancel();
393 if (abstime->tv_nsec >= 1000000000 || abstime->tv_nsec < 0) {
398 atomic_add_int(&(*sem)->nwaiters, 1);
399 pthread_cleanup_push(sem_cancel_handler, sem);
400 _pthread_cancel_enter(1);
401 retval = _umtx_wait_uint(&(*sem)->count, 0, abstime);
402 _pthread_cancel_leave(0);
403 pthread_cleanup_pop(0);
404 atomic_add_int(&(*sem)->nwaiters, -1);
410 _libc_sem_wait_compat(sem_t *sem)
412 return _libc_sem_timedwait_compat(sem, NULL);
416 _libc_sem_trywait_compat(sem_t *sem)
420 if (sem_check_validity(sem) != 0)
423 if ((*sem)->syssem != 0)
424 return ksem_trywait((*sem)->semid);
426 while ((val = (*sem)->count) > 0) {
427 if (atomic_cmpset_acq_int(&(*sem)->count, val, val - 1))
435 _libc_sem_post_compat(sem_t *sem)
438 if (sem_check_validity(sem) != 0)
441 if ((*sem)->syssem != 0)
442 return ksem_post((*sem)->semid);
444 atomic_add_rel_int(&(*sem)->count, 1);
446 if ((*sem)->nwaiters)
447 return _umtx_wake(&(*sem)->count);
452 _libc_sem_getvalue_compat(sem_t * __restrict sem, int * __restrict sval)
456 if (sem_check_validity(sem) != 0)
459 if ((*sem)->syssem != 0)
460 retval = ksem_getvalue((*sem)->semid, sval);
462 *sval = (int)(*sem)->count;