platform thread naming pass

This commit is contained in:
Reuben Dunnington 2023-07-30 19:18:00 -07:00
parent 442d86386e
commit baab4036c4
Signed by: rdunnington
GPG Key ID: 4EC5290E704FD482
2 changed files with 69 additions and 69 deletions

View File

@ -25,61 +25,65 @@ extern "C" {
// Platform Thread API // Platform Thread API
//--------------------------------------------------------------- //---------------------------------------------------------------
typedef struct platform_thread platform_thread; enum
{
MP_THREAD_NAME_MAX_SIZE = 64, // including null terminator
};
typedef void* (*ThreadStartFunction)(void* userPointer); typedef struct mp_thread mp_thread;
platform_thread* ThreadCreate(ThreadStartFunction start, void* userPointer); typedef void* (*mp_thread_start_function)(void* userPointer);
platform_thread* ThreadCreateWithName(ThreadStartFunction start, void* userPointer, const char* name);
const char* ThreadGetName(platform_thread* thread); 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);
u64 ThreadSelfID(); const char* mp_thread_get_name(mp_thread* thread);
u64 ThreadUniqueID(platform_thread* thread);
int ThreadSignal(platform_thread* thread, int sig); u64 mp_thread_unique_id(mp_thread* thread);
void ThreadCancel(platform_thread* thread); u64 mp_thread_self_id();
int ThreadJoin(platform_thread* thread, void** ret);
int ThreadDetach(platform_thread* thread); int mp_thread_signal(mp_thread* thread, int sig);
int mp_thread_join(mp_thread* thread, void** ret);
int mp_thread_detach(mp_thread* thread);
//--------------------------------------------------------------- //---------------------------------------------------------------
// Platform Mutex API // Platform Mutex API
//--------------------------------------------------------------- //---------------------------------------------------------------
typedef struct platform_mutex platform_mutex; typedef struct mp_mutex mp_mutex;
platform_mutex* MutexCreate(); mp_mutex* mp_mutex_create();
int MutexDestroy(platform_mutex* mutex); int mp_mutex_destroy(mp_mutex* mutex);
int MutexLock(platform_mutex* mutex); int mp_mutex_lock(mp_mutex* mutex);
int MutexUnlock(platform_mutex* mutex); int mp_mutex_unlock(mp_mutex* mutex);
//--------------------------------------------------------------- //---------------------------------------------------------------
// Lightweight ticket mutex API // Lightweight ticket mutex API
//--------------------------------------------------------------- //---------------------------------------------------------------
typedef struct ticket_spin_mutex typedef struct mp_ticket_spin_mutex
{ {
volatile _Atomic(u64) nextTicket; volatile _Atomic(u64) nextTicket;
volatile _Atomic(u64) serving; volatile _Atomic(u64) serving;
} ticket_spin_mutex; } mp_ticket_spin_mutex;
void TicketSpinMutexInit(ticket_spin_mutex* mutex); void mp_ticket_spin_mutex_init(mp_ticket_spin_mutex* mutex);
void TicketSpinMutexLock(ticket_spin_mutex* mutex); void mp_ticket_spin_mutex_lock(mp_ticket_spin_mutex* mutex);
void TicketSpinMutexUnlock(ticket_spin_mutex* mutex); void mp_ticket_spin_mutex_unlock(mp_ticket_spin_mutex* mutex);
//--------------------------------------------------------------- //---------------------------------------------------------------
// Platform condition variable API // Platform condition variable API
//--------------------------------------------------------------- //---------------------------------------------------------------
typedef struct platform_condition platform_condition; typedef struct mp_condition mp_condition;
platform_condition* ConditionCreate(); mp_condition* mp_condition_create();
int ConditionDestroy(platform_condition* cond); int mp_condition_destroy(mp_condition* cond);
int ConditionWait(platform_condition* cond, platform_mutex* mutex); int mp_condition_wait(mp_condition* cond, mp_mutex* mutex);
int ConditionTimedWait(platform_condition* cond, platform_mutex* mutex, f64 seconds); int mp_condition_timedwait(mp_condition* cond, mp_mutex* mutex, f64 seconds);
int ConditionSignal(platform_condition* cond); int mp_condition_signal(mp_condition* cond);
int ConditionBroadcast(platform_condition* cond); int mp_condition_broadcast(mp_condition* cond);
#ifdef __cplusplus #ifdef __cplusplus
} // extern "C" } // extern "C"

View File

