aboutsummaryrefslogtreecommitdiff
path: root/src/thread_win.c
blob: bb1477462bc92f7a49db6c0c2b106de13d99ad4f (plain)
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
#include <bh/internal/thread.h>

static unsigned __stdcall bh_thread_run(void *arg)
{
    bh_thread_data_t data;

    /* Fetch thread data, store it on stack and free from heap */
    data = *(bh_thread_data_t *)arg;
    free(arg);

    /* Do the task, mark as done, and if required free it */
    data.task->func(data.task->data);
    data.task->flags |= BH_THREAD_DONE;

    if (data.task->flags & BH_THREAD_CLEANUP)
        bh_task_free(data.task);

    /* Call thread specific end function (deallocate TLS) */
    data.end(0);
    return 0;
}

int bh_thread_init_base(bh_thread_t *thread,
                        bh_task_t *task,
                        bh_thread_begin_cb_t begin,
                        bh_thread_end_cb_t end)
{
    bh_thread_data_t *data;

    /* Allocate thread specific data */
    data = malloc(sizeof(*data));
    if (!data)
        return BH_OOM;

    /* Setup thread specific data */
    data->task = task;
    data->end = end;

    /* Create and setup thread (relative to the callers libc) */
    thread->allocated = 0;
    thread->handle = (HANDLE)_beginthreadex(NULL, 0, bh_thread_run, data, 0, NULL);

    /* Check for errors */
    if (!thread->handle)
    {
        free(data);
        return BH_ERROR;
    }

    return BH_OK;
}

bh_thread_t *bh_thread_new_base(bh_task_t *task,
                                bh_thread_begin_cb_t begin,
                                bh_thread_end_cb_t end)
{
    bh_thread_t *result;

    /* Allocate thread object */
    result = malloc(sizeof(*result));
    if (result && !bh_thread_init_base(result, task, begin, end))
    {
        free(result);
        result = NULL;
    }

    /* Mark thread as allocated for deallocation in join/detach */
    if (result)
        result->allocated = 1;

    return result;
}

int bh_thread_join(bh_thread_t *thread)
{
    /* Join the thread */
    WaitForSingleObject(thread->handle, INFINITE);
    CloseHandle(thread->handle);

    /* If thread is allocated, deallocate it */
    if (thread->allocated)
        free(thread);

    return BH_OK;
}

int bh_thread_detach(bh_thread_t *thread)
{
    /* Detach from thread */
    CloseHandle(thread->handle);

    /* If thread is allocated, deallocate it */
    if (thread->allocated)
        free(thread);

    return BH_OK;
}

int bh_mutex_init(bh_mutex_t *mutex)
{
    /* TODO: Is this spincount needed or sane? */
    if (!InitializeCriticalSectionAndSpinCount(&mutex->handle, 0x400))
        return BH_ERROR;
    return BH_OK;
}

void bh_mutex_destroy(bh_mutex_t *mutex)
{
    DeleteCriticalSection(&mutex->handle);
}

int bh_mutex_lock(bh_mutex_t *mutex)
{
    EnterCriticalSection(&mutex->handle);
    return BH_OK;
}

int bh_mutex_try_lock(bh_mutex_t *mutex)
{
    if (!TryEnterCriticalSection(&mutex->handle))
        return BH_ERROR;
    return BH_OK;
}

int bh_mutex_unlock(bh_mutex_t *mutex)
{
    LeaveCriticalSection(&mutex->handle);
    return BH_OK;
}

int bh_semaphore_init(bh_semaphore_t *semaphore, int count)
{
    /* Create semaphore with max value of 32767 (to match POSIX) */
    semaphore->handle = CreateSemaphore(NULL, count, 0x7FFF, NULL);
    if (!semaphore->handle)
        return BH_ERROR;

    return BH_OK;
}

void bh_semaphore_destroy(bh_semaphore_t *semaphore)
{
    CloseHandle(semaphore->handle);
}

