summaryrefslogtreecommitdiff
path: root/libs/pbd/pbd/semaphore.h
blob: caa5eb68c29e983415a0ab43656209db87b2a9b2 (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
/*
  Copyright (C) 2012 Paul Davis
  Author: David Robillard

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#ifndef __pbd_semaphore_h__
#define __pbd_semaphore_h__

#ifdef __APPLE__
#    include <mach/mach.h>
#elif defined(PLATFORM_WINDOWS)
#    include <windows.h>
#ifndef INFINITE
#define INFINITE 0xffffffffL
#endif
#else
#    include <semaphore.h>
#    include <errno.h>
#endif

#include "pbd/failed_constructor.h"

namespace PBD {

/**
   Unnamed (process local) counting semaphore.

   The civilized person's synchronisation primitive.  A counting semaphore is
   an integer which is always non-negative, so, an attempted decrement (or
   "wait") will block if the value is 0, until another thread does an increment
   (or "post").

   At least on Lignux, the main advantage of this is that it is fast and the
   only safe way to reliably signal from a real-time audio thread.  The
   counting semantics also complement ringbuffers of events nicely.
*/
class Semaphore
{
public:
	/**
	   Create a new semaphore.

	   Chances are you want 1 wait() per 1 post(), an initial value of 0.
	*/
	inline Semaphore(unsigned initial);

	inline ~Semaphore();

	/** Post/Increment/Signal */
	inline void post();

	/** Wait/Decrement.  Returns false on error. */
	inline bool wait();

	/** Attempt Wait/Decrement.  Returns true iff a decrement occurred. */
	inline bool try_wait();

private:
#if defined(__APPLE__)
	semaphore_t _sem;  // sem_t is a worthless broken mess on OSX
#elif defined(PLATFORM_WINDOWS)
	HANDLE _sem;  // types are overrated anyway
#else
	sem_t _sem;
#endif
};

#ifdef __APPLE__

inline
Semaphore::Semaphore(unsigned initial)
{
	if (semaphore_create(mach_task_self(), &_sem, SYNC_POLICY_FIFO, initial)) {
		throw failed_constructor();
	}
}

inline
Semaphore::~Semaphore()
{
	semaphore_destroy(mach_task_self(), _sem);
}

inline void
Semaphore::post()
{
	semaphore_signal(_sem);
}

inline bool
Semaphore::wait()
{
	if (semaphore_wait(_sem) != KERN_SUCCESS) {
		return false;
	}
	return true;
}

inline bool
Semaphore::try_wait()
{
	const mach_timespec_t zero = { 0, 0 };
	return semaphore_timedwait(_sem, zero) == KERN_SUCCESS;
}

#elif defined(PLATFORM_WINDOWS)

inline
Semaphore::Semaphore(unsigned initial)
{
	if (!(_sem = CreateSemaphore(NULL, initial, LONG_MAX, NULL))) {
		throw failed_constructor();
	}
}

inline
Semaphore::~Semaphore()
{
	CloseHandle(_sem);
}

inline void
Semaphore::post()
{
	ReleaseSemaphore(_sem, 1, NULL);
}

inline bool
Semaphore::wait()
{
	if (WaitForSingleObject(_sem, INFINITE) != WAIT_OBJECT_0) {
		return false;
	}
	return true;
}

inline bool
Semaphore::try_wait()
{
	return WaitForSingleObject(_sem, 0) == WAIT_OBJECT_0;
}

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

Semaphore::Semaphore(unsigned initial)
{
	if (sem_init(&_sem, 0, initial)) {
		throw failed_constructor();
	}
}

inline
Semaphore::~Semaphore()
{
	sem_destroy(&_sem);
}

inline void
Semaphore::post()
{
	sem_post(&_sem);
}

inline bool
Semaphore::wait()
{
	while (sem_wait(&_sem)) {
		if (errno != EINTR) {
			return false;  // We are all doomed
		}
		/* Otherwise, interrupted (rare/weird), so try again. */
	}

	return true;
}

inline bool
Semaphore::try_wait()
{
	return (sem_trywait(&_sem) == 0);
}

#endif

}  // namespace PBD

#endif  /* __pbd_semaphore_h__ */