diff options
author | Marat Dukhan <maratek@gmail.com> | 2017-03-24 15:10:39 -0400 |
---|---|---|
committer | Marat Dukhan <maratek@gmail.com> | 2017-03-24 15:10:39 -0400 |
commit | 2da784d011c9b26df68a8a79d71bdc067a8c64c8 (patch) | |
tree | a20f2a817e50b1d05b86f75b2d364504ec78b632 /src | |
parent | d08554a5a4d907b0544be2107f471fb1dd520007 (diff) | |
download | pthreadpool-2da784d011c9b26df68a8a79d71bdc067a8c64c8.tar.gz |
Use futex on Native Client
Diffstat (limited to 'src')
-rw-r--r-- | src/threadpool-pthreads.c | 24 |
1 files changed, 23 insertions, 1 deletions
diff --git a/src/threadpool-pthreads.c b/src/threadpool-pthreads.c index 9da070a..8c74c5c 100644 --- a/src/threadpool-pthreads.c +++ b/src/threadpool-pthreads.c @@ -13,6 +13,9 @@ #define PTHREADPOOL_USE_FUTEX 1 #include <sys/syscall.h> #include <linux/futex.h> +#elif defined(__native_client__) + #define PTHREADPOOL_USE_FUTEX 1 + #include <irt.h> #else #define PTHREADPOOL_USE_FUTEX 0 #endif @@ -72,6 +75,21 @@ static inline size_t min(size_t a, size_t b) { return syscall(SYS_futex, address, FUTEX_WAKE_PRIVATE, INT_MAX, NULL, NULL, 0); } + #elif defined(__native_client__) + static struct nacl_irt_futex nacl_irt_futex = { 0 }; + static pthread_once_t nacl_init_guard = PTHREAD_ONCE_INIT; + static void nacl_init(void) { + nacl_interface_query(NACL_IRT_FUTEX_v0_1, &nacl_irt_futex, sizeof(nacl_irt_futex)); + } + + static int futex_wait(volatile uint32_t* address, uint32_t value) { + return nacl_irt_futex.futex_wait_abs((volatile int*) address, (int) value, NULL); + } + + static int futex_wake_all(volatile uint32_t* address) { + int count; + return nacl_irt_futex.futex_wake((volatile int*) address, INT_MAX, &count); + } #else #error "Platform-specific implementation of futex_wait and futex_wake_all required" #endif @@ -200,7 +218,7 @@ static void wait_worker_threads(struct pthreadpool* threadpool) { #if PTHREADPOOL_USE_FUTEX uint32_t has_active_threads; while ((has_active_threads = threadpool->has_active_threads) != 0) { - futex_wait(&threadpool->has_active_threads, has_active_threads); + futex_wait(&threadpool->has_active_threads, 1); } #else if (threadpool->active_threads != 0) { @@ -296,6 +314,10 @@ static void* thread_main(void* arg) { } struct pthreadpool* pthreadpool_create(size_t threads_count) { +#if defined(__native_client__) + pthread_once(&nacl_init_guard, nacl_init); +#endif + if (threads_count == 0) { threads_count = (size_t) sysconf(_SC_NPROCESSORS_ONLN); } |