xref: /netbsd-src/lib/librt/sem.c (revision 001c68bd94f75ce9270b69227c4199fbf34ee396)
1 /*	$NetBSD: sem.c,v 1.2 2003/03/09 00:44:43 lukem Exp $	*/
2 
3 /*-
4  * Copyright (c) 2003 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jason R. Thorpe.
9  *
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.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *        This product includes software developed by the NetBSD
21  *        Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 /*
40  * Copyright (C) 2000 Jason Evans <jasone@freebsd.org>.
41  * All rights reserved.
42  *
43  * Redistribution and use in source and binary forms, with or without
44  * modification, are permitted provided that the following conditions
45  * are met:
46  * 1. Redistributions of source code must retain the above copyright
47  *    notice(s), this list of conditions and the following disclaimer as
48  *    the first lines of this file unmodified other than the possible
49  *    addition of one or more copyright notices.
50  * 2. Redistributions in binary form must reproduce the above copyright
51  *    notice(s), this list of conditions and the following disclaimer in
52  *    the documentation and/or other materials provided with the
53  *    distribution.
54  *
55  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
56  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
58  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE
59  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
60  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
61  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
62  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
63  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
64  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
65  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
66  */
67 
68 #include <sys/cdefs.h>
69 __RCSID("$NetBSD: sem.c,v 1.2 2003/03/09 00:44:43 lukem Exp $");
70 
71 /*
72  * If an application is linked against both librt and libpthread, the
73  * libpthread versions must be used.  Provide weak aliases to cause
74  * this behavior.
75  */
76 #define	sem_init	_librt_sem_init
77 #define	sem_destroy	_librt_sem_destroy
78 #define	sem_open	_librt_sem_open
79 #define	sem_close	_librt_sem_close
80 #define	sem_unlink	_librt_sem_unlink
81 #define	sem_wait	_librt_sem_wait
82 #define	sem_trywait	_librt_sem_trywait
83 #define	sem_post	_librt_sem_post
84 #define	sem_getvalue	_librt_sem_getvalue
85 
86 #define	_LIBC		/* XXX to get semid_t type */
87 
88 #include <sys/types.h>
89 #include <sys/ksem.h>
90 #include <sys/queue.h>
91 #include <stdlib.h>
92 #include <errno.h>
93 #include <fcntl.h>
94 #include <semaphore.h>
95 #include <stdarg.h>
96 
97 struct _sem_st {
98 	unsigned int	ksem_magic;
99 #define	KSEM_MAGIC	0x90af0421U
100 
101 	LIST_ENTRY(_sem_st) ksem_list;
102 	semid_t		ksem_semid;	/* 0 -> user (non-shared) */
103 	sem_t		*ksem_identity;
104 };
105 
106 static int sem_alloc(unsigned int value, semid_t semid, sem_t *semp);
107 static void sem_free(sem_t sem);
108 
109 static LIST_HEAD(, _sem_st) named_sems = LIST_HEAD_INITIALIZER(&named_sems);
110 
111 __weak_alias(sem_init,_librt_sem_init)
112 __weak_alias(sem_destroy,_librt_sem_destroy)
113 __weak_alias(sem_open,_librt_sem_open)
114 __weak_alias(sem_close,_librt_sem_close)
115 __weak_alias(sem_unlink,_librt_sem_unlink)
116 __weak_alias(sem_wait,_librt_sem_wait)
117 __weak_alias(sem_trywait,_librt_sem_trywait)
118 __weak_alias(sem_post,_librt_sem_post)
119 __weak_alias(sem_getvalue,_librt_sem_getvalue)
120 
121 static void
122 sem_free(sem_t sem)
123 {
124 
125 	sem->ksem_magic = 0;
126 	free(sem);
127 }
128 
129 static int
130 sem_alloc(unsigned int value, semid_t semid, sem_t *semp)
131 {
132 	sem_t sem;
133 
134 	if (value > SEM_VALUE_MAX)
135 		return (EINVAL);
136 
137 	if ((sem = malloc(sizeof(struct _sem_st))) == NULL)
138 		return (ENOSPC);
139 
140 	sem->ksem_magic = KSEM_MAGIC;
141 	sem->ksem_semid = semid;
142 
143 	*semp = sem;
144 	return (0);
145 }
146 
147 /* ARGSUSED */
148 int
149 sem_init(sem_t *sem, int pshared, unsigned int value)
150 {
151 	semid_t	semid;
152 	int error;
153 
154 	if (_ksem_init(value, &semid) == -1)
155 		return (-1);
156 
157 	if ((error = sem_alloc(value, semid, sem)) != 0) {
158 		_ksem_destroy(semid);
159 		errno = error;
160 		return (-1);
161 	}
162 
163 	return (0);
164 }
165 
166 int
167 sem_destroy(sem_t *sem)
168 {
169 
170 #ifdef ERRORCHECK
171 	if (sem == NULL || *sem == NULL || (*sem)->ksem_magic != KSEM_MAGIC) {
172 		errno = EINVAL;
173 		return (-1);
174 	}
175 #endif
176 
177 	if (_ksem_destroy((*sem)->ksem_semid) == -1)
178 		return (-1);
179 
180 	sem_free(*sem);
181 
182 	return (0);
183 }
184 
185 sem_t *
186 sem_open(const char *name, int oflag, ...)
187 {
188 	sem_t *sem, s;
189 	semid_t semid;
190 	mode_t mode;
191 	unsigned int value;
192 	int error;
193 	va_list ap;
194 
195 	mode = 0;
196 	value = 0;
197 
198 	if (oflag & O_CREAT) {
199 		va_start(ap, oflag);
200 		mode = va_arg(ap, int);
201 		value = va_arg(ap, unsigned int);
202 		va_end(ap);
203 	}
204 
205 	/*
206 	 * We can be lazy and let the kernel handle the oflag,
207 	 * we'll just merge duplicate IDs into our list.
208 	 */
209 	if (_ksem_open(name, oflag, mode, value, &semid) == -1)
210 		return (SEM_FAILED);
211 
212 	/*
213 	 * Search for a duplicate ID, we must return the same sem_t *
214 	 * if we locate one.
215 	 */
216 	LIST_FOREACH(s, &named_sems, ksem_list) {
217 		if (s->ksem_semid == semid)
218 			return (s->ksem_identity);
219 	}
220 
221 	if ((sem = malloc(sizeof(*sem))) == NULL) {
222 		error = ENOSPC;
223 		goto bad;
224 	}
225 	if ((error = sem_alloc(value, semid, sem)) != 0)
226 		goto bad;
227 
228 	LIST_INSERT_HEAD(&named_sems, *sem, ksem_list);
229 	(*sem)->ksem_identity = sem;
230 
231 	return (sem);
232 
233  bad:
234 	_ksem_close(semid);
235 	if (sem != NULL) {
236 		if (*sem != NULL)
237 			sem_free(*sem);
238 		free(sem);
239 	}
240 	errno = error;
241 	return (SEM_FAILED);
242 }
243 
244 int
245 sem_close(sem_t *sem)
246 {
247 
248 #ifdef ERRORCHECK
249 	if (sem == NULL || *sem == NULL || (*sem)->ksem_magic != KSEM_MAGIC) {
250 		errno = EINVAL;
251 		return (-1);
252 	}
253 #endif
254 
255 	if (_ksem_close((*sem)->ksem_semid) == -1)
256 		return (-1);
257 
258 	LIST_REMOVE((*sem), ksem_list);
259 	sem_free(*sem);
260 	free(sem);
261 	return (0);
262 }
263 
264 int
265 sem_unlink(const char *name)
266 {
267 
268 	return (_ksem_unlink(name));
269 }
270 
271 int
272 sem_wait(sem_t *sem)
273 {
274 
275 #ifdef ERRORCHECK
276 	if (sem == NULL || *sem == NULL || (*sem)->ksem_magic != KSEM_MAGIC) {
277 		errno = EINVAL;
278 		return (-1);
279 	}
280 #endif
281 
282 	return (_ksem_wait((*sem)->ksem_semid));
283 }
284 
285 int
286 sem_trywait(sem_t *sem)
287 {
288 
289 #ifdef ERRORCHECK
290 	if (sem == NULL || *sem == NULL || (*sem)->ksem_magic != KSEM_MAGIC) {
291 		errno = EINVAL;
292 		return (-1);
293 	}
294 #endif
295 
296 	return (_ksem_trywait((*sem)->ksem_semid));
297 }
298 
299 int
300 sem_post(sem_t *sem)
301 {
302 
303 #ifdef ERRORCHECK
304 	if (sem == NULL || *sem == NULL || (*sem)->ksem_magic != KSEM_MAGIC) {
305 		errno = EINVAL;
306 		return (-1);
307 	}
308 #endif
309 
310 	return (_ksem_post((*sem)->ksem_semid));
311 }
312 
313 int
314 sem_getvalue(sem_t * __restrict sem, int * __restrict sval)
315 {
316 
317 #ifdef ERRORCHECK
318 	if (sem == NULL || *sem == NULL || (*sem)->ksem_magic != KSEM_MAGIC) {
319 		errno = EINVAL;
320 		return (-1);
321 	}
322 #endif
323 	return (_ksem_getvalue((*sem)->ksem_semid, sval));
324 }
325