aboutsummaryrefslogtreecommitdiff
path: root/src/thread.c
blob: c3e536c7e04b3643d15c1afb8df259cc89a3ce85 (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
#include <bh/internal/thread.h>
#include <stdlib.h>

/**
 * \defgroup thread Multithreading
 *
 * Multithreading API
 * \{
 */

/**
 * Creates new task object with specified function \a func, \a data and \a
 * flags.
 *
 * If the \a flags contain BH_THREAD_CLEANUP, upon successful completion task
 * will be automaticly destroyed.
 *
 * \param func  Task's function
 * \param data  Task's data
 * \param flags Task's flags
 *
 * \return On success, returns new task object.
 * \return On failure, returns null pointer.
 */
bh_task_t *bh_task_new(void (*func)(void *),
                       void *data,
                       int flags)
{
    bh_task_t *result;

    result = malloc(sizeof(bh_task_t));
    if (result)
        bh_task_init(result, func, data, flags);

    return result;
}

/**
 * Frees the \a task object.
 *
 * \param task  Pointer to the task object.
 */
void bh_task_free(bh_task_t *task)
{
    bh_task_destroy(task);
    free(task);
}

/**
 * Initializes the \a task object with specified function \a func, \a data and
 * \a flags.
 *
 * If the \a flags contain BH_THREAD_CLEANUP, upon successful completion task
 * will be automaticly destroyed.
 *
 * \warning This is an internal function.
 *
 * \param task  Pointer to the task object
 * \param func  Task's function
 * \param data  Task's data
 * \param flags Task's flags
 */
void bh_task_init(bh_task_t *task,
                  void (*func)(void *),
                  void *data,
                  int flags)
{
    task->func = func;
    task->data = data;
    task->flags = flags;
}

/**
 * Destroyes the \a task object.
 *
 * \warning This is an internal function.
 *
 * \param task  Pointer to the task object
 */
void bh_task_destroy(bh_task_t *task)
{
    (void)task;
}

/**
 * Reuses the \a task object for different purpose.
 *
 * \param task  Pointer to the task object
 * \param func  New task's function
 * \param data  New task's data
 */
void bh_task_reuse(bh_task_t *task,
                   void (*func)(void *),
                   void *data)
{
    /* If the task is done - reinitilize it with new data */
    if (task->flags & BH_THREAD_DONE)
        bh_task_init(task, func, data, task->flags & ~(BH_THREAD_DONE));
}

/**
 * Checks if the \a task is done.
 *
 * \param task  Pointer to the task object
 *
 * \return If the task is done, returns non-zero.
 * \return If the task is not done, returns zero.
 */
int bh_task_done(bh_task_t *task)
{
    return (task->flags & BH_THREAD_DONE) != 0;
}

/**
 * Creates the mutex object.
 *
 * \return On success, returns new mutex object.
 * \return On failure, returns null pointer.
 */
bh_mutex_t *bh_mutex_new(void)
{
    bh_mutex_t *result;

    result = malloc(sizeof(*result));
    if (result && bh_mutex_init(result))
    {
        free(result);
        result = NULL;
    }

    return result;
}

/**
 * Frees the \a mutex object.
 *
 * \param mutex Pointer to the mutex object
 */
void bh_mutex_free(bh_mutex_t *mutex)
{
    bh_mutex_destroy(mutex);
    free(mutex);
}

/**
 * Creates the semaphore object with initial \a count value.
 *
 * \param count Initial semaphore value
 *
 * \return On success, returns new semaphore object.
 * \return On failure, returns null pointer.
 *
 * \note Guranteed maximum \a count value is 32767.
 */
bh_semaphore_t *bh_semaphore_new(int count)
{
    bh_semaphore_t *result;

    result = malloc(sizeof(*result));
    if (result && bh_semaphore_init(result, count))
    {
        free(result);
        result = NULL;
    }

    return result;
}

/**
 * Frees the \a semaphore object.
 *
 * \param semaphore Pointer to the semaphore object
 */
void bh_semaphore_free(bh_semaphore_t *semaphore)
{
    bh_semaphore_destroy(semaphore);
    free(semaphore);
}

/**
 * Creates the condition variable object.
 *
 * \return On success, returns new condition variable object.
 * \return On failure, returns null pointer.
 */
bh_cond_t *bh_cond_new(void)
{
    bh_cond_t *result;

    result = malloc(sizeof(*result));
    if (result && bh_cond_init(result))
    {
        free(result);
        result = NULL;
    }

    return result;
}

/**
 * Frees the condition variable object \a cond.
 *
 * \param cond Pointer to the condition variable object
 */
void bh_cond_free(bh_cond_t *cond)
{
    bh_cond_destroy(cond);
    free(cond);
}

/**
 * Adds \a task to the thread \a pool.
 *
 * \param pool  Pointer to the thread pool object
 * \param task  Pointer to the task object
 *
 * \return On success, returns zero.
 * \return On failure, returns error code.
 */
int bh_thread_pool_add(bh_thread_pool_t *pool,
                       bh_task_t *task)
{
    /* Queue task for execution */
    bh_mutex_lock(&pool->lock);
    if (bh_queue_insert(&pool->tasks, task))
    {
        bh_mutex_unlock(&pool->lock);
        return BH_ERROR;
    }

    /* Signal new job */
    bh_mutex_unlock(&pool->lock);
    bh_cond_signal(&pool->new_task);

    return BH_OK;
}

/**
 * Waits unitl all task in thread \a pool are complete.
 *
 * \param pool  Pointer to the thread pool.
 *
 * \return On success, returns zero.
 * \return On failure, returns error code.
 */
int bh_thread_pool_wait(bh_thread_pool_t *pool)
{
    /* Lock and check if there is jobs in the queue */
    bh_mutex_lock(&pool->lock);
    while (!bh_queue_empty(&pool->tasks))
    {
        bh_task_t *task;

        /* Fetch task from the queue */
        task = bh_queue_front(&pool->tasks);
        bh_queue_remove(&pool->tasks);

        /* Unlock and do the task */
        bh_mutex_unlock(&pool->lock);

        task->func(task->data);
        task->flags |= BH_THREAD_DONE;
        if (task->flags & BH_THREAD_CLEANUP)
            bh_task_free(task);

        /* Lock and check for the next task */
        bh_mutex_lock(&pool->lock);
    }

    /* Check until there is no active threads */
    while (pool->active)
        bh_cond_wait(&pool->done_task, &pool->lock);

    /* Unlock */
    bh_mutex_unlock(&pool->lock);

    return BH_OK;
}

/**
 * Destroyes the thread \a pool object.
 *
 * \warning This is an internal function.
 *
 * \param pool  Pointer to the thread pool object
 */
void bh_thread_pool_destroy(bh_thread_pool_t *pool)
{
    size_t i;

    /* Broadcast shutdown */
    pool->shutdown = 1;
    bh_cond_broadcast(&pool->new_task);

    /* Join and delete every thread */
    for (i = 0; i < pool->size; i++)
        bh_thread_join(pool->threads + i);

    /* Destroy any tasks with cleanup flag */
    while (!bh_queue_empty(&pool->tasks))
    {
        bh_task_t *task;

        task = bh_queue_front(&pool->tasks);
        bh_queue_remove(&pool->tasks);

        if (task->flags & BH_THREAD_CLEANUP)
            bh_task_free(task);
    }

    /* Destroy thread array, queue, lock and condition variables  */
    free(pool->threads);
    bh_queue_destroy(&pool->tasks);
    bh_cond_destroy(&pool->new_task);
    bh_cond_destroy(&pool->done_task);
    bh_mutex_destroy(&pool->lock);
}

/**
 * Frees the thread \a pool object.
 *
 * \param pool  Pointer to the thread pool object
 */
void bh_thread_pool_free(bh_thread_pool_t *pool)
{
    bh_thread_pool_destroy(pool);
    free(pool);
}

/**
 * \warning This is an internal function.
 * \warning Do not use this function directly!
 */
void bh_thread_pool_worker(void *arg)
{
    bh_thread_pool_t *pool;

    pool = (bh_thread_pool_t *)arg;
    while (1)
    {
        bh_task_t *task;

        /* Wait unitl there is a task or shutdown signal */
        bh_mutex_lock(&pool->lock);
        while (!pool->shutdown && bh_queue_empty(&pool->tasks))
            bh_cond_wait(&pool->new_task, &pool->lock);

        /* If its shutdown signal - exit the loop */
        if (pool->shutdown)
        {
            bh_mutex_unlock(&pool->lock);
            break;
        }

        /* Fetch task from the front of the queue, increase active counter */
        task = bh_queue_front(&pool->tasks);
        bh_queue_remove(&pool->tasks);
        pool->active++;
        bh_mutex_unlock(&pool->lock);

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

        /* Decrease active counter and broadcast that we are done */
        bh_mutex_lock(&pool->lock);
        pool->active--;

        bh_mutex_unlock(&pool->lock);
        bh_cond_broadcast(&pool->done_task);
    }
}

/**
 * \}
 */