reworking thread exit code API to make it easier on win32

This commit is contained in:
Reuben Dunnington 2023-07-30 20:58:10 -07:00
parent baab4036c4
commit 58210d1884
Signed by: rdunnington
GPG Key ID: 4EC5290E704FD482
2 changed files with 15 additions and 7 deletions

View File

@ -32,7 +32,7 @@ enum
typedef struct mp_thread mp_thread; 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(mp_thread_start_function start, void* userPointer);
mp_thread* mp_thread_create_with_name(mp_thread_start_function start, void* userPointer, const char* name); 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(); u64 mp_thread_self_id();
int mp_thread_signal(mp_thread* thread, int sig); 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); 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_lock(mp_mutex* mutex);
int mp_mutex_unlock(mp_mutex* mutex); int mp_mutex_unlock(mp_mutex* mutex);
//--------------------------------------------------------------- //---------------------------------------------------------------
// Lightweight ticket mutex API // Lightweight ticket mutex API
//--------------------------------------------------------------- //---------------------------------------------------------------

View File

@ -23,14 +23,15 @@ struct mp_thread
char name[MP_THREAD_NAME_MAX_SIZE]; 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; mp_thread* thread = (mp_thread*)data;
if(strlen(thread->name)) if(strlen(thread->name))
{ {
pthread_setname_np(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) 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)); 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); return(-1);
} }
free(thread); free(thread);
if (exitCode)
{
*exitCode = (off_t)ret;
}
return(0); return(0);
} }
@ -156,6 +163,8 @@ int mp_mutex_unlock(mp_mutex* mutex)
return(pthread_mutex_unlock(&mutex->pmutex)); 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) void mp_ticket_spin_mutex_init(mp_ticket_spin_mutex* mutex)
{ {
mutex->nextTicket = 0; mutex->nextTicket = 0;