@ -14,20 +14,18 @@
#include"platform_thread.h" #include"platform_thread.h"
const u32 PLATFORM_THREAD_NAME_MAX_SIZE = 64; // including null terminator struct mp_thread
struct platform_thread
{ {
bool valid; bool valid;
pthread_t pthread; pthread_t pthread;
ThreadStartFunction start; mp_thread_start_function start;
void* userPointer; void* userPointer;
char name[PLATFORM_THREAD_NAME_MAX_SIZE]; char name[MP_THREAD_NAME_MAX_SIZE];
}; };
void* platform_thread_bootstrap(void* data) void* mp_thread_bootstrap(void* data)
{ {
platform_thread* thread = (platform_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);
@ -35,9 +33,9 @@ void* platform_thread_bootstrap(void* data)
return(thread->start(thread->userPointer)); return(thread->start(thread->userPointer));
} }
platform_thread* ThreadCreateWithName(ThreadStartFunction start, void* userPointer, const char* name) mp_thread* mp_thread_create_with_name(mp_thread_start_function start, void* userPointer, const char* name)
{ {
platform_thread* thread = (platform_thread*)malloc(sizeof(platform_thread)); mp_thread* thread = (mp_thread*)malloc(sizeof(mp_thread));
if(!thread) if(!thread)
{ {
return(0); return(0);
@ -45,7 +43,7 @@ platform_thread* ThreadCreateWithName(ThreadStartFunction start, void* userPoint
if(name) if(name)
{ {
char* end = stpncpy(thread->name, name, PLATFORM_THREAD_NAME_MAX_SIZE-1); char* end = stpncpy(thread->name, name, MP_THREAD_NAME_MAX_SIZE-1);
*end = '\0'; *end = '\0';
} }
else else
@ -67,30 +65,24 @@ platform_thread* ThreadCreateWithName(ThreadStartFunction start, void* userPoint
} }
} }
platform_thread* ThreadCreate(ThreadStartFunction start, void* userPointer) mp_thread* mp_thread_create(mp_thread_start_function start, void* userPointer)
{ {
return(ThreadCreateWithName(start, userPointer, 0)); return(mp_thread_create_with_name(start, userPointer, 0));
} }
void ThreadCancel(platform_thread* thread) const char* mp_thread_get_name(mp_thread* thread)
{
pthread_cancel(thread->pthread);
}
const char* ThreadGetName(platform_thread* thread)
{ {
return(thread->name); return(thread->name);
} }
u64 mp_thread_unique_id(mp_thread* thread)
u64 ThreadUniqueID(platform_thread* thread)
{ {
u64 id; u64 id;
pthread_threadid_np(thread->pthread, &id); pthread_threadid_np(thread->pthread, &id);
return(id); return(id);
} }
u64 ThreadSelfID() u64 mp_thread_self_id()
{ {
pthread_t thread = pthread_self(); pthread_t thread = pthread_self();
u64 id; u64 id;
@ -98,12 +90,12 @@ u64 ThreadSelfID()
return(id); return(id);
} }
int ThreadSignal(platform_thread* thread, int sig) int mp_thread_signal(mp_thread* thread, int sig)
{ {
return(pthread_kill(thread->pthread, sig)); return(pthread_kill(thread->pthread, sig));
} }
int ThreadJoin(platform_thread* thread, void** ret) int mp_thread_join(mp_thread* thread, void** ret)
{ {
if(pthread_join(thread->pthread, ret)) if(pthread_join(thread->pthread, ret))
{ {
@ -113,7 +105,7 @@ int ThreadJoin(platform_thread* thread, void** ret)
return(0); return(0);
} }
int ThreadDetach(platform_thread* thread) int mp_thread_detach(mp_thread* thread)
{ {
if(pthread_detach(thread->pthread)) if(pthread_detach(thread->pthread))
{ {
@ -124,14 +116,14 @@ int ThreadDetach(platform_thread* thread)
} }
struct platform_mutex struct mp_mutex
{ {
pthread_mutex_t pmutex; pthread_mutex_t pmutex;
}; };
platform_mutex* MutexCreate() mp_mutex* mp_mutex_create()
{ {
platform_mutex* mutex = (platform_mutex*)malloc(sizeof(platform_mutex)); mp_mutex* mutex = (mp_mutex*)malloc(sizeof(mp_mutex));
if(!mutex) if(!mutex)
{ {
return(0); return(0);
@ -143,7 +135,8 @@ platform_mutex* MutexCreate()
} }
return(mutex); return(mutex);
} }
int MutexDestroy(platform_mutex* mutex)
int mp_mutex_destroy(mp_mutex* mutex)
{ {
if(pthread_mutex_destroy(&mutex->pmutex) != 0) if(pthread_mutex_destroy(&mutex->pmutex) != 0)
{ {
@ -153,41 +146,42 @@ int MutexDestroy(platform_mutex* mutex)
return(0); return(0);
} }
int MutexLock(platform_mutex* mutex) int mp_mutex_lock(mp_mutex* mutex)
{ {
return(pthread_mutex_lock(&mutex->pmutex)); return(pthread_mutex_lock(&mutex->pmutex));
} }
int MutexUnlock(platform_mutex* mutex) int mp_mutex_unlock(mp_mutex* mutex)
{ {
return(pthread_mutex_unlock(&mutex->pmutex)); return(pthread_mutex_unlock(&mutex->pmutex));
} }
void TicketSpinMutexInit(ticket_spin_mutex* mutex) void mp_ticket_spin_mutex_init(mp_ticket_spin_mutex* mutex)
{ {
mutex->nextTicket = 0; mutex->nextTicket = 0;
mutex->serving = 0; mutex->serving = 0;
} }
void TicketSpinMutexLock(ticket_spin_mutex* mutex) void mp_ticket_spin_mutex_lock(mp_ticket_spin_mutex* mutex)
{ {
u64 ticket = atomic_fetch_add(&mutex->nextTicket, 1ULL); u64 ticket = atomic_fetch_add(&mutex->nextTicket, 1ULL);
while(ticket != mutex->serving); //spin while(ticket != mutex->serving); //spin
} }
void TicketSpinMutexUnlock(ticket_spin_mutex* mutex) void mp_ticket_spin_mutex_unlock(mp_ticket_spin_mutex* mutex)
{ {
atomic_fetch_add(&mutex->serving, 1ULL); atomic_fetch_add(&mutex->serving, 1ULL);
} }
struct platform_condition
struct mp_condition
{ {
pthread_cond_t pcond; pthread_cond_t pcond;
}; };
platform_condition* ConditionCreate() mp_condition* mp_condition_create()
{ {
platform_condition* cond = (platform_condition*)malloc(sizeof(platform_condition)); mp_condition* cond = (mp_condition*)malloc(sizeof(mp_condition));
if(!cond) if(!cond)
{ {
return(0); return(0);
@ -199,7 +193,8 @@ platform_condition* ConditionCreate()
} }
return(cond); return(cond);
} }
int ConditionDestroy(platform_condition* cond)
int mp_condition_destroy(mp_condition* cond)
{ {
if(pthread_cond_destroy(&cond->pcond) != 0) if(pthread_cond_destroy(&cond->pcond) != 0)
{ {
@ -208,12 +203,13 @@ int ConditionDestroy(platform_condition* cond)
free(cond); free(cond);
return(0); return(0);
} }
int ConditionWait(platform_condition* cond, platform_mutex* mutex)
int mp_condition_wait(mp_condition* cond, mp_mutex* mutex)
{ {
return(pthread_cond_wait(&cond->pcond, &mutex->pmutex)); return(pthread_cond_wait(&cond->pcond, &mutex->pmutex));
} }
int ConditionTimedWait(platform_condition* cond, platform_mutex* mutex, f64 seconds) int mp_condition_timedwait(mp_condition* cond, mp_mutex* mutex, f64 seconds)
{ {
struct timeval tv; struct timeval tv;
gettimeofday(&tv, 0); gettimeofday(&tv, 0);
@ -230,12 +226,12 @@ int ConditionTimedWait(platform_condition* cond, platform_mutex* mutex, f64 seco
return(pthread_cond_timedwait(&cond->pcond, &mutex->pmutex, &ts)); return(pthread_cond_timedwait(&cond->pcond, &mutex->pmutex, &ts));
} }
int ConditionSignal(platform_condition* cond) int mp_condition_signal(mp_condition* cond)
{ {
return(pthread_cond_signal(&cond->pcond)); return(pthread_cond_signal(&cond->pcond));
} }
int ConditionBroadcast(platform_condition* cond) int mp_condition_broadcast(mp_condition* cond)
{ {
return(pthread_cond_broadcast(&cond->pcond)); return(pthread_cond_broadcast(&cond->pcond));
} }