diff options
Diffstat (limited to 'src/thread_posix.c')
| -rw-r--r-- | src/thread_posix.c | 33 |
1 files changed, 31 insertions, 2 deletions
diff --git a/src/thread_posix.c b/src/thread_posix.c index 2ed3ab8..22d4c62 100644 --- a/src/thread_posix.c +++ b/src/thread_posix.c @@ -20,17 +20,46 @@ static void *bh_thread_run(void *arg) int bh_thread_init(bh_thread_t *thread, bh_task_t *task) { + thread->allocated = 0; return pthread_create(&thread->handle, NULL, bh_thread_run, task); } +bh_thread_t *bh_thread_new(bh_task_t *task) +{ + bh_thread_t *result; + + result = malloc(sizeof(*result)); + + if (result && bh_thread_init(result, task)) + { + free(result); + result = NULL; + } + + if (result) + result->allocated = 1; + + return result; +} + int bh_thread_join(bh_thread_t *thread) { - return pthread_join(thread->handle, NULL); + int result = pthread_join(thread->handle, NULL); + + if (thread->allocated) + free(thread); + + return result; } int bh_thread_detach(bh_thread_t *thread) { - return pthread_detach(thread->handle); + int result = pthread_detach(thread->handle); + + if (thread->allocated) + free(thread); + + return result; } int bh_mutex_init(bh_mutex_t *mutex) |