int bh_semaphore_post(bh_semaphore_t *semaphore)
{
    if (!ReleaseSemaphore(semaphore->handle, 1, NULL))
        return BH_ERROR;
    return BH_OK;
}

int bh_semaphore_wait(bh_semaphore_t *semaphore)
{
    if (WaitForSingleObject(semaphore->handle, INFINITE))
        return BH_ERROR;
    return BH_OK;
}

int bh_semaphore_wait_for(bh_semaphore_t *semaphore,
                          unsigned long timeout)
{
    /* FIXME: Check if we timed out or errored out */
    if (WaitForSingleObject(semaphore->handle, timeout))
        return BH_TIMEOUT;
    return BH_ERROR;
}

int bh_semaphore_try_wait(bh_semaphore_t *semaphore)
{
    if (WaitForSingleObject(semaphore->handle, 0))
        return BH_ERROR;
    return BH_OK;
}

#if WINVER >= _WIN32_WINNT_VISTA
int bh_cond_init(bh_cond_t *cond)
{
    InitializeConditionVariable(&cond->handle);
    return BH_OK;
}

void bh_cond_destroy(bh_cond_t *cond)
{
    (void)cond;
}

int bh_cond_wait(bh_cond_t *cond,
                 bh_mutex_t *mutex)
{
    return bh_cond_wait_for(cond, mutex, INFINITE);
}

int bh_cond_wait_for(bh_cond_t *cond,
                     bh_mutex_t *mutex,
                     unsigned long timeout)
{
    /* FIXME: Check if we timed out or errored out */
    if (!SleepConditionVariableCS(&cond->handle, &mutex->handle, timeout))
        return BH_TIMEOUT;
    return BH_OK;
}

int bh_cond_signal(bh_cond_t *cond)
{
    WakeConditionVariable(&cond->handle);
    return BH_OK;
}

int bh_cond_broadcast(bh_cond_t *cond)
{
    WakeAllConditionVariable(&cond->handle);
    return BH_OK;
}
#else
/* Condition variable implementation based on BeOS article
 * http://www-classic.be.com/aboutbe/benewsletter/volume_III/Issue40.html
 *
 * Slow, but correct implementation of CVs.
 */

int bh_cond_init(bh_cond_t *cond)
{
    if (bh_mutex_init(&cond->lock))
        return BH_ERROR;

    if (bh_semaphore_init(&cond->wait, 0))
    {
        bh_mutex_destroy(&cond->lock);
        return BH_ERROR;
    }

    if (bh_semaphore_init(&cond->done, 0))
    {
        bh_semaphore_destroy(&cond->wait);
        bh_mutex_destroy(&cond->lock);
        return BH_ERROR;
    }
    cond->waiting = 0;
    cond->signals = 0;

    return 0;
}

void bh_cond_destroy(bh_cond_t *cond)
{
    bh_semaphore_destroy(&cond->done);
    bh_semaphore_destroy(&cond->wait);
    bh_mutex_destroy(&cond->lock);
}

int bh_cond_wait(bh_cond_t *cond,
                 bh_mutex_t *mutex)
{
    int retval;

    bh_mutex_lock(&cond->lock);
    cond->waiting++;
    bh_mutex_unlock(&cond->lock);
    bh_mutex_unlock(mutex);

    retval = bh_semaphore_wait(&cond->wait);

    bh_mutex_lock(&cond->lock);
    if (cond->signals > 0)
    {
        if (retval)
            bh_semaphore_wait(&cond->wait);

        bh_semaphore_post(&cond->done);
        cond->signals--;
    }
    cond->waiting--;
    bh_mutex_unlock(&cond->lock);
    bh_mutex_lock(mutex);

    return retval;
}

