-
Notifications
You must be signed in to change notification settings - Fork 1
/
threads.c
410 lines (331 loc) · 9.29 KB
/
threads.c
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/**
Library : Cross Platform C11 Native Threads
Author : Gokan EKINCI
Licence : CC-BY-NC-ND
Standard interface : http://en.cppreference.com/w/c/thread
Windows Notes:
Types : https://msdn.microsoft.com/en-us/library/windows/desktop/aa383751(v=vs.85).aspx
GNU:
https://www.gnu.org/prep/standards/standards.html
*/
#ifndef C11_THREADS_IMPLEMENTATION
#define C11_THREADS_IMPLEMENTATION
#include "threads.h"
#ifdef __unix__
#define thrd_errno errno
typedef void*(*POSIX_START_ROUTINE)(void*);
#endif /* __unix__ */
#ifdef _WIN32
#define thrd_errno GetLastError()
#endif /* _WIN32 */
#include <errno.h> /* For errno */
/**
* Posix:
* http://man7.org/linux/man-pages/man3/pthread_create.3.html
* Windows:
* https://msdn.microsoft.com/fr-fr/library/windows/desktop/ms682516(v=vs.85).aspx
* https://msdn.microsoft.com/fr-fr/library/windows/desktop/ms682453(v=vs.85).aspx
*/
int thrd_create(__OUT__ thrd_t* thr, thrd_start_t func, void* arg) {
#ifdef __unix__
int value = pthread_create(thr, NULL, (POSIX_START_ROUTINE) func, arg);
/* ERROR: Setting standard errno with posix value returned from function */
if (value != 0) {
errno = value;
return thrd_error;
}
#endif /* __unix__ */
#ifdef _WIN32
*thr = CreateThread(
NULL, // default security attributes
0, // use default stack size
(LPTHREAD_START_ROUTINE) func, // thread function name
arg, // argument to thread function
0, // use default creation flags
NULL // returns the thread identifier
);
/* ERROR: Setting standard errno with windows error value */
if(*thr == NULL) {
errno = thrd_errno;
return thrd_error;
}
#endif /* _WIN32 */
/* SUCCESS */
return thrd_success;
}
/**
* Posix: http://man7.org/linux/man-pages/man3/pthread_self.3.html
* Windows: https://msdn.microsoft.com/en-US/library/windows/desktop/ms683182(v=vs.85).aspx
*/
thrd_t thrd_current(void) {
#ifdef __unix__
return pthread_self();
#endif /* __unix__ */
#ifdef _WIN32
return GetCurrentThread();
#endif /* _WIN32 */
}
/**
* Posix:
* http://man7.org/linux/man-pages/man3/pthread_detach.3.html
* Windows:
* https://msdn.microsoft.com/en-us/library/windows/desktop/ms724211.aspx
* http://stackoverflow.com/questions/12744324/how-to-detach-a-thread-on-windows-c
*/
int thrd_detach(thrd_t thr) {
#ifdef __unix__
int value = pthread_detach(thr);
/* ERROR: Setting standard errno with posix value returned from function */
if (value != 0) {
errno = value;
return thrd_error;
}
#endif /* __unix__ */
#ifdef _WIN32
/* Documentation: Closing a thread handle does not terminate the associated thread or remove the thread object. */
if (CloseHandle(thr) == 0) {
/* ERROR */
errno = thrd_errno;
return thrd_error;
}
#endif /* _WIN32 */
/* SUCCESS */
return thrd_success;
}
/**
* Windows: https://msdn.microsoft.com/en-US/library/windows/desktop/ms683233(v=vs.85).aspx
*/
int thrd_equal(thrd_t lhs, thrd_t rhs) {
#ifdef __unix__
return (lhs == rhs);
#endif /* __unix__ */
#ifdef _WIN32
DWORD lhsValue = GetThreadId(lhs);
if (lhsValue == 0) {
/* ERROR */
errno = thrd_errno;
return thrd_error;
}
DWORD rhsValue = GetThreadId(rhs);
if (rhsValue == 0) {
/* ERROR */
errno = thrd_errno;
return thrd_error;
}
return (lhsValue == rhsValue);
#endif /* _WIN32 */
}
/**
* Posix: http://man7.org/linux/man-pages/man3/pthread_exit.3.html
* Windows: https://msdn.microsoft.com/fr-fr/library/windows/desktop/ms682659(v=vs.85).aspx
*/
void thrd_exit(int res) {
#ifdef __unix__
pthread_exit((void*) &res);
#endif /* __unix__ */
#ifdef _WIN32
ExitThread((DWORD) res);
#endif /* _WIN32 */
}
/**
* Posix: http://man7.org/linux/man-pages/man3/pthread_join.3.html
* Windows: https://msdn.microsoft.com/fr-fr/library/windows/desktop/ms687032(v=vs.85).aspx
*/
int thrd_join(thrd_t thr, __OUT__ int* res) {
#ifdef __unix__
int value = pthread_join(thr, (void**) &res);
/* ERROR: Setting standard errno with posix value returned from function */
if (value != 0) {
errno = value;
return thrd_error;
}
#endif /* __unix__ */
#ifdef _WIN32
DWORD status = WaitForSingleObject(
thr,
INFINITE
);
/* Setting res value */
if (res != NULL) {
*res = (int) status;
}
/* ERROR: Setting standard errno with windows error value */
if (status != WAIT_OBJECT_0) {
if (status == WAIT_FAILED) {
errno = thrd_errno;
} else {
errno = status;
}
return thrd_error;
}
#endif /* _WIN32 */
/* SUCCESS */
return thrd_success;
}
/**
* Posix: https://linux.die.net/man/3/pthread_yield
* Windows: https://msdn.microsoft.com/en-US/library/windows/desktop/ms686352(v=vs.85).aspx
*/
void thrd_yield(void) {
#ifdef __unix__
int value = pthread_yield();
/* ERROR: Setting standard errno with posix value returned from function */
if (value != 0) {
errno = value;
/* This standard function needs a return value... */
}
#endif /* __unix__ */
#ifdef _WIN32
SwitchToThread();
#endif /* _WIN32 */
}
/**
* Posix: https://linux.die.net/man/3/pthread_mutex_destroy
* Windows: https://msdn.microsoft.com/en-US/library/windows/desktop/ms724211(v=vs.85).aspx
*/
void mtx_destroy(__OUT__ mtx_t* mutex) {
#ifdef __unix__
int value = pthread_mutex_destroy(mutex);
/* ERROR: Setting standard errno with posix value returned from function */
if (value != 0) {
errno = value;
/* This standard function needs a return value... */
}
#endif /* __unix__ */
#ifdef _WIN32
if (CloseHandle(*mutex) == 0) {
/* ERROR */
errno = thrd_errno;
/* This standard function needs a return value... */
}
#endif /* _WIN32 */
}
/**
* Posix: https://linux.die.net/man/3/pthread_mutex_init
* Windows: https://msdn.microsoft.com/en-us/library/windows/desktop/ms682411(v=vs.85).aspx
*/
int mtx_init(__OUT__ mtx_t* mutex, int type) {
#ifdef __unix__
int value = 0;
if (type == (mtx_plain | mtx_recursive)) {
pthread_mutexattr_t properties;
pthread_mutexattr_init(&properties);
pthread_mutexattr_settype(&properties, PTHREAD_MUTEX_RECURSIVE);
value = pthread_mutex_init(mutex, &properties);
} else if (type == mtx_plain) {
value = pthread_mutex_init(mutex, NULL);
} else {
/* ERROR: Unsupported type parameter */
errno = EINVAL;
return thrd_error;
}
/* ERROR: Setting standard errno with posix value returned from function */
if (value != 0) {
errno = value;
return thrd_error;
}
#endif /* __unix__ */
#ifdef _WIN32
/* ERROR: Unsupported type parameter */
if (type != mtx_plain || type != (mtx_plain | mtx_recursive)) {
return thrd_error;
}
BOOL owner = ((type & mtx_recursive) == mtx_recursive);
*mutex = CreateMutex(
NULL, // default security attributes
owner, // owned if true
NULL // unnamed mutex
);
/* ERROR: Setting standard errno with windows error value */
if(*mutex == NULL) {
errno = thrd_errno;
return thrd_error;
}
#endif /* _WIN32 */
/* SUCCESS */
return thrd_success;
}
/**
* Posix: https://linux.die.net/man/3/pthread_mutex_lock
* Windows: https://msdn.microsoft.com/en-US/library/windows/desktop/ms687032(v=vs.85).aspx
*/
int mtx_lock(__OUT__ mtx_t* mutex) {
#ifdef __unix__
int value = pthread_mutex_lock(mutex);
/* ERROR: Setting standard errno with posix value returned from function */
if (value != 0) {
errno = value;
return thrd_error;
}
#endif /* __unix__ */
#ifdef _WIN32
DWORD status = WaitForSingleObject(*mutex, INFINITE);
/* ERROR: Setting standard errno with windows error value */
if (status != WAIT_OBJECT_0) {
if (status == WAIT_FAILED) {
errno = thrd_errno;
} else {
errno = status;
}
return thrd_error;
}
#endif /* _WIN32 */
/* SUCCESS */
return thrd_success;
}
/**
* Posix: https://linux.die.net/man/3/pthread_mutex_trylock
* Windows: https://msdn.microsoft.com/en-US/library/windows/desktop/ms687032(v=vs.85).aspx
*/
int mtx_trylock(__OUT__ mtx_t* mutex) {
#ifdef __unix__
int value = pthread_mutex_trylock(mutex);
/* ERROR: Setting standard errno with posix value returned from function */
if (value != 0) {
errno = value;
return (value == EBUSY) ? thrd_busy : thrd_error;
}
#endif /* __unix__ */
#ifdef _WIN32
DWORD status = WaitForSingleObject(*mutex, INFINITE);
/* ERROR: Setting standard errno with windows error value */
if (status != WAIT_OBJECT_0) {
if (status == WAIT_TIMEOUT) {
errno = status;
return thrd_busy;
} else if (status == WAIT_FAILED) {
errno = thrd_errno;
return thrd_error;
} else {
errno = status;
return thrd_error;
}
}
#endif /* _WIN32 */
/* SUCCESS */
return thrd_success;
}
/**
* Posix: https://linux.die.net/man/3/pthread_mutex_unlock
* Windows: https://msdn.microsoft.com/en-US/library/windows/desktop/ms685066(v=vs.85).aspx
*/
int mtx_unlock(__OUT__ mtx_t* mutex) {
#ifdef __unix__
int value = pthread_mutex_unlock(mutex);
/* ERROR: Setting standard errno with posix value returned from function */
if (value != 0) {
errno = value;
return thrd_error;
}
#endif /* __unix__ */
#ifdef _WIN32
if (ReleaseMutex(*mutex) == 0) {
/* ERROR */
errno = thrd_errno;
return thrd_error;
}
#endif /* _WIN32 */
/* SUCCESS */
return thrd_success;
}
#endif /* C11_THREADS_IMPLEMENTATION */