-
Notifications
You must be signed in to change notification settings - Fork 32
/
ndicapi_thread.cxx
273 lines (230 loc) · 8.21 KB
/
ndicapi_thread.cxx
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*=======================================================================
Copyright (c) 2000-2005 Atamai, Inc.
Use, modification and redistribution of the software, in source or
binary forms, are permitted provided that the following terms and
conditions are met:
1) Redistribution of the source code, in verbatim or modified
form, must retain the above copyright notice, this license,
the following disclaimer, and any notices that refer to this
license and/or the following disclaimer.
2) Redistribution in binary form must include the above copyright
notice, a copy of this license and the following disclaimer
in the documentation or with other materials provided with the
distribution.
3) Modified copies of the source code must be clearly marked as such,
and must not be misrepresented as verbatim copies of the source code.
THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE SOFTWARE "AS IS"
WITHOUT EXPRESSED OR IMPLIED WARRANTY INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE. IN NO EVENT SHALL ANY COPYRIGHT HOLDER OR OTHER PARTY WHO MAY
MODIFY AND/OR REDISTRIBUTE THE SOFTWARE UNDER THE TERMS OF THIS LICENSE
BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, LOSS OF DATA OR DATA BECOMING INACCURATE
OR LOSS OF PROFIT OR BUSINESS INTERRUPTION) ARISING IN ANY WAY OUT OF
THE USE OR INABILITY TO USE THE SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGES.
=======================================================================*/
#include "ndicapi_thread.h"
#include <stdlib.h>
// The interface is modeled after the Windows threading interface,
// but the only real difference from POSIX threads is the "Event"
// type which does not exists in POSIX threads (more information is
// provided below)
#ifdef _WIN32
//----------------------------------------------------------------------------
ndicapiExport HANDLE ndiMutexCreate()
{
return CreateMutex(0, FALSE, 0);
}
//----------------------------------------------------------------------------
ndicapiExport void ndiMutexDestroy(HANDLE mutex)
{
CloseHandle(mutex);
}
//----------------------------------------------------------------------------
ndicapiExport void ndiMutexLock(HANDLE mutex)
{
WaitForSingleObject(mutex, INFINITE);
}
//----------------------------------------------------------------------------
ndicapiExport void ndiMutexUnlock(HANDLE mutex)
{
ReleaseMutex(mutex);
}
#elif defined(unix) || defined(__unix__) || defined(__APPLE__)
//----------------------------------------------------------------------------
ndicapiExport pthread_mutex_t* ndiMutexCreate()
{
pthread_mutex_t* mutex;
mutex = (pthread_mutex_t*)malloc(sizeof(pthread_mutex_t));
pthread_mutex_init(mutex, 0);
return mutex;
}
//----------------------------------------------------------------------------
ndicapiExport void ndiMutexDestroy(pthread_mutex_t* mutex)
{
pthread_mutex_destroy(mutex);
free(mutex);
}
//----------------------------------------------------------------------------
ndicapiExport void ndiMutexLock(pthread_mutex_t* mutex)
{
pthread_mutex_lock(mutex);
}
//----------------------------------------------------------------------------
ndicapiExport void ndiMutexUnlock(pthread_mutex_t* mutex)
{
pthread_mutex_unlock(mutex);
}
#endif
#ifdef _WIN32
//----------------------------------------------------------------------------
ndicapiExport HANDLE ndiEventCreate()
{
return CreateEvent(0, FALSE, FALSE, 0);
}
//----------------------------------------------------------------------------
ndicapiExport void ndiEventDestroy(HANDLE event)
{
CloseHandle(event);
}
//----------------------------------------------------------------------------
ndicapiExport void ndiEventSignal(HANDLE event)
{
SetEvent(event);
}
//----------------------------------------------------------------------------
ndicapiExport int ndiEventWait(HANDLE event, int milliseconds)
{
if (milliseconds < 0)
{
WaitForSingleObject(event, INFINITE);
}
else
{
if (WaitForSingleObject(event, milliseconds) == WAIT_TIMEOUT)
{
return 1;
}
}
return 0;
}
#elif defined(unix) || defined(__unix__) || defined(__APPLE__)
// There is no equivalent of an 'event' in POSIX threads, so we define
// our own event type consisting of a boolean variable (to say whether
// an event has occurred), a cond, an a mutex for locking the cond
// and the variable.
//----------------------------------------------------------------------------
ndicapiExport pl_cond_and_mutex_t* ndiEventCreate()
{
pl_cond_and_mutex_t* event;
event = (pl_cond_and_mutex_t*)malloc(sizeof(pl_cond_and_mutex_t));
event->signalled = 0;
pthread_cond_init(&event->cond, 0);
pthread_mutex_init(&event->mutex, 0);
return event;
}
//----------------------------------------------------------------------------
ndicapiExport void ndiEventDestroy(pl_cond_and_mutex_t* event)
{
pthread_cond_destroy(&event->cond);
pthread_mutex_destroy(&event->mutex);
free(event);
}
//----------------------------------------------------------------------------
// Setting the event is simple: lock, set the variable, signal the cond,
// and unlock.
ndicapiExport void ndiEventSignal(pl_cond_and_mutex_t* event)
{
pthread_mutex_lock(&event->mutex);
event->signalled = 1;
pthread_cond_signal(&event->cond);
pthread_mutex_unlock(&event->mutex);
}
//----------------------------------------------------------------------------
// Waiting for the event is simple if we don't want a timed wait:
// lock, check the variable, wait until the variable becomes set,
// unset the variable, unlock.
// Note that the event can be received by only one thread.
//
// If a timed wait is needed, then there is a little bit of
// hassle because the pthread_cond_timedwait() wait is until
// an absolute time, so we must get the current time and then
// do a little math as well as a conversion from one time structure
// to another time structure.
ndicapiExport int ndiEventWait(pl_cond_and_mutex_t* event, int milliseconds)
{
int timedout = 0;
if (milliseconds < 0) /* do infinite wait */
{
pthread_mutex_lock(&event->mutex);
if (event->signalled == 0)
{
pthread_cond_wait(&event->cond, &event->mutex);
}
event->signalled = 0;
pthread_mutex_unlock(&event->mutex);
}
else /* do timed wait */
{
struct timeval tv;
struct timespec ts;
pthread_mutex_lock(&event->mutex);
if (event->signalled == 0)
{
/* all the time stuff is used to check for timeouts */
gettimeofday(&tv, 0);
tv.tv_sec += milliseconds / 1000; /* msec to sec */
tv.tv_usec += (milliseconds % 1000) * 1000; /* msec to usec */
if (tv.tv_usec >= 1000000) /* if usec overflow */
{
tv.tv_usec -= 1000000;
tv.tv_sec += 1;
}
/* convert timeval to timespec */
ts.tv_sec = tv.tv_sec;
ts.tv_nsec = tv.tv_usec * 1000;
#ifdef PTHREAD_COND_TIMEDWAIT_USES_TIMEVAL
timedout = (pthread_cond_timedwait(&event->cond, &event->mutex, &tv) == ETIMEDOUT);
#else
timedout = (pthread_cond_timedwait(&event->cond, &event->mutex, &ts) == ETIMEDOUT);
#endif
}
if (!timedout)
{
event->signalled = 0;
}
pthread_mutex_unlock(&event->mutex);
}
return timedout;
}
#endif
#ifdef _WIN32
//----------------------------------------------------------------------------
ndicapiExport HANDLE ndiThreadSplit(void* thread_func(void* userdata), void* userdata)
{
DWORD thread_id;
return CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)&thread_func, userdata, 0, &thread_id);
}
//----------------------------------------------------------------------------
ndicapiExport void ndiThreadJoin(HANDLE Thread)
{
WaitForSingleObject(Thread, INFINITE);
}
#elif defined(unix) || defined(__unix__) || defined(__APPLE__)
//----------------------------------------------------------------------------
ndicapiExport pthread_t ndiThreadSplit(void* thread_func(void* userdata), void* userdata)
{
pthread_t Thread;
if (pthread_create(&Thread, 0, thread_func, userdata))
{
return 0;
}
return Thread;
}
//----------------------------------------------------------------------------
ndicapiExport void ndiThreadJoin(pthread_t Thread)
{
pthread_join(Thread, 0);
}
#endif