From d106ecdc6541c2081c4f2fa9b670e984a79a6326 Mon Sep 17 00:00:00 2001 From: Jan Vidar Krey Date: Thu, 25 Oct 2012 04:10:42 +0200 Subject: [PATCH] Bugfixes for pthreads. --- src/util/threads.c | 24 ++++++++++++++++-------- src/util/threads.h | 2 +- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/util/threads.c b/src/util/threads.c index 814037f..b76ef91 100644 --- a/src/util/threads.c +++ b/src/util/threads.c @@ -20,6 +20,12 @@ #include "uhub.h" #ifdef POSIX_THREAD_SUPPORT + +struct pthread_data +{ + pthread_t handle; +}; + void uhub_mutex_init(uhub_mutex_t* mutex) { pthread_mutex_init(mutex, NULL); @@ -48,23 +54,25 @@ int uhub_mutex_trylock(uhub_mutex_t* mutex) uhub_thread_t* uhub_thread_create(uhub_thread_start start, void* arg) { - uhub_thread_t* thread = (uhub_thread_t*) hub_malloc_zero(sizeof(uhub_thread_t)); - int ret = pthread_create(thread, NULL, start, arg); - if (ret == 0) - return thread; - hub_free(thread); - return NULL; + struct pthread_data* thread = (struct pthread_data*) hub_malloc_zero(sizeof(struct pthread_data)); + int ret = pthread_create(&thread->handle, NULL, start, arg); + if (ret != 0) + { + hub_free(thread); + thread = NULL; + } + return thread; } void uhub_thread_cancel(uhub_thread_t* thread) { - pthread_cancel(thread); + pthread_cancel(thread->handle); } void* uhub_thread_join(uhub_thread_t* thread) { void* ret = NULL; - pthread_join(thread, &ret); + pthread_join(thread->handle, &ret); hub_free(thread); return ret; } diff --git a/src/util/threads.h b/src/util/threads.h index afc4359..0e4a9ac 100644 --- a/src/util/threads.h +++ b/src/util/threads.h @@ -23,7 +23,7 @@ typedef void*(*uhub_thread_start)(void*) ; #ifdef POSIX_THREAD_SUPPORT -typedef pthread_t uhub_thread_t; +typedef struct pthread_data uhub_thread_t; typedef pthread_mutex_t uhub_mutex_t; #endif