-
Notifications
You must be signed in to change notification settings - Fork 3
/
threads.h
213 lines (181 loc) · 8.51 KB
/
threads.h
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
/*
threads.h - Cross platform thread macros for C
Written in 2011-2018 Steve "Sc00bz" Thomas (steve at tobtu dot com)
To the extent possible under law, the author(s) have dedicated all copyright and related and neighboring
rights to this software to the public domain worldwide. This software is distributed without any warranty.
You should have received a copy of the CC0 Public Domain Dedication along with this software.
If not, see <https://creativecommons.org/publicdomain/zero/1.0/>.
*/
#pragma once
#ifdef _WIN32
#include <windows.h>
typedef HANDLE THREAD;
typedef CRITICAL_SECTION MUTEX;
typedef CRITICAL_SECTION *PMUTEX;
#define THREAD_WAIT(thread) WaitForSingleObject(thread, INFINITE)
#define THREAD_CREATE(thread,func,arg) ((thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) func, arg, 0, NULL)) == NULL ? -1 : 0)
#define MUTEX_CREATE(mutex) InitializeCriticalSection(&mutex)
#define MUTEX_DELETE(mutex) DeleteCriticalSection(&mutex)
#define MUTEX_LOCK(mutex) EnterCriticalSection(&mutex)
#define MUTEX_TRY_LOCK(mutex) (TryEnterCriticalSection(&mutex) != 0)
#define MUTEX_UNLOCK(mutex) LeaveCriticalSection(&mutex)
#define PMUTEX_CREATE(pmutex) InitializeCriticalSection(pmutex = new CRITICAL_SECTION)
#define PMUTEX_DELETE(pmutex) do \
{ \
DeleteCriticalSection(pmutex); \
delete pmutex; \
} while (0)
#define PMUTEX_LOCK(pmutex) EnterCriticalSection(pmutex)
#define PMUTEX_TRY_LOCK(pmutex) (TryEnterCriticalSection(pmutex) != 0)
#define PMUTEX_UNLOCK(pmutex) LeaveCriticalSection(pmutex)
#if (_WIN32_WINNT >= _WIN32_WINNT_VISTA && !defined(THREADS_WIN_XP_FTW))
typedef CONDITION_VARIABLE COND;
typedef CONDITION_VARIABLE *PCOND;
#define COND_CREATE(cond) InitializeConditionVariable(&cond)
#define COND_DELETE(cond) /* Memory leak? */
#define COND_SIGNAL(cond) WakeConditionVariable(&cond)
#define COND_SIGNAL_ALL(cond) WakeAllConditionVariable(&cond)
#define COND_WAIT(cond,mutex) SleepConditionVariableCS(&cond, &mutex, INFINITE)
#define PCOND_CREATE(pcond) InitializeConditionVariable(pcond = new CONDITION_VARIABLE)
#define PCOND_DELETE(pcond) /* Memory leak? */delete pcond
#define PCOND_SIGNAL(pcond) WakeConditionVariable(pcond)
#define PCOND_SIGNAL_ALL(pcond) WakeAllConditionVariable(pcond)
#define PCOND_WAIT(pcond,pmutex) SleepConditionVariableCS(pcond, pmutex, INFINITE)
#else
typedef HANDLE COND;
typedef HANDLE PCOND;
#define COND_CREATE(cond) (cond = CreateEvent(NULL, TRUE, FALSE, NULL))
#define COND_DELETE(cond) CloseHandle(cond)
#define COND_SIGNAL(cond) SetEvent(cond)
#define COND_SIGNAL_ALL(cond) SetEvent(cond)
#define COND_WAIT(cond,mutex) do \
{ \
ResetEvent(cond); \
MUTEX_UNLOCK(mutex); \
WaitForSingleObject(cond, 1000 /* 1 second because of race condition */); \
MUTEX_LOCK(mutex); \
} while (0)
#define PCOND_CREATE(pcond) (pcond = CreateEvent(NULL, TRUE, FALSE, NULL))
#define PCOND_DELETE(pcond) CloseHandle(pcond)
#define PCOND_SIGNAL(pcond) SetEvent(pcond)
#define PCOND_SIGNAL_ALL(pcond) SetEvent(pcond)
#define PCOND_WAIT(pcond,pmutex) do \
{ \
ResetEvent(pcond); \
PMUTEX_UNLOCK(pmutex); \
WaitForSingleObject(pcond, 1000 /* 1 second because of race condition */); \
PMUTEX_LOCK(pmutex); \
} while (0)
#endif
inline int getNumCores()
{
SYSTEM_INFO sysinfo;
GetSystemInfo(&sysinfo);
return (int) sysinfo.dwNumberOfProcessors;
}
inline bool threadPriorityIncrease(THREAD thread)
{
// Yes there are more but just going with these:
// THREAD_PRIORITY_HIGHEST 2
// THREAD_PRIORITY_ABOVE_NORMAL 1
// THREAD_PRIORITY_NORMAL 0
// THREAD_PRIORITY_BELOW_NORMAL -1
// THREAD_PRIORITY_LOWEST -2
int priority;
int ret = 0;
priority = GetThreadPriority(thread);
if (priority != THREAD_PRIORITY_ERROR_RETURN &&
priority >= THREAD_PRIORITY_LOWEST &&
priority < THREAD_PRIORITY_HIGHEST)
{
ret = SetThreadPriority(thread, priority + 1);
}
return ret == 0;
}
inline bool threadPriorityDecrease(THREAD thread)
{
int priority;
int ret = 0;
priority = GetThreadPriority(thread);
if (priority != THREAD_PRIORITY_ERROR_RETURN &&
priority > THREAD_PRIORITY_LOWEST &&
priority <= THREAD_PRIORITY_HIGHEST)
{
ret = SetThreadPriority(thread, priority - 1);
}
return ret == 0;
}
#else
#include <unistd.h>
#include <pthread.h>
#include <sched.h>
typedef pthread_t THREAD;
typedef pthread_mutex_t MUTEX;
typedef pthread_mutex_t *PMUTEX;
typedef pthread_cond_t COND;
typedef pthread_cond_t *PCOND;
#define THREAD_WAIT(thread) pthread_join(thread, NULL)
#define THREAD_CREATE(thread,func,arg) pthread_create(&thread, NULL, func, arg)
#define MUTEX_CREATE(mutex) pthread_mutex_init(&mutex, NULL)
#define MUTEX_DELETE(mutex) pthread_mutex_destroy(&mutex)
#define MUTEX_LOCK(mutex) pthread_mutex_lock(&mutex)
#define MUTEX_TRY_LOCK(mutex) (pthread_mutex_trylock(&mutex) == 0)
#define MUTEX_UNLOCK(mutex) pthread_mutex_unlock(&mutex)
#define PMUTEX_CREATE(pmutex) pthread_mutex_init(pmutex = new pthread_mutex_t, NULL)
#define PMUTEX_DELETE(pmutex) do \
{ \
pthread_mutex_destroy(pmutex); \
delete pmutex; \
} while (0)
#define PMUTEX_LOCK(pmutex) pthread_mutex_lock(pmutex)
#define PMUTEX_TRY_LOCK(pmutex) (pthread_mutex_trylock(pmutex) == 0)
#define PMUTEX_UNLOCK(pmutex) pthread_mutex_unlock(pmutex)
#define COND_CREATE(cond) pthread_cond_init(&cond, NULL)
#define COND_DELETE(cond) pthread_cond_destroy(&cond)
#define COND_SIGNAL(cond) pthread_cond_signal(&cond)
#define COND_SIGNAL_ALL(cond) pthread_cond_broadcast(&cond)
#define COND_WAIT(cond,mutex) pthread_cond_wait(&cond, &mutex)
#define PCOND_CREATE(pcond) pthread_cond_init(pcond = new pthread_cond_t, NULL)
#define PCOND_DELETE(pcond) do \
{ \
pthread_cond_destroy(pcond); \
delete pcond; \
} while (0)
#define PCOND_SIGNAL(pcond) pthread_cond_signal(pcond)
#define PCOND_SIGNAL_ALL(pcond) pthread_cond_broadcast(pcond)
#define PCOND_WAIT(pcond,pmutex) pthread_cond_wait(pcond, pmutex)
inline int getNumCores()
{
return (int) sysconf(_SC_NPROCESSORS_ONLN);
}
inline bool threadPriorityIncrease(THREAD thread)
{
sched_param param;
int policy;
int ret = 1;
if (pthread_getschedparam(thread, &policy, ¶m) == 0)
{
int limit = sched_get_priority_max(policy);
if (limit != -1 && param.sched_priority < limit)
{
ret = pthread_setschedprio(thread, param.sched_priority + 1);
}
}
return ret != 0;
}
inline bool threadPriorityDecrease(THREAD thread)
{
sched_param param;
int policy;
int ret = 1;
if (pthread_getschedparam(thread, &policy, ¶m) == 0)
{
int limit = sched_get_priority_min(policy);
if (limit != -1 && param.sched_priority > limit)
{
ret = pthread_setschedprio(thread, param.sched_priority - 1);
}
}
return ret != 0;
}
#endif