int bh_cond_wait_for(bh_cond_t *cond,
                     bh_mutex_t *mutex,
                     unsigned long timeout)
{
    int retval;

    bh_mutex_lock(&cond->lock);
    cond->waiting++;
    bh_mutex_unlock(&cond->lock);
    bh_mutex_unlock(mutex);

    retval = bh_semaphore_wait_for(&cond->wait, timeout);

    bh_mutex_lock(&cond->lock);
    if (cond->signals > 0)
    {
        if (retval)
            bh_semaphore_wait(&cond->wait);

        bh_semaphore_post(&cond->done);
        cond->signals--;
    }
    cond->waiting--;
    bh_mutex_unlock(&cond->lock);
    bh_mutex_lock(mutex);

    return retval;
}

int bh_cond_signal(bh_cond_t *cond)
{
    bh_mutex_lock(&cond->lock);

    if (cond->waiting > cond->signals)
    {
        cond->signals++;

        bh_semaphore_post(&cond->wait);
        bh_mutex_unlock(&cond->lock);
        bh_semaphore_wait(&cond->done);
    }
    else
        bh_mutex_unlock(&cond->lock);

    return BH_OK;
}

int bh_cond_broadcast(bh_cond_t *cond)
{
    int i, waiting;

    bh_mutex_lock(&cond->lock);
    if (cond->waiting > cond->signals)
    {
        waiting = cond->waiting - cond->signals;
        cond->signals = cond->waiting;

        for (i = 0; i < waiting; i++)
            bh_semaphore_post(&cond->wait);

        bh_mutex_unlock(&cond->lock);

        for (i = 0; i < waiting; i++)
            bh_semaphore_wait(&cond->done);
    }
    else
        bh_mutex_unlock(&cond->lock);

    return BH_OK;
}
#endif

int bh_thread_pool_init_base(bh_thread_pool_t *pool,
                             size_t size,
                             bh_thread_begin_cb_t begin,
                             bh_thread_end_cb_t end)
{
    size_t i;
    static bh_task_t pool_task;
    static int pool_task_init = 0;

    /* Initialize static thread pool task */
    if (!pool_task_init)
    {
        bh_task_init(&pool_task, bh_thread_pool_worker, pool, 0);
        pool_task_init = 1;
    }

    /* Zero out pool structure */
    memset(pool, 0, sizeof(*pool));

    /* Initialize mutex and condition variables */
    if (bh_mutex_init(&pool->lock))
        goto lock_fail;

    if (bh_cond_init(&pool->new_task))
        goto task_fail;

    if (bh_cond_init(&pool->done_task))
        goto done_fail;

    /* Allocate array of threads */
    pool->threads = malloc(size * sizeof(bh_thread_t));
    pool->size = size;

    if (!pool->threads)
        goto thread_fail;

    /* Initialize queue */
    bh_queue_init(&pool->tasks);
    if (bh_queue_reserve(&pool->tasks, 64))
        goto queue_fail;

    /* Initialize threads */
    for (i = 0; i < size; i++)
    {
        if (bh_thread_init_base(&pool->threads[i], &pool_task, begin, end))
        {
            pool->shutdown = 1;
            bh_cond_broadcast(&pool->new_task);
            for(; i; i--)
                bh_thread_join(&pool->threads[i - 1]);

            bh_queue_destroy(&pool->tasks);
            goto queue_fail;
        }
    }

    return BH_OK;

queue_fail:
    free(pool->threads);

thread_fail:
    bh_cond_destroy(&pool->done_task);

done_fail:
    bh_cond_destroy(&pool->new_task);

task_fail:
    bh_mutex_destroy(&pool->lock);

lock_fail:
    return BH_ERROR;
}

bh_thread_pool_t *bh_thread_pool_new_base(size_t size,
                                          bh_thread_begin_cb_t begin,
                                          bh_thread_end_cb_t end)
{
    bh_thread_pool_t *result;

    result = malloc(sizeof(*result));
    if (result && bh_thread_pool_init_base(result, size, begin, end))
    {
        free(result);
        result = NULL;
    }

    return result;
}