From 62221ca51746b156e912a6d9de35c674bc9ce0f8 Mon Sep 17 00:00:00 2001 From: Kevin Wang Date: Wed, 14 Oct 2020 16:06:08 +0800 Subject: [PATCH] Fix crash due to sem on macOS --- src/homer_lib/hmr_os_primitives.h | 51 +++++++++++++++++++++---------- 1 file changed, 35 insertions(+), 16 deletions(-) diff --git a/src/homer_lib/hmr_os_primitives.h b/src/homer_lib/hmr_os_primitives.h index e1a0c29..010f91f 100644 --- a/src/homer_lib/hmr_os_primitives.h +++ b/src/homer_lib/hmr_os_primitives.h @@ -100,8 +100,12 @@ typedef void* hmr_thread_t; #include #include #include -#include +#ifdef __APPLE__ +#include +#else +#include +#endif //-----------------------------------------------------------threads--------------------------------------------------- //data alignment @@ -127,14 +131,38 @@ typedef pthread_mutex_t hmr_mutex; #define MUTEX_UNLOCK(mutex) pthread_mutex_unlock(&mutex) //-----------------------------------------------------------semaphores--------------------------------------------------- - -typedef sem_t hmr_sem_t; -typedef sem_t* hmr_sem_ptr; +#ifdef __APPLE__ +typedef dispatch_semaphore_t hmr_sem_t; +typedef dispatch_semaphore_t* hmr_sem_ptr; +#else +typedef sem_t hmr_sem_t; +typedef sem_t* hmr_sem_ptr; +#endif #define SEM_COPY(src,dst) dst=&src; -#define SEM_INIT(sem, count, max_count) sem_init(&sem, 0, count); -#define SEM_POST(sem) sem_post(sem); -#define SEM_WAIT(sem) sem_wait(sem); +#ifdef __APPLE__ +#define SEM_INIT(sem, count, max_count) sem = dispatch_semaphore_create(count); +#define SEM_POST(sem) dispatch_semaphore_signal(*sem); +#define SEM_WAIT(sem) dispatch_semaphore_wait(*sem, DISPATCH_TIME_FOREVER); +#define SEM_RESET(sem) \ +{ \ + long rc=0; \ + while(rc==0) \ + rc = dispatch_semaphore_wait(*sem, 0); \ +} +#define SEM_DESTROY(sem) +#else +#define SEM_INIT(sem, count, max_count) sem_init(&sem, 0, count); +#define SEM_POST(sem) sem_post(sem); +#define SEM_WAIT(sem) sem_wait(sem); +#define SEM_RESET(sem) \ +{ \ + int rc=0; \ + while(rc==0 && errno != EAGAIN) \ + rc = sem_trywait(sem); \ +} +#define SEM_DESTROY(sem) sem_destroy(sem); +#endif #define SEM_WAIT_MULTIPLE(semaphores, n) \ { \ @@ -143,15 +171,6 @@ typedef sem_t* hmr_sem_ptr; SEM_WAIT(semaphores[nSems]); \ } -#define SEM_RESET(sem) \ -{ \ - int rc=0; \ - while(rc==0 && errno != EAGAIN) \ - rc = sem_trywait(sem); \ -} - -#define SEM_DESTROY(sem) sem_destroy(sem); - //-----------------------------------------------------------threads--------------------------------------------------- //thread function return format