From c3ebff45a67d791b4eeab8ce033507a4e5a134ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Santamaria?= Date: Mon, 23 Oct 2023 19:06:53 +0200 Subject: [PATCH] Add comments to thrd.h --- internal/vm/thrd.h | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/internal/vm/thrd.h b/internal/vm/thrd.h index 49cda6a..9fa0cd2 100644 --- a/internal/vm/thrd.h +++ b/internal/vm/thrd.h @@ -5,9 +5,12 @@ #elif defined(_WIN32) || defined(WIN32) #include + // Thread #define thrd_t HANDLE #define thrd_success 0 + #define thrd_create(thrd, fn, arg) ((*(thrd) = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)(fn), (arg), 0, NULL)) == NULL) + // Mutex #define mtx_t CRITICAL_SECTION #define mtx_plain NULL #define mtx_init InitializeCriticalSection @@ -15,21 +18,25 @@ #define mtx_unlock LeaveCriticalSection #define mtx_destroy DeleteCriticalSection + // Condition #define cnd_t CONDITION_VARIABLE #define cnd_init(arg) InitializeConditionVariable(arg) #define cnd_broadcast WakeAllConditionVariable #define cnd_signal WakeConditionVariable #define cnd_wait(cond, mtx) while (!SleepConditionVariableCS(cond, mtx, INFINITE)) {} #define cnd_destroy DeleteConditionVariable - - #define thrd_create(thrd, fn, arg) ((*(thrd) = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)(fn), (arg), 0, NULL)) == NULL) - #else #include + // Thread #define thrd_t pthread_t #define thrd_success 0 + #define thrd_create(thrd, fn, arg) ({ \ + void *wrapper(void *a) { return (void *)(intptr_t)fn(a); } \ + pthread_create((thrd), NULL, wrapper, (arg)); \ + }) + // Mutex #define mtx_t pthread_mutex_t #define mtx_plain NULL #define mtx_init pthread_mutex_init @@ -37,12 +44,11 @@ #define mtx_unlock pthread_mutex_unlock #define mtx_destroy pthread_mutex_destroy + // Condition #define cnd_t pthread_cond_t #define cnd_init(arg) pthread_cond_init((arg), NULL) #define cnd_broadcast pthread_cond_broadcast #define cnd_signal pthread_cond_signal #define cnd_wait pthread_cond_wait #define cnd_destroy pthread_cond_destroy - - #define thrd_create(thrd, fn, arg) pthread_create((thrd), NULL, (fn), (arg)) #endif