added mutex support on linux
This commit is contained in:
parent
bb6f3f967c
commit
4cc2159793
3 changed files with 840 additions and 814 deletions
44
cthreads.c
44
cthreads.c
|
|
@ -73,12 +73,10 @@ cmutex_t mtxInit(void) {
|
||||||
InitializeCriticalSection(crit_sec);
|
InitializeCriticalSection(crit_sec);
|
||||||
}
|
}
|
||||||
return (cmutex_t)crit_sec;
|
return (cmutex_t)crit_sec;
|
||||||
// return (cmutex_t)CreateMutexW(NULL, false, NULL);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void mtxDestroy(cmutex_t ctx) {
|
void mtxDestroy(cmutex_t ctx) {
|
||||||
DeleteCriticalSection((CRITICAL_SECTION *)ctx);
|
DeleteCriticalSection((CRITICAL_SECTION *)ctx);
|
||||||
// CloseHandle((HANDLE)ctx);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool mtxValid(cmutex_t ctx) {
|
bool mtxValid(cmutex_t ctx) {
|
||||||
|
|
@ -88,22 +86,15 @@ bool mtxValid(cmutex_t ctx) {
|
||||||
bool mtxLock(cmutex_t ctx) {
|
bool mtxLock(cmutex_t ctx) {
|
||||||
EnterCriticalSection((CRITICAL_SECTION *)ctx);
|
EnterCriticalSection((CRITICAL_SECTION *)ctx);
|
||||||
return true;
|
return true;
|
||||||
// DWORD result = WaitForSingleObject((HANDLE)ctx, INFINITE);
|
|
||||||
// // TODO maybe remove abandoned? or return a enum? it'll hurt usability tho
|
|
||||||
// return result == WAIT_OBJECT_0 || result == WAIT_ABANDONED;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool mtxTryLock(cmutex_t ctx) {
|
bool mtxTryLock(cmutex_t ctx) {
|
||||||
return TryEnterCriticalSection((CRITICAL_SECTION *)ctx);
|
return TryEnterCriticalSection((CRITICAL_SECTION *)ctx);
|
||||||
// int result = mtxTimedLock(ctx, 0);
|
|
||||||
// if(result == CMTX_TIMEDOUT) return CMTX_BUSY;
|
|
||||||
// return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool mtxUnlock(cmutex_t ctx) {
|
bool mtxUnlock(cmutex_t ctx) {
|
||||||
LeaveCriticalSection((CRITICAL_SECTION *)ctx);
|
LeaveCriticalSection((CRITICAL_SECTION *)ctx);
|
||||||
return true;
|
return true;
|
||||||
// return ReleaseMutex((HANDLE)ctx);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -114,6 +105,8 @@ bool mtxUnlock(cmutex_t ctx) {
|
||||||
#include <sys/syscall.h>
|
#include <sys/syscall.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
|
|
||||||
|
// == THREAD ===========================================
|
||||||
|
|
||||||
#define INT_TO_VOIDP(a) ((void *)((uintptr_t)(a)))
|
#define INT_TO_VOIDP(a) ((void *)((uintptr_t)(a)))
|
||||||
|
|
||||||
static void *_thrFuncInternal(void *arg) {
|
static void *_thrFuncInternal(void *arg) {
|
||||||
|
|
@ -169,4 +162,37 @@ bool thrJoin(cthread_t ctx, int *code) {
|
||||||
return pthread_join((pthread_t)ctx, &result) != 0;
|
return pthread_join((pthread_t)ctx, &result) != 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// == MUTEX ============================================
|
||||||
|
|
||||||
|
cmutex_t mtxInit(void) {
|
||||||
|
pthread_mutex_t *mutex = malloc(sizeof(pthread_mutex_t));
|
||||||
|
|
||||||
|
if(mutex) {
|
||||||
|
int res = pthread_mutex_init(mutex, NULL);
|
||||||
|
if(res != 0) mutex = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (cmutex_t)mutex;
|
||||||
|
}
|
||||||
|
|
||||||
|
void mtxDestroy(cmutex_t ctx) {
|
||||||
|
pthread_mutex_destroy((pthread_mutex_t *)ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool mtxValid(cmutex_t ctx) {
|
||||||
|
return (void *)ctx != NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool mtxLock(cmutex_t ctx) {
|
||||||
|
return pthread_mutex_lock((pthread_mutex_t *)ctx) == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool mtxTryLock(cmutex_t ctx) {
|
||||||
|
return pthread_mutex_trylock((pthread_mutex_t *)ctx) == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool mtxUnlock(cmutex_t ctx) {
|
||||||
|
return pthread_mutex_unlock((pthread_mutex_t *)ctx) == 0;
|
||||||
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
Loading…
Add table
Add a link
Reference in a new issue