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
|
#ifndef TH_THREAD_INC
#define TH_THREAD_INC
#include <stdlib.h>
#include <string.h>
/*#include "TH.h" */
#include "THThread.h"
#if defined(USE_PTHREAD_THREADS)
#include <pthread.h>
#elif defined(USE_WIN32_THREADS)
/* very basic emulation to suit our needs */
#include <process.h>
#include <windows.h>
typedef HANDLE pthread_t;
typedef DWORD pthread_attr_t;
typedef HANDLE pthread_mutex_t;
typedef HANDLE pthread_cond_t;
static int pthread_create(pthread_t *restrict thread,
const pthread_attr_t *restrict attr, void *(*start_routine)(void *),
void *restrict arg)
{
*thread = (HANDLE)_beginthreadex(NULL, 0, (THREAD_FUNCTION)start_routine, arg, 0, NULL);
return (int)(*thread == NULL);
}
static int pthread_join(pthread_t thread, void **value_ptr)
{
return ((WaitForSingleObject((thread), INFINITE) != WAIT_OBJECT_0) || !CloseHandle(thread));
}
static int pthread_mutex_init(pthread_mutex_t *restrict mutex,
const pthread_mutexattr_t *restrict attr)
{
*mutex = CreateMutex(NULL, FALSE, NULL);
return (int)(*mutex == NULL);
}
static int pthread_mutex_lock(pthread_mutex_t *mutex)
{
return WaitForSingleObject(*mutex, INFINITE) == 0;
}
static int pthread_mutex_unlock(pthread_mutex_t *mutex)
{
return ReleaseMutex(*mutex) == 0;
}
static int pthread_mutex_destroy(pthread_mutex_t *mutex)
{
return CloseHandle(*mutex) == 0;
}
static int pthread_cond_init(pthread_cond_t *restrict cond,
const pthread_condattr_t *restrict attr)
{
*cond = CreateEvent(NULL, FALSE, FALSE, NULL);
return (int)(*cond == NULL);
}
static int pthread_cond_wait(pthread_cond_t *restrict cond,
pthread_mutex_t *restrict mutex)
{
SignalObjectAndWait(*mutex, *cond, INFINITE, FALSE);
return WaitForSingleObject(*mutex, INFINITE) == 0;
}
static int pthread_cond_destroy(pthread_cond_t *cond)
{
return CloseHandle(*cond) == 0;
}
int pthread_cond_signal(pthread_cond_t *cond)
{
return SetEvent(*cond) == 0;
}
#else
#error no thread system available
#endif
typedef struct THThread_ {
pthread_t id;
int (*func)(void*);
void* data;
int status;
} THThread;
typedef struct THMutex_{
pthread_mutex_t id;
int refcount;
} THMutex;
typedef struct THCondition_ {
pthread_cond_t id;
int refcount;
} THCondition;
typedef struct THSemaphore_ {
pthread_mutex_t mutex_;
pthread_cond_t cond_;
int counter_;
int refcount;
}THSemaphore;
static void* thread_closure(void *data)
{
THThread *thread = data;
thread->status = thread->func(thread->data);
return NULL;
}
THThread* THThread_new(int (*func)(void*), void *data)
{
THThread *self = malloc(sizeof(THThread));
self->func = func;
self->data = data;
self->status = 0;
if(!self)
return NULL;
if(pthread_create(&self->id, NULL, thread_closure, self)) {
free(self);
return NULL;
}
return self;
}
long THThread_id(THThread *self)
{
return (long)(self);
}
int THThread_free(THThread *self)
{
int status = 1;
if(self) {
if(pthread_join(self->id, NULL))
return 1;
status = self->status;
free(self);
self = NULL;
}
return status;
}
THMutex* THMutex_new(void)
{
THMutex *self = malloc(sizeof(THMutex));
if(!self)
return NULL;
if(pthread_mutex_init(&self->id, NULL) != 0) {
free(self);
return NULL;
}
self->refcount = 1;
return self;
}
THMutex* THMutex_newWithId(long id)
{
THMutex *self = (THMutex*)id;
__sync_fetch_and_add(&self->refcount, 1);
return self;
}
long THMutex_id(THMutex *self)
{
return (long)(self);
}
int THMutex_lock(THMutex *self)
{
if(pthread_mutex_lock(&self->id) != 0)
return 1;
return 0;
}
int THMutex_trylock(THMutex *self)
{
if (pthread_mutex_trylock(&self->id) != 0)
return 1;
return 0;
}
int THMutex_unlock(THMutex *self)
{
if(pthread_mutex_unlock(&self->id) != 0)
return 1;
return 0;
}
void THMutex_free(THMutex *self)
{
if(self) {
if(__sync_fetch_and_add(&self->refcount, -1) == 1) {
pthread_mutex_destroy(&self->id);
free(self);
self = NULL;
}
}
}
THCondition* THCondition_new(void)
{
THCondition *self = malloc(sizeof(THCondition));
if(!self)
return NULL;
if(pthread_cond_init(&self->id, NULL)) {
free(self);
return NULL;
}
self->refcount = 1;
return self;
}
THCondition* THCondition_newWithId(long id)
{
THCondition *self = (THCondition*)id;
__sync_fetch_and_add(&self->refcount, 1);
return self;
}
long THCondition_id(THCondition *self)
{
return (long)(self);
}
int THCondition_signal(THCondition *self)
{
if(pthread_cond_signal(&self->id))
return 1;
return 0;
}
int THCondition_wait(THCondition *self, THMutex *mutex)
{
if(pthread_cond_wait(&self->id, &mutex->id))
return 1;
return 0;
}
void THCondition_free(THCondition *self)
{
if(self) {
if(__sync_fetch_and_add(&self->refcount, -1) == 1) {
pthread_cond_destroy(&self->id);
free(self);
self = NULL;
}
}
}
THSemaphore* THSemaphore_new(int initValue)
{
THSemaphore *self = malloc(sizeof(THSemaphore));
if(!self)
return NULL;
self->counter_ = initValue;
if(pthread_mutex_init(&self->mutex_, NULL) != 0)
{
free(self);
return NULL;
}
if(pthread_cond_init(&self->cond_, NULL) != 0)
{
free(self);
return NULL;
}
self->refcount = 1;
return self;
}
THSemaphore* THSemaphore_newWithId(long id)
{
THSemaphore *self = (THSemaphore*)id;
__sync_fetch_and_add(&self->refcount, 1);
return self;
}
long THSemaphore_id(THSemaphore *self)
{
return (long)(self);
}
int THSemaphore_wait(THSemaphore *self)
{
int ret = 0;
ret |= pthread_mutex_lock(&self->mutex_);
while (self->counter_ <= 0)
ret |= pthread_cond_wait(&self->cond_, &self->mutex_);
self->counter_--;
ret |= pthread_mutex_unlock(&self->mutex_);
return ret;
}
int THSemaphore_signal(THSemaphore *self)
{
int ret = 0;
ret |= pthread_mutex_lock(&self->mutex_);
self->counter_++;
ret |= pthread_cond_signal(&self->cond_);
ret |= pthread_mutex_unlock(&self->mutex_);
return ret;
}
int THSemaphore_trywait(THSemaphore *self)
{
int ret = 0;
int try_wait_succeeded = 1;
ret |= pthread_mutex_lock(&self->mutex_);
if (self->counter_ > 0) {
self->counter_--;
try_wait_succeeded = 0;
}
ret |= pthread_mutex_unlock(&self->mutex_);
return try_wait_succeeded;
}
void THSemaphore_free(THSemaphore *self)
{
if(self)
{
if(__sync_fetch_and_add(&self->refcount, -1) == 1)
{
pthread_mutex_destroy(&self->mutex_);
pthread_cond_destroy(&self->cond_);
free(self);
self = NULL;
}
}
}
#endif
|