From 58210d1884bb6cd5884edb1da8f925fe1ba1d112 Mon Sep 17 00:00:00 2001 From: Reuben Dunnington Date: Sun, 30 Jul 2023 20:58:10 -0700 Subject: [PATCH] reworking thread exit code API to make it easier on win32 --- src/platform/platform_thread.h | 5 ++--- src/platform/posix_thread.c | 17 +++++++++++++---- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/platform/platform_thread.h b/src/platform/platform_thread.h index 6a77061..fcedf18 100644 --- a/src/platform/platform_thread.h +++ b/src/platform/platform_thread.h @@ -32,7 +32,7 @@ enum typedef struct mp_thread mp_thread; -typedef void* (*mp_thread_start_function)(void* userPointer); +typedef i32 (*mp_thread_start_function)(void* userPointer); mp_thread* mp_thread_create(mp_thread_start_function start, void* userPointer); mp_thread* mp_thread_create_with_name(mp_thread_start_function start, void* userPointer, const char* name); @@ -43,7 +43,7 @@ u64 mp_thread_unique_id(mp_thread* thread); u64 mp_thread_self_id(); int mp_thread_signal(mp_thread* thread, int sig); -int mp_thread_join(mp_thread* thread, void** ret); +int mp_thread_join(mp_thread* thread, i64* exitCode); int mp_thread_detach(mp_thread* thread); //--------------------------------------------------------------- @@ -57,7 +57,6 @@ int mp_mutex_destroy(mp_mutex* mutex); int mp_mutex_lock(mp_mutex* mutex); int mp_mutex_unlock(mp_mutex* mutex); - //--------------------------------------------------------------- // Lightweight ticket mutex API //--------------------------------------------------------------- diff --git a/src/platform/posix_thread.c b/src/platform/posix_thread.c index 135f04e..70269f5 100644 --- a/src/platform/posix_thread.c +++ b/src/platform/posix_thread.c @@ -23,14 +23,15 @@ struct mp_thread char name[MP_THREAD_NAME_MAX_SIZE]; }; -void* mp_thread_bootstrap(void* data) +static void* mp_thread_bootstrap(void* data) { mp_thread* thread = (mp_thread*)data; if(strlen(thread->name)) { pthread_setname_np(thread->name); } - return(thread->start(thread->userPointer)); + i32 exitCode = thread->start(thread->userPointer); + return((void*)exitCode); } mp_thread* mp_thread_create_with_name(mp_thread_start_function start, void* userPointer, const char* name) @@ -95,13 +96,19 @@ int mp_thread_signal(mp_thread* thread, int sig) return(pthread_kill(thread->pthread, sig)); } -int mp_thread_join(mp_thread* thread, void** ret) +int mp_thread_join(mp_thread* thread, i64* exitCode) { - if(pthread_join(thread->pthread, ret)) + void* ret; + if(pthread_join(thread->pthread, &ret)) { return(-1); } free(thread); + + if (exitCode) + { + *exitCode = (off_t)ret; + } return(0); } @@ -156,6 +163,8 @@ int mp_mutex_unlock(mp_mutex* mutex) return(pthread_mutex_unlock(&mutex->pmutex)); } +// mp_ticket_spin_mutex has a mirrored implementation in win32_thread.c + void mp_ticket_spin_mutex_init(mp_ticket_spin_mutex* mutex) { mutex->nextTicket = 0;