Skip to content
This repository has been archived by the owner on May 4, 2018. It is now read-only.

Commit

Permalink
unix: thread: use mach semaphores on osx
Browse files Browse the repository at this point in the history
  • Loading branch information
indutny authored and bnoordhuis committed Jul 8, 2012
1 parent be09be7 commit 3a54805
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
13 changes: 12 additions & 1 deletion include/uv-private/uv-unix.h
Expand Up @@ -40,10 +40,17 @@
#include <termios.h>
#include <pwd.h>

#include <semaphore.h>
#include <pthread.h>
#include <signal.h>

#if defined(__APPLE__) && defined(__MACH__)
# include <mach/mach.h>
# include <mach/task.h>
# include <mach/semaphore.h>
#else
# include <semaphore.h>
#endif

#if __sun
# include <sys/port.h>
# include <port.h>
Expand All @@ -67,7 +74,11 @@ typedef pthread_once_t uv_once_t;
typedef pthread_t uv_thread_t;
typedef pthread_mutex_t uv_mutex_t;
typedef pthread_rwlock_t uv_rwlock_t;
#if defined(__APPLE__) && defined(__MACH__)
typedef semaphore_t uv_sem_t;
#else
typedef sem_t uv_sem_t;
#endif

/* Platform-specific definitions for uv_spawn support. */
typedef gid_t uv_gid_t;
Expand Down
40 changes: 40 additions & 0 deletions src/unix/thread.c
Expand Up @@ -167,6 +167,44 @@ void uv_once(uv_once_t* guard, void (*callback)(void)) {
abort();
}

#if defined(__APPLE__) && defined(__MACH__)

int uv_sem_init(uv_sem_t* sem, unsigned int value) {
return semaphore_create(mach_task_self(), sem, SYNC_POLICY_FIFO, value);
}


void uv_sem_destroy(uv_sem_t* sem) {
if (semaphore_destroy(mach_task_self(), *sem))
abort();
}


void uv_sem_post(uv_sem_t* sem) {
if (semaphore_signal(*sem))
abort();
}


void uv_sem_wait(uv_sem_t* sem) {
if (semaphore_wait(*sem))
abort();
}


int uv_sem_trywait(uv_sem_t* sem) {
mach_timespec_t interval;

interval.tv_sec = 0;
interval.tv_nsec = 0;

if (semaphore_timedwait(*sem, interval) == KERN_SUCCESS) {
return 0;
else
return -1;
}

#else /* !(defined(__APPLE__) && defined(__MACH__)) */

int uv_sem_init(uv_sem_t* sem, unsigned int value) {
return sem_init(sem, 0, value);
Expand Down Expand Up @@ -209,3 +247,5 @@ int uv_sem_trywait(uv_sem_t* sem) {

return r;
}

#endif /* defined(__APPLE__) && defined(__MACH__) */

0 comments on commit 3a54805

Please sign in to comment.