2 * Copyright (C) 2010 David Xu <davidxu@freebsd.org>.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice(s), this list of conditions and the following disclaimer as
10 * the first lines of this file unmodified other than the possible
11 * addition of one or more copyright notices.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice(s), this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE
21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
27 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include "namespace.h"
33 #include <sys/types.h>
34 #include <sys/queue.h>
38 #include <machine/atomic.h>
48 #include <semaphore.h>
50 #include "un-namespace.h"
51 #include "libc_private.h"
53 __weak_reference(_sem_close, sem_close);
54 __weak_reference(_sem_destroy, sem_destroy);
55 __weak_reference(_sem_getvalue, sem_getvalue);
56 __weak_reference(_sem_init, sem_init);
57 __weak_reference(_sem_open, sem_open);
58 __weak_reference(_sem_post, sem_post);
59 __weak_reference(_sem_timedwait, sem_timedwait);
60 __weak_reference(_sem_clockwait_np, sem_clockwait_np);
61 __weak_reference(_sem_trywait, sem_trywait);
62 __weak_reference(_sem_unlink, sem_unlink);
63 __weak_reference(_sem_wait, sem_wait);
65 #define SEM_PREFIX "/tmp/SEMD"
66 #define SEM_MAGIC ((u_int32_t)0x73656d32)
68 _Static_assert(SEM_VALUE_MAX <= USEM_MAX_COUNT, "SEM_VALUE_MAX too large");
76 LIST_ENTRY(sem_nameinfo) next;
79 static pthread_once_t once = PTHREAD_ONCE_INIT;
80 static pthread_mutex_t sem_llock;
81 static LIST_HEAD(, sem_nameinfo) sem_list = LIST_HEAD_INITIALIZER(sem_list);
87 _pthread_mutex_lock(&sem_llock);
94 _pthread_mutex_unlock(&sem_llock);
98 sem_child_postfork(void)
101 _pthread_mutex_unlock(&sem_llock);
105 sem_module_init(void)
108 _pthread_mutex_init(&sem_llock, NULL);
109 _pthread_atfork(sem_prefork, sem_postfork, sem_child_postfork);
113 sem_check_validity(sem_t *sem)
116 if (sem->_magic == SEM_MAGIC)
123 _sem_init(sem_t *sem, int pshared, unsigned int value)
126 if (value > SEM_VALUE_MAX) {
131 bzero(sem, sizeof(sem_t));
132 sem->_magic = SEM_MAGIC;
133 sem->_kern._count = (u_int32_t)value;
134 sem->_kern._flags = pshared ? USYNC_PROCESS_SHARED : 0;
139 _sem_open(const char *name, int flags, ...)
144 struct sem_nameinfo *ni;
146 int errsave, fd, len, mode, value;
153 if (name[0] != '/') {
158 strcpy(path, SEM_PREFIX);
159 if (strlcat(path, name, sizeof(path)) >= sizeof(path)) {
160 errno = ENAMETOOLONG;
163 if (flags & ~(O_CREAT|O_EXCL)) {
167 if ((flags & O_CREAT) != 0) {
169 mode = va_arg(ap, int);
170 value = va_arg(ap, int);
174 _pthread_once(&once, sem_module_init);
176 _pthread_mutex_lock(&sem_llock);
177 LIST_FOREACH(ni, &sem_list, next) {
178 if (ni->name != NULL && strcmp(name, ni->name) == 0) {
179 fd = _open(path, flags | O_RDWR | O_CLOEXEC |
181 if (fd == -1 || _fstat(fd, &sb) == -1) {
185 if ((flags & (O_CREAT | O_EXCL)) == (O_CREAT |
186 O_EXCL) || ni->dev != sb.st_dev ||
187 ni->ino != sb.st_ino) {
194 _pthread_mutex_unlock(&sem_llock);
200 len = sizeof(*ni) + strlen(name) + 1;
201 ni = (struct sem_nameinfo *)malloc(len);
207 ni->name = (char *)(ni+1);
208 strcpy(ni->name, name);
211 fd = _open(path, flags | O_RDWR | O_CLOEXEC | O_EXLOCK, mode);
212 if (fd == -1 || _fstat(fd, &sb) == -1)
215 if (sb.st_size < sizeof(sem_t)) {
216 tmp._magic = SEM_MAGIC;
217 tmp._kern._count = value;
218 tmp._kern._flags = USYNC_PROCESS_SHARED | SEM_NAMED;
219 if (_write(fd, &tmp, sizeof(tmp)) != sizeof(tmp))
223 sem = mmap(NULL, sizeof(sem_t), PROT_READ | PROT_WRITE,
224 MAP_SHARED | MAP_NOSYNC, fd, 0);
225 if (sem == MAP_FAILED) {
231 if (sem->_magic != SEM_MAGIC) {
239 LIST_INSERT_HEAD(&sem_list, ni, next);
241 _pthread_mutex_unlock(&sem_llock);
249 munmap(sem, sizeof(sem_t));
251 _pthread_mutex_unlock(&sem_llock);
257 _sem_close(sem_t *sem)
259 struct sem_nameinfo *ni;
262 if (sem_check_validity(sem) != 0)
265 if (!(sem->_kern._flags & SEM_NAMED)) {
270 _pthread_once(&once, sem_module_init);
272 _pthread_mutex_lock(&sem_llock);
273 LIST_FOREACH(ni, &sem_list, next) {
274 if (sem == ni->sem) {
275 last = --ni->open_count == 0;
277 LIST_REMOVE(ni, next);
278 _pthread_mutex_unlock(&sem_llock);
280 munmap(sem, sizeof(*sem));
286 _pthread_mutex_unlock(&sem_llock);
292 _sem_unlink(const char *name)
296 if (name[0] != '/') {
301 strcpy(path, SEM_PREFIX);
302 if (strlcat(path, name, sizeof(path)) >= sizeof(path)) {
303 errno = ENAMETOOLONG;
307 return (unlink(path));
311 _sem_destroy(sem_t *sem)
314 if (sem_check_validity(sem) != 0)
317 if (sem->_kern._flags & SEM_NAMED) {
326 _sem_getvalue(sem_t * __restrict sem, int * __restrict sval)
329 if (sem_check_validity(sem) != 0)
332 *sval = (int)USEM_COUNT(sem->_kern._count);
337 usem_wake(struct _usem2 *sem)
340 return (_umtx_op(sem, UMTX_OP_SEM2_WAKE, 0, NULL, NULL));
344 usem_wait(struct _usem2 *sem, clockid_t clock_id, int flags,
345 const struct timespec *rqtp, struct timespec *rmtp)
348 struct _umtx_time timeout;
349 struct timespec remain;
359 tms.timeout._clockid = clock_id;
360 tms.timeout._flags = (flags & TIMER_ABSTIME) ? UMTX_ABSTIME : 0;
361 tms.timeout._timeout = *rqtp;
363 tm_size = sizeof(tms);
365 retval = _umtx_op(sem, UMTX_OP_SEM2_WAIT, 0, (void *)tm_size, tm_p);
366 if (retval == -1 && errno == EINTR && (flags & TIMER_ABSTIME) == 0 &&
367 rqtp != NULL && rmtp != NULL) {
375 _sem_trywait(sem_t *sem)
379 if (sem_check_validity(sem) != 0)
382 while (USEM_COUNT(val = sem->_kern._count) > 0) {
383 if (atomic_cmpset_acq_int(&sem->_kern._count, val, val - 1))
391 _sem_clockwait_np(sem_t * __restrict sem, clockid_t clock_id, int flags,
392 const struct timespec *rqtp, struct timespec *rmtp)
396 if (sem_check_validity(sem) != 0)
400 _pthread_testcancel();
402 while (USEM_COUNT(val = sem->_kern._count) > 0) {
403 if (atomic_cmpset_acq_int(&sem->_kern._count, val,
409 _pthread_testcancel();
414 * The timeout argument is only supposed to
415 * be checked if the thread would have blocked.
418 if (rqtp->tv_nsec >= 1000000000 || rqtp->tv_nsec < 0) {
423 _pthread_cancel_enter(1);
424 retval = usem_wait(&sem->_kern, clock_id, flags, rqtp, rmtp);
425 _pthread_cancel_leave(0);
431 _sem_timedwait(sem_t * __restrict sem,
432 const struct timespec * __restrict abstime)
435 return (_sem_clockwait_np(sem, CLOCK_REALTIME, TIMER_ABSTIME, abstime,
440 _sem_wait(sem_t *sem)
443 return (_sem_timedwait(sem, NULL));
448 * The sem_post() interface is reentrant with respect to signals and may be
449 * invoked from a signal-catching function.
450 * The implementation does not use lock, so it should be safe.
453 _sem_post(sem_t *sem)
457 if (sem_check_validity(sem) != 0)
461 count = sem->_kern._count;
462 if (USEM_COUNT(count) + 1 > SEM_VALUE_MAX) {
466 } while (!atomic_cmpset_rel_int(&sem->_kern._count, count, count + 1));
467 if (count & USEM_HAS_WAITERS)
468 usem_wake(&sem->_kern);