Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash due to the lack of posix semaphore on macOS #14

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 35 additions & 16 deletions src/homer_lib/hmr_os_primitives.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,12 @@ typedef void* hmr_thread_t;
#include <limits.h>
#include <errno.h>
#include <pthread.h>
#include <semaphore.h>

#ifdef __APPLE__
#include <dispatch/dispatch.h>
#else
#include <semaphore.h>
#endif
//-----------------------------------------------------------threads---------------------------------------------------

//data alignment
Expand All @@ -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) \
{ \
Expand All @@ -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
Expand Down