1 /*
2 Copyright 2012 David Robillard <http://drobilla.net>
3
4 Permission to use, copy, modify, and/or distribute this software for any
5 purpose with or without fee is hereby granted, provided that the above
6 copyright notice and this permission notice appear in all copies.
7
8 THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17 #ifndef ZIX_SEM_H
18 #define ZIX_SEM_H
19
20 #ifdef __APPLE__
21 # include <mach/mach.h>
22 #elif defined(_WIN32)
23 # include <limits.h>
24 # include <windows.h>
25 #else
26 # include <semaphore.h>
27 #endif
28
29 #include "common.h"
30
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34
35 /**
36 @addtogroup zix
37 @{
38 @name Semaphore
39 @{
40 */
41
42 /**
43 A counting semaphore.
44
45 This is an integer that is always positive, and has two main operations:
46 increment (post) and decrement (wait). If a decrement can not be performed
47 (i.e. the value is 0) the caller will be blocked until another thread posts
48 and the operation can succeed.
49
50 Semaphores can be created with any starting value, but typically this will
51 be 0 so the semaphore can be used as a simple signal where each post
52 corresponds to one wait.
53
54 Semaphores are very efficient (much moreso than a mutex/cond pair). In
55 particular, at least on Linux, post is async-signal-safe, which means it
56 does not block and will not be interrupted. If you need to signal from
57 a realtime thread, this is the most appropriate primitive to use.
58 */
59 typedef struct ZixSemImpl ZixSem;
60
61 /**
62 Create and initialize @c sem to @c initial.
63 */
64 static inline ZixStatus
65 zix_sem_init(ZixSem* sem, unsigned initial);
66
67 /**
68 Destroy @c sem.
69 */
70 static inline void
71 zix_sem_destroy(ZixSem* sem);
72
73 /**
74 Increment (and signal any waiters).
75 Realtime safe.
76 */
77 static inline void
78 zix_sem_post(ZixSem* sem);
79
80 /**
81 Wait until count is > 0, then decrement.
82 Obviously not realtime safe.
83 */
84 static inline void
85 zix_sem_wait(ZixSem* sem);
86
87 /**
88 Non-blocking version of wait().
89
90 @return true if decrement was successful (lock was acquired).
91 */
92 static inline bool
93 zix_sem_try_wait(ZixSem* sem);
94
95 /**
96 @cond
97 */
98
99 #ifdef __APPLE__
100
101 struct ZixSemImpl {
102 semaphore_t sem;
103 };
104
105 static inline ZixStatus
zix_sem_init(ZixSem * sem,unsigned initial)106 zix_sem_init(ZixSem* sem, unsigned initial)
107 {
108 return semaphore_create(mach_task_self(), &sem->sem, SYNC_POLICY_FIFO, 0)
109 ? ZIX_STATUS_ERROR : ZIX_STATUS_SUCCESS;
110 }
111
112 static inline void
zix_sem_destroy(ZixSem * sem)113 zix_sem_destroy(ZixSem* sem)
114 {
115 semaphore_destroy(mach_task_self(), sem->sem);
116 }
117
118 static inline void
zix_sem_post(ZixSem * sem)119 zix_sem_post(ZixSem* sem)
120 {
121 semaphore_signal(sem->sem);
122 }
123
124 static inline void
zix_sem_wait(ZixSem * sem)125 zix_sem_wait(ZixSem* sem)
126 {
127 semaphore_wait(sem->sem);
128 }
129
130 static inline bool
zix_sem_try_wait(ZixSem * sem)131 zix_sem_try_wait(ZixSem* sem)
132 {
133 const mach_timespec_t zero = { 0, 0 };
134 return semaphore_timedwait(sem->sem, zero) == KERN_SUCCESS;
135 }
136
137 #elif defined(_WIN32)
138
139 struct ZixSemImpl {
140 HANDLE sem;
141 };
142
143 static inline ZixStatus
zix_sem_init(ZixSem * sem,unsigned initial)144 zix_sem_init(ZixSem* sem, unsigned initial)
145 {
146 sem->sem = CreateSemaphore(NULL, initial, LONG_MAX, NULL);
147 return (sem->sem) ? ZIX_STATUS_ERROR : ZIX_STATUS_SUCCESS;
148 }
149
150 static inline void
zix_sem_destroy(ZixSem * sem)151 zix_sem_destroy(ZixSem* sem)
152 {
153 CloseHandle(sem->sem);
154 }
155
156 static inline void
zix_sem_post(ZixSem * sem)157 zix_sem_post(ZixSem* sem)
158 {
159 ReleaseSemaphore(sem->sem, 1, NULL);
160 }
161
162 static inline void
zix_sem_wait(ZixSem * sem)163 zix_sem_wait(ZixSem* sem)
164 {
165 WaitForSingleObject(sem->sem, INFINITE);
166 }
167
168 static inline bool
zix_sem_try_wait(ZixSem * sem)169 zix_sem_try_wait(ZixSem* sem)
170 {
171 WaitForSingleObject(sem->sem, 0);
172 }
173
174 #else /* !defined(__APPLE__) && !defined(_WIN32) */
175
176 struct ZixSemImpl {
177 sem_t sem;
178 };
179
180 static inline ZixStatus
zix_sem_init(ZixSem * sem,unsigned initial)181 zix_sem_init(ZixSem* sem, unsigned initial)
182 {
183 return sem_init(&sem->sem, 0, initial)
184 ? ZIX_STATUS_ERROR : ZIX_STATUS_SUCCESS;
185 }
186
187 static inline void
zix_sem_destroy(ZixSem * sem)188 zix_sem_destroy(ZixSem* sem)
189 {
190 sem_destroy(&sem->sem);
191 }
192
193 static inline void
zix_sem_post(ZixSem * sem)194 zix_sem_post(ZixSem* sem)
195 {
196 sem_post(&sem->sem);
197 }
198
199 static inline void
zix_sem_wait(ZixSem * sem)200 zix_sem_wait(ZixSem* sem)
201 {
202 /* Note that sem_wait always returns 0 in practice, except in
203 gdb (at least), where it returns nonzero, so the while is
204 necessary (and is the correct/safe solution in any case).
205 */
206 while (sem_wait(&sem->sem) != 0) {}
207 }
208
209 static inline bool
zix_sem_try_wait(ZixSem * sem)210 zix_sem_try_wait(ZixSem* sem)
211 {
212 return (sem_trywait(&sem->sem) == 0);
213 }
214
215 #endif
216
217 /**
218 @endcond
219 @}
220 @}
221 */
222
223 #ifdef __cplusplus
224 } /* extern "C" */
225 #endif
226
227 #endif /* ZIX_SEM_H */
228