Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

unix: thread: use mach semaphores on osx

  • Loading branch information...
indutny authored and bnoordhuis committed Jul 8, 2012
1 parent be09be7 commit 3726dee5e932563bdfce5b545023efe326b18544
Showing with 52 additions and 1 deletion.
  1. +12 −1 include/uv-private/uv-unix.h
  2. +40 −0 src/unix/thread.c
@@ -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>
@@ -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;
@@ -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);
@@ -209,3 +247,5 @@ int uv_sem_trywait(uv_sem_t* sem) {

return r;
}

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

0 comments on commit 3726dee

Please sign in to comment.
You can’t perform that action at this time.