1 /*
2 * Copyright (c) 2013-2014 Hugh Bailey <obs.jim@gmail.com>
3 *
4 * Permission to use, copy, modify, and 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 * THE 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 #pragma once
18
19 /*
20 * Allows posix thread usage on windows as well as other operating systems.
21 * Use this header if you want to make your code more platform independent.
22 *
23 * Also provides a custom platform-independent "event" handler via
24 * pthread conditional waits.
25 */
26
27 #include "c99defs.h"
28
29 #ifdef _MSC_VER
30 #include "../../deps/w32-pthreads/pthread.h"
31 #else
32 #include <errno.h>
33 #include <pthread.h>
34 #endif
35
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39
40 #ifdef _WIN32
41 #include "threading-windows.h"
42 #else
43 #include "threading-posix.h"
44 #endif
45
46 /* this may seem strange, but you can't use it unless it's an initializer */
pthread_mutex_init_value(pthread_mutex_t * mutex)47 static inline void pthread_mutex_init_value(pthread_mutex_t *mutex)
48 {
49 pthread_mutex_t init_val = PTHREAD_MUTEX_INITIALIZER;
50 if (!mutex)
51 return;
52
53 *mutex = init_val;
54 }
55
pthread_mutex_init_recursive(pthread_mutex_t * mutex)56 static inline int pthread_mutex_init_recursive(pthread_mutex_t *mutex)
57 {
58 pthread_mutexattr_t attr;
59 int ret = pthread_mutexattr_init(&attr);
60 if (ret == 0) {
61 ret = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
62 if (ret == 0) {
63 ret = pthread_mutex_init(mutex, &attr);
64 }
65
66 pthread_mutexattr_destroy(&attr);
67 }
68
69 return ret;
70 }
71
72 enum os_event_type {
73 OS_EVENT_TYPE_AUTO,
74 OS_EVENT_TYPE_MANUAL,
75 };
76
77 struct os_event_data;
78 struct os_sem_data;
79 typedef struct os_event_data os_event_t;
80 typedef struct os_sem_data os_sem_t;
81
82 EXPORT int os_event_init(os_event_t **event, enum os_event_type type);
83 EXPORT void os_event_destroy(os_event_t *event);
84 EXPORT int os_event_wait(os_event_t *event);
85 EXPORT int os_event_timedwait(os_event_t *event, unsigned long milliseconds);
86 EXPORT int os_event_try(os_event_t *event);
87 EXPORT int os_event_signal(os_event_t *event);
88 EXPORT void os_event_reset(os_event_t *event);
89
90 EXPORT int os_sem_init(os_sem_t **sem, int value);
91 EXPORT void os_sem_destroy(os_sem_t *sem);
92 EXPORT int os_sem_post(os_sem_t *sem);
93 EXPORT int os_sem_wait(os_sem_t *sem);
94
95 EXPORT void os_set_thread_name(const char *name);
96
97 #ifdef _MSC_VER
98 #define THREAD_LOCAL __declspec(thread)
99 #else
100 #define THREAD_LOCAL __thread
101 #endif
102
103 #ifdef __cplusplus
104 }
105 #endif
106