Fix mdoc(7)/man(7) mix up.
[netbsd-mini2440.git] / lib / libpthread / sem.c
blob57172e135af969e53e81547bbc3b78013b38b206
1 /* $NetBSD: sem.c,v 1.20 2008/04/28 20:23:02 martin Exp $ */
3 /*-
4 * Copyright (c) 2003, 2006, 2007 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of Wasabi Systems, Inc, and by Andrew Doran.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
33 * Copyright (C) 2000 Jason Evans <jasone@freebsd.org>.
34 * All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 * notice(s), this list of conditions and the following disclaimer as
41 * the first lines of this file unmodified other than the possible
42 * addition of one or more copyright notices.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice(s), this list of conditions and the following disclaimer in
45 * the documentation and/or other materials provided with the
46 * distribution.
48 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
49 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
51 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE
52 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
53 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
54 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
55 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
56 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
57 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
58 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
61 #include <sys/cdefs.h>
62 __RCSID("$NetBSD: sem.c,v 1.20 2008/04/28 20:23:02 martin Exp $");
64 #include <sys/types.h>
65 #include <sys/ksem.h>
66 #include <sys/queue.h>
67 #include <stdlib.h>
68 #include <errno.h>
69 #include <fcntl.h>
70 #include <semaphore.h>
71 #include <stdarg.h>
73 #include "pthread.h"
74 #include "pthread_int.h"
76 struct _sem_st {
77 unsigned int usem_magic;
78 #define USEM_MAGIC 0x09fa4012
80 LIST_ENTRY(_sem_st) usem_list;
81 intptr_t usem_semid; /* 0 -> user (non-shared) */
82 #define USEM_USER 0 /* assumes kernel does not use NULL */
83 sem_t *usem_identity;
85 /* Protects data below. */
86 pthread_mutex_t usem_interlock;
87 pthread_cond_t usem_cv;
88 unsigned int usem_count;
91 static int sem_alloc(unsigned int value, intptr_t semid, sem_t *semp);
92 static void sem_free(sem_t sem);
94 static LIST_HEAD(, _sem_st) named_sems = LIST_HEAD_INITIALIZER(&named_sems);
95 static pthread_mutex_t named_sems_mtx = PTHREAD_MUTEX_INITIALIZER;
97 static void
98 sem_free(sem_t sem)
101 if (sem->usem_semid == USEM_USER) {
102 pthread_cond_destroy(&sem->usem_cv);
103 pthread_mutex_destroy(&sem->usem_interlock);
105 sem->usem_magic = 0;
106 free(sem);
109 static int
110 sem_alloc(unsigned int value, intptr_t semid, sem_t *semp)
112 sem_t sem;
114 if (value > SEM_VALUE_MAX)
115 return (EINVAL);
117 if ((sem = malloc(sizeof(struct _sem_st))) == NULL)
118 return (ENOSPC);
120 sem->usem_magic = USEM_MAGIC;
121 pthread_mutex_init(&sem->usem_interlock, NULL);
122 pthread_cond_init(&sem->usem_cv, NULL);
123 sem->usem_count = value;
124 sem->usem_semid = semid;
126 *semp = sem;
127 return (0);
131 sem_init(sem_t *sem, int pshared, unsigned int value)
133 intptr_t semid;
134 int error;
136 semid = USEM_USER;
138 if (pshared && _ksem_init(value, &semid) == -1)
139 return (-1);
141 if ((error = sem_alloc(value, semid, sem)) != 0) {
142 if (semid != USEM_USER)
143 _ksem_destroy(semid);
144 errno = error;
145 return (-1);
148 return (0);
152 sem_destroy(sem_t *sem)
155 #ifdef ERRORCHECK
156 if (sem == NULL || *sem == NULL || (*sem)->usem_magic != USEM_MAGIC) {
157 errno = EINVAL;
158 return (-1);
160 #endif
162 if ((*sem)->usem_semid != USEM_USER) {
163 if (_ksem_destroy((*sem)->usem_semid))
164 return (-1);
165 } else {
166 pthread_mutex_lock(&(*sem)->usem_interlock);
167 if (!PTQ_EMPTY(&(*sem)->usem_cv.ptc_waiters)) {
168 pthread_mutex_unlock(&(*sem)->usem_interlock);
169 errno = EBUSY;
170 return (-1);
172 pthread_mutex_unlock(&(*sem)->usem_interlock);
175 sem_free(*sem);
177 return (0);
180 sem_t *
181 sem_open(const char *name, int oflag, ...)
183 sem_t *sem, s;
184 intptr_t semid;
185 mode_t mode;
186 unsigned int value;
187 int error;
188 va_list ap;
190 mode = 0;
191 value = 0;
193 if (oflag & O_CREAT) {
194 va_start(ap, oflag);
195 mode = va_arg(ap, int);
196 value = va_arg(ap, unsigned int);
197 va_end(ap);
201 * We can be lazy and let the kernel handle the oflag,
202 * we'll just merge duplicate IDs into our list.
204 if (_ksem_open(name, oflag, mode, value, &semid) == -1)
205 return (SEM_FAILED);
208 * Search for a duplicate ID, we must return the same sem_t *
209 * if we locate one.
211 pthread_mutex_lock(&named_sems_mtx);
212 LIST_FOREACH(s, &named_sems, usem_list) {
213 if (s->usem_semid == semid) {
214 pthread_mutex_unlock(&named_sems_mtx);
215 return (s->usem_identity);
219 if ((sem = malloc(sizeof(*sem))) == NULL) {
220 error = ENOSPC;
221 goto bad;
223 if ((error = sem_alloc(value, semid, sem)) != 0)
224 goto bad;
226 LIST_INSERT_HEAD(&named_sems, *sem, usem_list);
227 (*sem)->usem_identity = sem;
228 pthread_mutex_unlock(&named_sems_mtx);
230 return (sem);
232 bad:
233 pthread_mutex_unlock(&named_sems_mtx);
234 _ksem_close(semid);
235 if (sem != NULL) {
236 if (*sem != NULL)
237 sem_free(*sem);
238 free(sem);
240 errno = error;
241 return (SEM_FAILED);
245 sem_close(sem_t *sem)
248 #ifdef ERRORCHECK
249 if (sem == NULL || *sem == NULL || (*sem)->usem_magic != USEM_MAGIC) {
250 errno = EINVAL;
251 return (-1);
253 #endif
255 if ((*sem)->usem_semid == USEM_USER) {
256 errno = EINVAL;
257 return (-1);
260 pthread_mutex_lock(&named_sems_mtx);
261 if (_ksem_close((*sem)->usem_semid) == -1) {
262 pthread_mutex_unlock(&named_sems_mtx);
263 return (-1);
265 LIST_REMOVE((*sem), usem_list);
266 pthread_mutex_unlock(&named_sems_mtx);
267 sem_free(*sem);
268 free(sem);
269 return (0);
273 sem_unlink(const char *name)
276 return (_ksem_unlink(name));
280 sem_wait(sem_t *sem)
282 pthread_t self;
283 extern int pthread__started;
285 #ifdef ERRORCHECK
286 if (sem == NULL || *sem == NULL || (*sem)->usem_magic != USEM_MAGIC) {
287 errno = EINVAL;
288 return (-1);
290 #endif
292 self = pthread__self();
294 if ((*sem)->usem_semid != USEM_USER) {
295 pthread__testcancel(self);
296 return (_ksem_wait((*sem)->usem_semid));
299 if (pthread__started == 0) {
300 sigset_t set, oset;
302 sigfillset(&set);
303 (void) sigprocmask(SIG_SETMASK, &set, &oset);
304 for (;;) {
305 if ((*sem)->usem_count > 0) {
306 break;
308 (void) sigsuspend(&oset);
310 (*sem)->usem_count--;
311 (void) sigprocmask(SIG_SETMASK, &oset, NULL);
312 return 0;
315 pthread_mutex_lock(&(*sem)->usem_interlock);
316 for (;;) {
317 if (self->pt_cancel) {
318 pthread_mutex_unlock(&(*sem)->usem_interlock);
319 pthread__cancelled();
321 if ((*sem)->usem_count > 0)
322 break;
323 (void)pthread_cond_wait(&(*sem)->usem_cv,
324 &(*sem)->usem_interlock);
326 (*sem)->usem_count--;
327 pthread_mutex_unlock(&(*sem)->usem_interlock);
329 return (0);
333 sem_trywait(sem_t *sem)
335 extern int pthread__started;
337 #ifdef ERRORCHECK
338 if (sem == NULL || *sem == NULL || (*sem)->usem_magic != USEM_MAGIC) {
339 errno = EINVAL;
340 return (-1);
342 #endif
344 if ((*sem)->usem_semid != USEM_USER)
345 return (_ksem_trywait((*sem)->usem_semid));
347 if (pthread__started == 0) {
348 sigset_t set, oset;
349 int rv = 0;
351 sigfillset(&set);
352 (void) sigprocmask(SIG_SETMASK, &set, &oset);
353 if ((*sem)->usem_count > 0) {
354 (*sem)->usem_count--;
355 } else {
356 errno = EAGAIN;
357 rv = -1;
359 (void) sigprocmask(SIG_SETMASK, &oset, NULL);
360 return rv;
363 pthread_mutex_lock(&(*sem)->usem_interlock);
364 if ((*sem)->usem_count == 0) {
365 pthread_mutex_unlock(&(*sem)->usem_interlock);
366 errno = EAGAIN;
367 return (-1);
369 (*sem)->usem_count--;
370 pthread_mutex_unlock(&(*sem)->usem_interlock);
372 return (0);
376 sem_post(sem_t *sem)
379 #ifdef ERRORCHECK
380 if (sem == NULL || *sem == NULL || (*sem)->usem_magic != USEM_MAGIC) {
381 errno = EINVAL;
382 return (-1);
384 #endif
386 if ((*sem)->usem_semid != USEM_USER)
387 return (_ksem_post((*sem)->usem_semid));
389 pthread_mutex_lock(&(*sem)->usem_interlock);
390 (*sem)->usem_count++;
391 pthread_cond_signal(&(*sem)->usem_cv);
392 pthread_mutex_unlock(&(*sem)->usem_interlock);
394 return (0);
398 sem_getvalue(sem_t * __restrict sem, int * __restrict sval)
401 #ifdef ERRORCHECK
402 if (sem == NULL || *sem == NULL || (*sem)->usem_magic != USEM_MAGIC) {
403 errno = EINVAL;
404 return (-1);
406 #endif
407 if ((*sem)->usem_semid != USEM_USER)
408 return (_ksem_getvalue((*sem)->usem_semid, sval));
410 pthread_mutex_lock(&(*sem)->usem_interlock);
411 *sval = (int) (*sem)->usem_count;
412 pthread_mutex_unlock(&(*sem)->usem_interlock);
414 return (0);