2 * Copyright (C) 2000 Jason Evans <jasone@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 <semaphore.h>
41 #include <_semaphore.h>
42 #include "un-namespace.h"
43 #include "libc_private.h"
44 #include "thr_private.h"
46 __weak_reference(_sem_init, sem_init);
47 __weak_reference(_sem_wait, sem_wait);
48 __weak_reference(_sem_timedwait, sem_timedwait);
49 __weak_reference(_sem_post, sem_post);
53 sem_check_validity(sem_t *sem)
56 if ((sem != NULL) && ((*sem)->magic == SEM_MAGIC))
65 decrease_nwaiters(void *arg)
67 sem_t *sem = (sem_t *)arg;
71 * this function is called from cancellation point,
72 * the mutex should already be hold.
74 _pthread_mutex_unlock(&(*sem)->lock);
78 sem_alloc(unsigned int value, semid_t semid, int system_sem)
82 if (value > SEM_VALUE_MAX) {
87 sem = (sem_t)malloc(sizeof(struct sem));
94 * Initialize the semaphore.
96 if (_pthread_mutex_init(&sem->lock, NULL) != 0) {
102 if (_pthread_cond_init(&sem->gtzero, NULL) != 0) {
103 _pthread_mutex_destroy(&sem->lock);
109 sem->count = (u_int32_t)value;
111 sem->magic = SEM_MAGIC;
113 sem->syssem = system_sem;
118 _sem_init(sem_t *sem, int pshared, unsigned int value)
122 semid = (semid_t)SEM_USER;
123 if ((pshared != 0) && (ksem_init(&semid, value) != 0))
126 (*sem) = sem_alloc(value, semid, pshared);
127 if ((*sem) == NULL) {
136 _sem_wait(sem_t *sem)
138 struct pthread *curthread;
141 if (sem_check_validity(sem) != 0)
144 curthread = _get_curthread();
145 if ((*sem)->syssem != 0) {
146 _thr_cancel_enter(curthread);
147 retval = ksem_wait((*sem)->semid);
148 _thr_cancel_leave(curthread, retval != 0);
151 _pthread_testcancel();
152 _pthread_mutex_lock(&(*sem)->lock);
154 while ((*sem)->count <= 0) {
156 THR_CLEANUP_PUSH(curthread, decrease_nwaiters, sem);
157 _pthread_cond_wait(&(*sem)->gtzero, &(*sem)->lock);
158 THR_CLEANUP_POP(curthread, 0);
163 _pthread_mutex_unlock(&(*sem)->lock);
171 _sem_timedwait(sem_t * __restrict sem,
172 const struct timespec * __restrict abs_timeout)
174 struct pthread *curthread;
178 if (sem_check_validity(sem) != 0)
181 if ((*sem)->syssem != 0) {
182 curthread = _get_curthread();
183 _thr_cancel_enter(curthread);
184 retval = ksem_timedwait((*sem)->semid, abs_timeout);
185 _thr_cancel_leave(curthread, retval != 0);
189 * The timeout argument is only supposed to
190 * be checked if the thread would have blocked.
191 * This is checked outside of the lock so a
192 * segfault on an invalid address doesn't end
193 * up leaving the mutex locked.
195 _pthread_testcancel();
196 timeout_invalid = (abs_timeout->tv_nsec >= 1000000000) ||
197 (abs_timeout->tv_nsec < 0);
198 _pthread_mutex_lock(&(*sem)->lock);
200 if ((*sem)->count <= 0) {
201 if (timeout_invalid) {
202 _pthread_mutex_unlock(&(*sem)->lock);
207 _pthread_cleanup_push(decrease_nwaiters, sem);
208 _pthread_cond_timedwait(&(*sem)->gtzero,
209 &(*sem)->lock, abs_timeout);
210 _pthread_cleanup_pop(0);
213 if ((*sem)->count == 0) {
222 _pthread_mutex_unlock(&(*sem)->lock);
229 _sem_post(sem_t *sem)
231 struct pthread *curthread;
234 if (sem_check_validity(sem) != 0)
237 if ((*sem)->syssem != 0)
238 retval = ksem_post((*sem)->semid);
241 * sem_post() is required to be safe to call from within
242 * signal handlers. Thus, we must enter a critical region.
244 curthread = _get_curthread();
245 _thr_critical_enter(curthread);
246 _pthread_mutex_lock(&(*sem)->lock);
249 if ((*sem)->nwaiters > 0)
250 _pthread_cond_signal(&(*sem)->gtzero);
252 _pthread_mutex_unlock(&(*sem)->lock);
253 _thr_critical_leave(curthread);