diff options
author | Marat Dukhan <maratek@google.com> | 2019-09-30 16:16:36 -0700 |
---|---|---|
committer | Marat Dukhan <maratek@google.com> | 2019-09-30 16:16:36 -0700 |
commit | 714c953fb44bbbb1ae3e0e761b9e67cd40df41cf (patch) | |
tree | 64d8a68ba94745b9385240f8b09a9994f402fde5 | |
parent | 29f0e2ced866e321d8293d4d1bdae71a5fdc90ec (diff) | |
download | platform_external_pthreadpool-714c953fb44bbbb1ae3e0e761b9e67cd40df41cf.tar.gz platform_external_pthreadpool-714c953fb44bbbb1ae3e0e761b9e67cd40df41cf.tar.bz2 platform_external_pthreadpool-714c953fb44bbbb1ae3e0e761b9e67cd40df41cf.zip |
Enable spin-wait in the main thread
-rw-r--r-- | src/threadpool-pthreads.c | 31 |
1 files changed, 24 insertions, 7 deletions
diff --git a/src/threadpool-pthreads.c b/src/threadpool-pthreads.c index b9b5e01..fd2829a 100644 --- a/src/threadpool-pthreads.c +++ b/src/threadpool-pthreads.c @@ -233,20 +233,37 @@ static void checkin_worker_thread(struct pthreadpool* threadpool) { } static void wait_worker_threads(struct pthreadpool* threadpool) { + /* Initial check */ + const uint32_t active_threads = threadpool->active_threads; + if (active_threads == 0) { + __sync_synchronize(); + return; + } + + /* Spin-wait */ + for (uint32_t i = 0; i < PTHREADPOOL_SPIN_WAIT_ITERATIONS; i++) { + __sync_synchronize(); + const uint32_t active_threads = threadpool->active_threads; + if (active_threads == 0) { + __sync_synchronize(); + return; + } + } + + /* Fall-back to mutex/futex wait */ #if PTHREADPOOL_USE_FUTEX uint32_t has_active_threads; while ((has_active_threads = threadpool->has_active_threads) != 0) { futex_wait(&threadpool->has_active_threads, 1); } #else - if (threadpool->active_threads != 0) { - pthread_mutex_lock(&threadpool->completion_mutex); - while (threadpool->active_threads != 0) { - pthread_cond_wait(&threadpool->completion_condvar, &threadpool->completion_mutex); - }; - pthread_mutex_unlock(&threadpool->completion_mutex); - } + pthread_mutex_lock(&threadpool->completion_mutex); + while (threadpool->active_threads != 0) { + pthread_cond_wait(&threadpool->completion_condvar, &threadpool->completion_mutex); + }; + pthread_mutex_unlock(&threadpool->completion_mutex); #endif + __sync_synchronize(); } inline static bool atomic_decrement(volatile size_t* value) { |