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
|
#include "common.h"
#include "thread.h"
#include <errno.h>
#include <pthread.h>
#if !HAVE_PTHREAD_YIELD
#include <sched.h>
#endif
#if !HAVE_CLOCK_GETTIME
# include <sys/time.h>
#endif
#ifdef DEBUG
#define ABORTFAIL(x) \
do { \
if ((x)) abort(); \
} while (false)
#else
#define ABORTFAIL(x) (x)
#endif
bool thread_new(thread_run_t run, void* userdata)
{
pthread_t thread;
pthread_attr_t attr;
assert(run);
if (pthread_attr_init(&attr))
{
return NULL;
}
if (pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED))
{
pthread_attr_destroy(&attr);
return NULL;
}
if (pthread_create(&thread, &attr, run, userdata))
{
pthread_attr_destroy(&attr);
return false;
}
pthread_attr_destroy(&attr);
return true;
}
struct thread_t
{
pthread_t thread;
};
thread_t* thread_joinable_new(thread_run_t run, void* userdata)
{
thread_t* ret = malloc(sizeof(thread_t));
pthread_attr_t attr;
assert(run);
if (!ret)
{
return NULL;
}
if (pthread_attr_init(&attr))
{
free(ret);
return NULL;
}
if (pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE))
{
pthread_attr_destroy(&attr);
free(ret);
return NULL;
}
if (pthread_create(&ret->thread, &attr, run, userdata))
{
pthread_attr_destroy(&attr);
free(ret);
return NULL;
}
pthread_attr_destroy(&attr);
return ret;
}
void* thread_join(thread_t* thread)
{
void* ret;
assert(thread);
ABORTFAIL(pthread_join(thread->thread, &ret));
free(thread);
return ret;
}
struct thread_id_t
{
pthread_t thread;
};
thread_id_t* thread_id_new(thread_t* thread)
{
thread_id_t* id = malloc(sizeof(thread_id_t));
if (id)
{
id->thread = thread ? thread->thread : pthread_self();
}
return id;
}
void thread_id_free(thread_id_t* id)
{
free(id);
}
bool thread_id_is_current(thread_id_t* id)
{
assert(id);
return id->thread == pthread_self();
}
void thread_yield(void)
{
#if HAVE_PTHREAD_YIELD
# if PTHREAD_YIELD_VOID
pthread_yield();
# else
ABORTFAIL(pthread_yield());
# endif
#else
ABORTFAIL(sched_yield());
#endif
}
struct thread_lock_t
{
pthread_mutex_t mutex;
};
thread_lock_t* thread_lock_new(void)
{
thread_lock_t* lock = malloc(sizeof(thread_lock_t));
if (!lock)
{
return lock;
}
for (;;)
{
#ifdef DEBUG
pthread_mutexattr_t attr;
if (pthread_mutexattr_init(&attr))
{
break;
}
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK);
if (pthread_mutex_init(&lock->mutex, &attr))
{
pthread_mutexattr_destroy(&attr);
break;
}
pthread_mutexattr_destroy(&attr);
#else
if (pthread_mutex_init(&lock->mutex, NULL))
{
break;
}
#endif
return lock;
}
free(lock);
return NULL;
}
thread_lock_t* thread_lock_recursive_new(void)
{
thread_lock_t* lock = malloc(sizeof(thread_lock_t));
for (;;)
{
if (!lock)
{
break;
}
pthread_mutexattr_t attr;
if (pthread_mutexattr_init(&attr))
{
break;
}
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
if (pthread_mutex_init(&lock->mutex, &attr))
{
pthread_mutexattr_destroy(&attr);
break;
}
pthread_mutexattr_destroy(&attr);
return lock;
}
free(lock);
return NULL;
}
void thread_lock_free(thread_lock_t* lock)
{
if (lock)
{
pthread_mutex_destroy(&lock->mutex);
free(lock);
}
}
void thread_lock_lock(thread_lock_t* lock)
{
assert(lock);
ABORTFAIL(pthread_mutex_lock(&lock->mutex));
}
void thread_lock_unlock(thread_lock_t* lock)
{
assert(lock);
ABORTFAIL(pthread_mutex_unlock(&lock->mutex));
}
struct thread_cond_t
{
pthread_cond_t cond;
};
thread_cond_t* thread_cond_new(void)
{
thread_cond_t* cond = malloc(sizeof(thread_cond_t));
if (cond)
{
if (pthread_cond_init(&cond->cond, NULL))
{
free(cond);
return NULL;
}
}
return cond;
}
void thread_cond_free(thread_cond_t* cond)
{
if (cond)
{
pthread_cond_destroy(&cond->cond);
free(cond);
}
}
void thread_cond_wait(thread_cond_t* cond, thread_lock_t* lock)
{
assert(cond);
assert(lock);
ABORTFAIL(pthread_cond_wait(&cond->cond, &lock->mutex));
}
bool thread_cond_timedwait(thread_cond_t* cond, thread_lock_t* lock,
const struct timespec *abstime)
{
int ret;
assert(cond);
assert(lock);
assert(abstime);
ret = pthread_cond_timedwait(&cond->cond, &lock->mutex, abstime);
if (ret == 0)
{
return true;
}
#ifdef DEBUG
if (ret != ETIMEDOUT)
{
abort();
}
#endif
return false;
}
void thread_cond_signal(thread_cond_t* cond)
{
assert(cond);
ABORTFAIL(pthread_cond_signal(&cond->cond));
}
void thread_cond_broadcast(thread_cond_t* cond)
{
ABORTFAIL(pthread_cond_broadcast(&cond->cond));
}
void thread_abstime(struct timespec *abstime, unsigned long add_ms)
{
#if HAVE_CLOCK_GETTIME
clock_gettime(CLOCK_REALTIME, abstime);
#else
{
struct timeval tv;
gettimeofday(&tv, NULL);
abstime->tv_sec = tv.tv_sec;
abstime->tv_nsec = tv.tv_usec * 1000;
}
#endif
timespec_addms(abstime, add_ms);
}
struct thread_data_t
{
pthread_key_t key;
};
thread_data_t *thread_data_new(thread_data_value_free_t free_value)
{
thread_data_t *data = malloc(sizeof(thread_data_t));
if (data)
{
if (pthread_key_create(&data->key, free_value))
{
free(data);
return NULL;
}
}
return data;
}
void thread_data_free(thread_data_t *data)
{
if (data)
{
pthread_key_delete(data->key);
free(data);
}
}
void *thread_data_value(thread_data_t *data)
{
assert(data);
return pthread_getspecific(data->key);
}
void thread_data_set(thread_data_t *data, void *value)
{
assert(data);
ABORTFAIL(pthread_setspecific(data->key, value));
}
void thread_once(thread_once_t *once, thread_run_once_t run)
{
ABORTFAIL(pthread_once(once, run));
}
|