src/util/threading.h (view raw)
1/* Copyright (c) 2013-2014 Jeffrey Pfau
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6#ifndef THREADING_H
7#define THREADING_H
8
9#include "util/common.h"
10
11#ifndef DISABLE_THREADING
12#ifdef USE_PTHREADS
13#include <pthread.h>
14#include <sys/time.h>
15#ifdef __FreeBSD__
16#include <pthread_np.h>
17#endif
18
19#define THREAD_ENTRY void*
20typedef THREAD_ENTRY (*ThreadEntry)(void*);
21
22typedef pthread_t Thread;
23typedef pthread_mutex_t Mutex;
24typedef pthread_cond_t Condition;
25
26static inline int MutexInit(Mutex* mutex) {
27 return pthread_mutex_init(mutex, 0);
28}
29
30static inline int MutexDeinit(Mutex* mutex) {
31 return pthread_mutex_destroy(mutex);
32}
33
34static inline int MutexLock(Mutex* mutex) {
35 return pthread_mutex_lock(mutex);
36}
37
38static inline int MutexUnlock(Mutex* mutex) {
39 return pthread_mutex_unlock(mutex);
40}
41
42static inline int ConditionInit(Condition* cond) {
43 return pthread_cond_init(cond, 0);
44}
45
46static inline int ConditionDeinit(Condition* cond) {
47 return pthread_cond_destroy(cond);
48}
49
50static inline int ConditionWait(Condition* cond, Mutex* mutex) {
51 return pthread_cond_wait(cond, mutex);
52}
53
54static inline int ConditionWaitTimed(Condition* cond, Mutex* mutex, int32_t timeoutMs) {
55 struct timespec ts;
56 struct timeval tv;
57
58 gettimeofday(&tv, 0);
59 ts.tv_sec = tv.tv_sec;
60 ts.tv_nsec = (tv.tv_usec + timeoutMs * 1000L) * 1000L;
61 if (ts.tv_nsec >= 1000000000L) {
62 ts.tv_nsec -= 1000000000L;
63 ++ts.tv_sec;
64 }
65
66 return pthread_cond_timedwait(cond, mutex, &ts);
67}
68
69static inline int ConditionWake(Condition* cond) {
70 return pthread_cond_broadcast(cond);
71}
72
73static inline int ThreadCreate(Thread* thread, ThreadEntry entry, void* context) {
74 return pthread_create(thread, 0, entry, context);
75}
76
77static inline int ThreadJoin(Thread thread) {
78 return pthread_join(thread, 0);
79}
80
81static inline int ThreadSetName(const char* name) {
82#ifdef __APPLE__
83 return pthread_setname_np(name);
84#elif defined(__FreeBSD__)
85 pthread_set_name_np(pthread_self(), name);
86 return 0;
87#else
88 return pthread_setname_np(pthread_self(), name);
89#endif
90}
91
92#elif _WIN32
93#define _WIN32_WINNT 0x0600
94#include <windows.h>
95#define THREAD_ENTRY DWORD WINAPI
96typedef THREAD_ENTRY ThreadEntry(LPVOID);
97
98typedef HANDLE Thread;
99typedef CRITICAL_SECTION Mutex;
100typedef CONDITION_VARIABLE Condition;
101
102static inline int MutexInit(Mutex* mutex) {
103 InitializeCriticalSection(mutex);
104 return GetLastError();
105}
106
107static inline int MutexDeinit(Mutex* mutex) {
108 DeleteCriticalSection(mutex);
109 return GetLastError();
110}
111
112static inline int MutexLock(Mutex* mutex) {
113 EnterCriticalSection(mutex);
114 return GetLastError();
115}
116
117static inline int MutexUnlock(Mutex* mutex) {
118 LeaveCriticalSection(mutex);
119 return GetLastError();
120}
121
122static inline int ConditionInit(Condition* cond) {
123 InitializeConditionVariable(cond);
124 return GetLastError();
125}
126
127static inline int ConditionDeinit(Condition* cond) {
128 // This is a no-op on Windows
129 UNUSED(cond);
130 return 0;
131}
132
133static inline int ConditionWait(Condition* cond, Mutex* mutex) {
134 SleepConditionVariableCS(cond, mutex, INFINITE);
135 return GetLastError();
136}
137
138static inline int ConditionWaitTimed(Condition* cond, Mutex* mutex, int32_t timeoutMs) {
139 SleepConditionVariableCS(cond, mutex, timeoutMs);
140 return GetLastError();
141}
142
143static inline int ConditionWake(Condition* cond) {
144 WakeAllConditionVariable(cond);
145 return GetLastError();
146}
147
148static inline int ThreadCreate(Thread* thread, ThreadEntry entry, void* context) {
149 *thread = CreateThread(NULL, 0, entry, context, 0, 0);
150 return GetLastError();
151}
152
153static inline int ThreadJoin(Thread thread) {
154 DWORD error = WaitForSingleObject(thread, INFINITE);
155 if (error == WAIT_FAILED) {
156 return GetLastError();
157 }
158 return 0;
159}
160
161static inline int ThreadSetName(const char* name) {
162 return -1;
163}
164#else
165#define DISABLE_THREADING
166#endif
167#endif
168#ifdef DISABLE_THREADING
169typedef void* Thread;
170typedef void* Mutex;
171typedef void* Condition;
172
173static inline int MutexInit(Mutex* mutex) {
174 UNUSED(mutex);
175 return 0;
176}
177
178static inline int MutexDeinit(Mutex* mutex) {
179 UNUSED(mutex);
180 return 0;
181}
182
183static inline int MutexLock(Mutex* mutex) {
184 UNUSED(mutex);
185 return 0;
186}
187
188static inline int MutexUnlock(Mutex* mutex) {
189 UNUSED(mutex);
190 return 0;
191}
192
193static inline int ConditionInit(Condition* cond) {
194 UNUSED(cond);
195 return 0;
196}
197
198static inline int ConditionDeinit(Condition* cond) {
199 UNUSED(cond);
200 return 0;
201}
202
203static inline int ConditionWait(Condition* cond, Mutex* mutex) {
204 UNUSED(cond);
205 UNUSED(mutex);
206 return 0;
207}
208
209static inline int ConditionWaitTimed(Condition* cond, Mutex* mutex, int32_t timeoutMs) {
210 UNUSED(cond);
211 UNUSED(mutex);
212 UNUSED(timeoutMs);
213 return 0;
214}
215
216static inline int ConditionWake(Condition* cond) {
217 UNUSED(cond);
218 return 0;
219}
220#endif
221
222#endif