aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/eg-sampler.lv2/zix/sem.h
blob: d536c9952a8c206aca94f9891364f8cf34d19ec3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*
  Copyright 2012-2012 David Robillard <http://drobilla.net>

  Permission to use, copy, modify, and/or distribute this software for any
  purpose with or without fee is hereby granted, provided that the above
  copyright notice and this permission notice appear in all copies.

  THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

#ifndef ZIX_SEM_H
#define ZIX_SEM_H

#ifdef __APPLE__
#    include <mach/mach.h>
#elif defined(_WIN32)
#    include <windows.h>
#else
#    include <semaphore.h>
#    include <errno.h>
#endif

#include "zix/common.h"

#ifdef __cplusplus
extern "C" {
#endif

/**
   @addtogroup zix
   @{
   @name Semaphore
   @{
*/

/**
   A counting semaphore.

   This is an integer that is always positive, and has two main operations:
   increment (post) and decrement (wait).  If a decrement can not be performed
   (i.e. the value is 0) the caller will be blocked until another thread posts
   and the operation can succeed.

   Semaphores can be created with any starting value, but typically this will
   be 0 so the semaphore can be used as a simple signal where each post
   corresponds to one wait.

   Semaphores are very efficient (much moreso than a mutex/cond pair).  In
   particular, at least on Linux, post is async-signal-safe, which means it
   does not block and will not be interrupted.  If you need to signal from
   a realtime thread, this is the most appropriate primitive to use.
*/
typedef struct ZixSemImpl ZixSem;

/**
   Create and initialize @c sem to @c initial.
*/
static inline ZixStatus
zix_sem_init(ZixSem* sem, unsigned initial);

/**
   Destroy @c sem.
*/
static inline void
zix_sem_destroy(ZixSem* sem);

/**
   Increment (and signal any waiters).
   Realtime safe.
*/
static inline void
zix_sem_post(ZixSem* sem);

/**
   Wait until count is > 0, then decrement.
   Obviously not realtime safe.
*/
static inline ZixStatus
zix_sem_wait(ZixSem* sem);

/**
   Non-blocking version of wait().

   @return true if decrement was successful (lock was acquired).
*/
static inline bool
zix_sem_try_wait(ZixSem* sem);

/**
   @cond
*/

#ifdef __APPLE__

struct ZixSemImpl {
	semaphore_t sem;
};

static inline ZixStatus
zix_sem_init(ZixSem* sem, unsigned initial)
{
	return semaphore_create(mach_task_self(), &sem->sem, SYNC_POLICY_FIFO, 0)
		? ZIX_STATUS_ERROR : ZIX_STATUS_SUCCESS;
}

static inline void
zix_sem_destroy(ZixSem* sem)
{
	semaphore_destroy(mach_task_self(), sem->sem);
}

static inline void
zix_sem_post(ZixSem* sem)
{
	semaphore_signal(sem->sem);
}

static inline ZixStatus
zix_sem_wait(ZixSem* sem)
{
	if (semaphore_wait(sem->sem) != KERN_SUCCESS) {
		return ZIX_STATUS_ERROR;
	}
	return ZIX_STATUS_SUCCESS;
}

static inline bool
zix_sem_try_wait(ZixSem* sem)
{
	const mach_timespec_t zero = { 0, 0 };
	return semaphore_timedwait(sem->sem, zero) == KERN_SUCCESS;
}

#elif defined(_WIN32)

struct ZixSemImpl {
	HANDLE sem;
};

static inline ZixStatus
zix_sem_init(ZixSem* sem, unsigned initial)
{
	sem->sem = CreateSemaphore(NULL, initial, LONG_MAX, NULL);
	return (sem->sem) ? ZIX_STATUS_ERROR : ZIX_STATUS_SUCCESS;
}

static inline void
zix_sem_destroy(ZixSem* sem)
{
	CloseHandle(sem->sem);
}

static inline void
zix_sem_post(ZixSem* sem)
{
	ReleaseSemaphore(sem->sem, 1, NULL);
}

static inline ZixStatus
zix_sem_wait(ZixSem* sem)
{
	if (WaitForSingleObject(sem->sem, INFINITE) != WAIT_OBJECT_0) {
		return ZIX_STATUS_ERROR;
	}
	return ZIX_STATUS_SUCCESS;
}

static inline bool
zix_sem_try_wait(ZixSem* sem)
{
	WaitForSingleObject(sem->sem, 0);
}

#else  /* !defined(__APPLE__) && !defined(_WIN32) */

struct ZixSemImpl {
	sem_t sem;
};

static inline ZixStatus
zix_sem_init(ZixSem* sem, unsigned initial)
{
	return sem_init(&sem->sem, 0, initial)
		? ZIX_STATUS_ERROR : ZIX_STATUS_SUCCESS;
}

static inline void
zix_sem_destroy(ZixSem* sem)
{
	sem_destroy(&sem->sem);
}

static inline void
zix_sem_post(ZixSem* sem)
{
	sem_post(&sem->sem);
}

static inline ZixStatus
zix_sem_wait(ZixSem* sem)
{
	while (sem_wait(&sem->sem)) {
		if (errno != EINTR) {
			return ZIX_STATUS_ERROR;
		}
		/* Otherwise, interrupted, so try again. */
	}

	return ZIX_STATUS_SUCCESS;
}

static inline bool
zix_sem_try_wait(ZixSem* sem)
{
	return (sem_trywait(&sem->sem) == 0);
}

#endif

/**
   @endcond
   @}
   @}
*/

#ifdef __cplusplus
}  /* extern "C" */
#endif

#endif  /* ZIX_SEM_H */