1 /*
2 * Copyright (c) 2006-2008 Amit Singh/Google Inc.
3 * Copyright (c) 2012 Anatol Pomozov
4 * Copyright (c) 2011-2013 Benjamin Fleischer
5 */
6
7 #include "darwin_compat.h"
8
9 #include <assert.h>
10 #include <errno.h>
11 #include <sys/types.h>
12
13 /*
14 * Semaphore implementation based on:
15 *
16 * Copyright (C) 2000,02 Free Software Foundation, Inc.
17 * This file is part of the GNU C Library.
18 * Written by Ga<EB>l Le Mignot <address@hidden>
19 *
20 * The GNU C Library is free software; you can redistribute it and/or
21 * modify it under the terms of the GNU Library General Public License as
22 * published by the Free Software Foundation; either version 2 of the
23 * License, or (at your option) any later version.
24 *
25 * The GNU C Library is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
28 * Library General Public License for more details.
29 *
30 * You should have received a copy of the GNU Library General Public
31 * License along with the GNU C Library; see the file COPYING.LIB. If not,
32 * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
33 * Boston, MA 02111-1307, USA.
34 */
35
36 /* Semaphores */
37
38 #define __SEM_ID_NONE ((int)0x0)
39 #define __SEM_ID_LOCAL ((int)0xcafef00d)
40
41 /* http://www.opengroup.org/onlinepubs/007908799/xsh/sem_init.html */
42 int
darwin_sem_init(darwin_sem_t * sem,int pshared,unsigned int value)43 darwin_sem_init(darwin_sem_t *sem, int pshared, unsigned int value)
44 {
45 if (pshared) {
46 errno = ENOSYS;
47 return -1;
48 }
49
50 sem->id = __SEM_ID_NONE;
51
52 if (pthread_cond_init(&sem->__data.local.count_cond, NULL)) {
53 goto cond_init_fail;
54 }
55
56 if (pthread_mutex_init(&sem->__data.local.count_lock, NULL)) {
57 goto mutex_init_fail;
58 }
59
60 sem->__data.local.count = value;
61 sem->id = __SEM_ID_LOCAL;
62
63 return 0;
64
65 mutex_init_fail:
66
67 pthread_cond_destroy(&sem->__data.local.count_cond);
68
69 cond_init_fail:
70
71 return -1;
72 }
73
74 /* http://www.opengroup.org/onlinepubs/007908799/xsh/sem_destroy.html */
75 int
darwin_sem_destroy(darwin_sem_t * sem)76 darwin_sem_destroy(darwin_sem_t *sem)
77 {
78 int res = 0;
79
80 pthread_mutex_lock(&sem->__data.local.count_lock);
81
82 sem->id = __SEM_ID_NONE;
83 pthread_cond_broadcast(&sem->__data.local.count_cond);
84
85 if (pthread_cond_destroy(&sem->__data.local.count_cond)) {
86 res = -1;
87 }
88
89 pthread_mutex_unlock(&sem->__data.local.count_lock);
90
91 if (pthread_mutex_destroy(&sem->__data.local.count_lock)) {
92 res = -1;
93 }
94
95 return res;
96 }
97
98 int
darwin_sem_getvalue(darwin_sem_t * sem,unsigned int * sval)99 darwin_sem_getvalue(darwin_sem_t *sem, unsigned int *sval)
100 {
101 int res = 0;
102
103 pthread_mutex_lock(&sem->__data.local.count_lock);
104
105 if (sem->id != __SEM_ID_LOCAL) {
106 res = -1;
107 errno = EINVAL;
108 } else {
109 *sval = sem->__data.local.count;
110 }
111
112 pthread_mutex_unlock(&sem->__data.local.count_lock);
113
114 return res;
115 }
116
117 /* http://www.opengroup.org/onlinepubs/007908799/xsh/sem_post.html */
118 int
darwin_sem_post(darwin_sem_t * sem)119 darwin_sem_post(darwin_sem_t *sem)
120 {
121 int res = 0;
122
123 pthread_mutex_lock(&sem->__data.local.count_lock);
124
125 if (sem->id != __SEM_ID_LOCAL) {
126 res = -1;
127 errno = EINVAL;
128 } else if (sem->__data.local.count < DARWIN_SEM_VALUE_MAX) {
129 sem->__data.local.count++;
130 if (sem->__data.local.count == 1) {
131 pthread_cond_signal(&sem->__data.local.count_cond);
132 }
133 } else {
134 errno = ERANGE;
135 res = -1;
136 }
137
138 pthread_mutex_unlock(&sem->__data.local.count_lock);
139
140 return res;
141 }
142
143 /* http://www.opengroup.org/onlinepubs/009695399/functions/sem_timedwait.html */
144 int
darwin_sem_timedwait(darwin_sem_t * sem,const struct timespec * abs_timeout)145 darwin_sem_timedwait(darwin_sem_t *sem, const struct timespec *abs_timeout)
146 {
147 int res = 0;
148
149 if (abs_timeout &&
150 (abs_timeout->tv_nsec < 0 || abs_timeout->tv_nsec >= 1000000000)) {
151 errno = EINVAL;
152 return -1;
153 }
154
155 pthread_cleanup_push((void(*)(void*))&pthread_mutex_unlock,
156 &sem->__data.local.count_lock);
157
158 pthread_mutex_lock(&sem->__data.local.count_lock);
159
160 if (sem->id != __SEM_ID_LOCAL) {
161 errno = EINVAL;
162 res = -1;
163 } else {
164 if (!sem->__data.local.count) {
165 res = pthread_cond_timedwait(&sem->__data.local.count_cond,
166 &sem->__data.local.count_lock,
167 abs_timeout);
168 }
169 if (res) {
170 assert(res == ETIMEDOUT);
171 res = -1;
172 errno = ETIMEDOUT;
173 } else if (sem->id != __SEM_ID_LOCAL) {
174 res = -1;
175 errno = EINVAL;
176 } else {
177 sem->__data.local.count--;
178 }
179 }
180
181 pthread_cleanup_pop(1);
182
183 return res;
184 }
185
186 /* http://www.opengroup.org/onlinepubs/007908799/xsh/sem_trywait.html */
187 int
darwin_sem_trywait(darwin_sem_t * sem)188 darwin_sem_trywait(darwin_sem_t *sem)
189 {
190 int res = 0;
191
192 pthread_mutex_lock(&sem->__data.local.count_lock);
193
194 if (sem->id != __SEM_ID_LOCAL) {
195 res = -1;
196 errno = EINVAL;
197 } else if (sem->__data.local.count) {
198 sem->__data.local.count--;
199 } else {
200 res = -1;
201 errno = EAGAIN;
202 }
203
204 pthread_mutex_unlock (&sem->__data.local.count_lock);
205
206 return res;
207 }
208
209 /* http://www.opengroup.org/onlinepubs/007908799/xsh/sem_wait.html */
210 int
darwin_sem_wait(darwin_sem_t * sem)211 darwin_sem_wait(darwin_sem_t *sem)
212 {
213 /* Must be volatile or will be clobbered by longjmp */
214 volatile int res = 0;
215
216 pthread_cleanup_push((void(*)(void*))&pthread_mutex_unlock,
217 &sem->__data.local.count_lock);
218
219 pthread_mutex_lock(&sem->__data.local.count_lock);
220
221 if (sem->id != __SEM_ID_LOCAL) {
222 errno = EINVAL;
223 res = -1;
224 } else {
225 if (!sem->__data.local.count) {
226 pthread_cond_wait(&sem->__data.local.count_cond,
227 &sem->__data.local.count_lock);
228 if (!sem->__data.local.count) {
229 /* spurious wakeup, assume it is an interruption */
230 res = -1;
231 errno = EINTR;
232 goto out;
233 }
234 }
235 if (sem->id != __SEM_ID_LOCAL) {
236 res = -1;
237 errno = EINVAL;
238 } else {
239 sem->__data.local.count--;
240 }
241 }
242
243 out:
244 pthread_cleanup_pop(1);
245
246 return res;
247 }
248