/
threads.c
417 lines (345 loc) · 9.77 KB
/
threads.c
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/* ______ ___ ___
* /\ _ \ /\_ \ /\_ \
* \ \ \L\ \\//\ \ \//\ \ __ __ _ __ ___
* \ \ __ \ \ \ \ \ \ \ /'__`\ /'_ `\/\`'__\/ __`\
* \ \ \/\ \ \_\ \_ \_\ \_/\ __//\ \L\ \ \ \//\ \L\ \
* \ \_\ \_\/\____\/\____\ \____\ \____ \ \_\\ \____/
* \/_/\/_/\/____/\/____/\/____/\/___L\ \/_/ \/___/
* /\____/
* \_/__/
*
* Public threads interface.
*
* By Peter Wang.
*
* See readme.txt for copyright information.
*/
#include "allegro5/allegro.h"
#include "allegro5/internal/aintern.h"
#include "allegro5/internal/aintern_thread.h"
#include "allegro5/internal/aintern_system.h"
typedef enum THREAD_STATE {
THREAD_STATE_CREATED, /* -> starting or -> joining */
THREAD_STATE_STARTING, /* -> started */
THREAD_STATE_STARTED, /* -> joining */
THREAD_STATE_JOINING, /* -> joined */
THREAD_STATE_JOINED, /* -> destroyed */
THREAD_STATE_DESTROYED,
THREAD_STATE_DETACHED
} THREAD_STATE;
struct ALLEGRO_THREAD {
_AL_THREAD thread;
_AL_MUTEX mutex;
_AL_COND cond;
THREAD_STATE thread_state;
void *proc;
void *arg;
void *retval;
};
struct ALLEGRO_MUTEX {
_AL_MUTEX mutex;
};
struct ALLEGRO_COND {
_AL_COND cond;
};
static void thread_func_trampoline(_AL_THREAD *inner, void *_outer)
{
ALLEGRO_THREAD *outer = (ALLEGRO_THREAD *) _outer;
ALLEGRO_SYSTEM *system = al_get_system_driver();
(void)inner;
if (system && system->vt && system->vt->thread_init) {
system->vt->thread_init(outer);
}
/* Wait to start the actual user thread function. The thread could also be
* destroyed before ever running the user function.
*/
_al_mutex_lock(&outer->mutex);
while (outer->thread_state == THREAD_STATE_CREATED) {
_al_cond_wait(&outer->cond, &outer->mutex);
}
_al_mutex_unlock(&outer->mutex);
if (outer->thread_state == THREAD_STATE_STARTING) {
outer->thread_state = THREAD_STATE_STARTED;
outer->retval =
((void *(*)(ALLEGRO_THREAD *, void *))outer->proc)(outer, outer->arg);
}
if (system && system->vt && system->vt->thread_exit) {
system->vt->thread_exit(outer);
}
}
static void detached_thread_func_trampoline(_AL_THREAD *inner, void *_outer)
{
ALLEGRO_THREAD *outer = (ALLEGRO_THREAD *) _outer;
(void)inner;
((void *(*)(void *))outer->proc)(outer->arg);
al_free(outer);
}
static ALLEGRO_THREAD *create_thread(void)
{
ALLEGRO_THREAD *outer;
outer = al_malloc(sizeof(*outer));
if (!outer) {
return NULL;
}
_AL_MARK_MUTEX_UNINITED(outer->mutex); /* required */
outer->retval = NULL;
return outer;
}
/* Function: al_create_thread
*/
ALLEGRO_THREAD *al_create_thread(
void *(*proc)(ALLEGRO_THREAD *thread, void *arg), void *arg)
{
ALLEGRO_THREAD *outer = create_thread();
outer->thread_state = THREAD_STATE_CREATED;
_al_mutex_init(&outer->mutex);
_al_cond_init(&outer->cond);
outer->arg = arg;
outer->proc = proc;
_al_thread_create(&outer->thread, thread_func_trampoline, outer);
/* XXX _al_thread_create should return an error code */
return outer;
}
/* Function: al_create_thread_with_stacksize
*/
ALLEGRO_THREAD *al_create_thread_with_stacksize(
void *(*proc)(ALLEGRO_THREAD *thread, void *arg), void *arg, size_t stacksize)
{
ALLEGRO_THREAD *outer = create_thread();
outer->thread_state = THREAD_STATE_CREATED;
_al_mutex_init(&outer->mutex);
_al_cond_init(&outer->cond);
outer->arg = arg;
outer->proc = proc;
_al_thread_create_with_stacksize(&outer->thread, thread_func_trampoline, outer, stacksize);
/* XXX _al_thread_create should return an error code */
return outer;
}
/* Function: al_run_detached_thread
*/
void al_run_detached_thread(void *(*proc)(void *arg), void *arg)
{
ALLEGRO_THREAD *outer = create_thread();
outer->thread_state = THREAD_STATE_DETACHED;
outer->arg = arg;
outer->proc = proc;
_al_thread_create(&outer->thread, detached_thread_func_trampoline, outer);
_al_thread_detach(&outer->thread);
}
/* Function: al_start_thread
*/
void al_start_thread(ALLEGRO_THREAD *thread)
{
ASSERT(thread);
switch (thread->thread_state) {
case THREAD_STATE_CREATED:
_al_mutex_lock(&thread->mutex);
thread->thread_state = THREAD_STATE_STARTING;
_al_cond_broadcast(&thread->cond);
_al_mutex_unlock(&thread->mutex);
break;
case THREAD_STATE_STARTING:
break;
case THREAD_STATE_STARTED:
break;
/* invalid cases */
case THREAD_STATE_JOINING:
ASSERT(thread->thread_state != THREAD_STATE_JOINING);
break;
case THREAD_STATE_JOINED:
ASSERT(thread->thread_state != THREAD_STATE_JOINED);
break;
case THREAD_STATE_DESTROYED:
ASSERT(thread->thread_state != THREAD_STATE_DESTROYED);
break;
case THREAD_STATE_DETACHED:
ASSERT(thread->thread_state != THREAD_STATE_DETACHED);
break;
}
}
/* Function: al_join_thread
*/
void al_join_thread(ALLEGRO_THREAD *thread, void **ret_value)
{
ASSERT(thread);
/* If al_join_thread() is called soon after al_start_thread(), the thread
* function may not yet have noticed the STARTING state and executed the
* user's thread function. Hence we must wait until the thread enters the
* STARTED state.
*/
while (thread->thread_state == THREAD_STATE_STARTING) {
al_rest(0.001);
}
switch (thread->thread_state) {
case THREAD_STATE_CREATED: /* fall through */
case THREAD_STATE_STARTED:
_al_mutex_lock(&thread->mutex);
thread->thread_state = THREAD_STATE_JOINING;
_al_cond_broadcast(&thread->cond);
_al_mutex_unlock(&thread->mutex);
_al_cond_destroy(&thread->cond);
_al_mutex_destroy(&thread->mutex);
_al_thread_join(&thread->thread);
thread->thread_state = THREAD_STATE_JOINED;
break;
case THREAD_STATE_STARTING:
ASSERT(thread->thread_state != THREAD_STATE_STARTING);
break;
case THREAD_STATE_JOINING:
ASSERT(thread->thread_state != THREAD_STATE_JOINING);
break;
case THREAD_STATE_JOINED:
ASSERT(thread->thread_state != THREAD_STATE_JOINED);
break;
case THREAD_STATE_DESTROYED:
ASSERT(thread->thread_state != THREAD_STATE_DESTROYED);
break;
case THREAD_STATE_DETACHED:
ASSERT(thread->thread_state != THREAD_STATE_DETACHED);
break;
}
if (ret_value) {
*ret_value = thread->retval;
}
}
/* Function: al_set_thread_should_stop
*/
void al_set_thread_should_stop(ALLEGRO_THREAD *thread)
{
ASSERT(thread);
_al_thread_set_should_stop(&thread->thread);
}
/* Function: al_get_thread_should_stop
*/
bool al_get_thread_should_stop(ALLEGRO_THREAD *thread)
{
ASSERT(thread);
return _al_get_thread_should_stop(&thread->thread);
}
/* Function: al_destroy_thread
*/
void al_destroy_thread(ALLEGRO_THREAD *thread)
{
if (!thread) {
return;
}
/* Join if required. */
switch (thread->thread_state) {
case THREAD_STATE_CREATED: /* fall through */
case THREAD_STATE_STARTING: /* fall through */
case THREAD_STATE_STARTED:
al_join_thread(thread, NULL);
break;
case THREAD_STATE_JOINING:
ASSERT(thread->thread_state != THREAD_STATE_JOINING);
break;
case THREAD_STATE_JOINED:
break;
case THREAD_STATE_DESTROYED:
ASSERT(thread->thread_state != THREAD_STATE_DESTROYED);
break;
case THREAD_STATE_DETACHED:
ASSERT(thread->thread_state != THREAD_STATE_DETACHED);
break;
}
/* May help debugging. */
thread->thread_state = THREAD_STATE_DESTROYED;
al_free(thread);
}
/* Function: al_create_mutex
*/
ALLEGRO_MUTEX *al_create_mutex(void)
{
ALLEGRO_MUTEX *mutex = al_malloc(sizeof(*mutex));
if (mutex) {
_AL_MARK_MUTEX_UNINITED(mutex->mutex);
_al_mutex_init(&mutex->mutex);
}
return mutex;
}
/* Function: al_create_mutex_recursive
*/
ALLEGRO_MUTEX *al_create_mutex_recursive(void)
{
ALLEGRO_MUTEX *mutex = al_malloc(sizeof(*mutex));
if (mutex) {
_AL_MARK_MUTEX_UNINITED(mutex->mutex);
_al_mutex_init_recursive(&mutex->mutex);
}
return mutex;
}
/* Function: al_lock_mutex
*/
void al_lock_mutex(ALLEGRO_MUTEX *mutex)
{
ASSERT(mutex);
_al_mutex_lock(&mutex->mutex);
}
/* Function: al_unlock_mutex
*/
void al_unlock_mutex(ALLEGRO_MUTEX *mutex)
{
ASSERT(mutex);
_al_mutex_unlock(&mutex->mutex);
}
/* Function: al_destroy_mutex
*/
void al_destroy_mutex(ALLEGRO_MUTEX *mutex)
{
if (mutex) {
_al_mutex_destroy(&mutex->mutex);
al_free(mutex);
}
}
/* Function: al_create_cond
*/
ALLEGRO_COND *al_create_cond(void)
{
ALLEGRO_COND *cond = al_malloc(sizeof(*cond));
if (cond) {
_al_cond_init(&cond->cond);
}
return cond;
}
/* Function: al_destroy_cond
*/
void al_destroy_cond(ALLEGRO_COND *cond)
{
if (cond) {
_al_cond_destroy(&cond->cond);
al_free(cond);
}
}
/* Function: al_wait_cond
*/
void al_wait_cond(ALLEGRO_COND *cond, ALLEGRO_MUTEX *mutex)
{
ASSERT(cond);
ASSERT(mutex);
_al_cond_wait(&cond->cond, &mutex->mutex);
}
/* Function: al_wait_cond_until
*/
int al_wait_cond_until(ALLEGRO_COND *cond, ALLEGRO_MUTEX *mutex,
const ALLEGRO_TIMEOUT *timeout)
{
ASSERT(cond);
ASSERT(mutex);
ASSERT(timeout);
return _al_cond_timedwait(&cond->cond, &mutex->mutex, timeout);
}
/* Function: al_broadcast_cond
*/
void al_broadcast_cond(ALLEGRO_COND *cond)
{
ASSERT(cond);
_al_cond_broadcast(&cond->cond);
}
/* Function: al_signal_cond
*/
void al_signal_cond(ALLEGRO_COND *cond)
{
ASSERT(cond);
_al_cond_signal(&cond->cond);
}
/* vim: set sts=3 sw=3 et: */