diff options
author | Marat Dukhan <maratek@google.com> | 2019-09-30 14:05:14 -0700 |
---|---|---|
committer | Marat Dukhan <maratek@google.com> | 2019-09-30 14:05:14 -0700 |
commit | e807f76c98bfde48b242607bbb76ccd1ec903948 (patch) | |
tree | 64fef0cbd298038bd0ff0c58ea7f1b27e9d34963 | |
parent | c06f29339e2a200d9da38d86a2cb8be5128cf4c5 (diff) | |
download | platform_external_pthreadpool-e807f76c98bfde48b242607bbb76ccd1ec903948.tar.gz platform_external_pthreadpool-e807f76c98bfde48b242607bbb76ccd1ec903948.tar.bz2 platform_external_pthreadpool-e807f76c98bfde48b242607bbb76ccd1ec903948.zip |
Refactor modulo increment in work-stealing loop
-rw-r--r-- | src/threadpool-pthreads.c | 15 |
1 files changed, 14 insertions, 1 deletions
diff --git a/src/threadpool-pthreads.c b/src/threadpool-pthreads.c index f03af5a..8fffd0b 100644 --- a/src/threadpool-pthreads.c +++ b/src/threadpool-pthreads.c @@ -252,6 +252,16 @@ inline static bool atomic_decrement(volatile size_t* value) { return actual_value != 0; } +inline static size_t modulo_increment(uint32_t i, uint32_t n) { + /* Increment input variable */ + i = i + 1; + /* Wrap modulo n, if needed */ + if (i == n) { + i = 0; + } + return i; +} + static void thread_compute_1d(struct pthreadpool* threadpool, struct thread_info* thread) { const pthreadpool_function_1d_t function = (pthreadpool_function_1d_t) threadpool->function; void *const argument = threadpool->argument; @@ -264,7 +274,10 @@ static void thread_compute_1d(struct pthreadpool* threadpool, struct thread_info /* There still may be other threads with work */ const size_t thread_number = thread->thread_number; const size_t threads_count = threadpool->threads_count; - for (size_t tid = (thread_number + 1) % threads_count; tid != thread_number; tid = (tid + 1) % threads_count) { + for (size_t tid = modulo_increment(thread_number, threads_count); + tid != thread_number; + tid = modulo_increment(tid, threads_count)) + { struct thread_info* other_thread = &threadpool->threads[tid]; while (atomic_decrement(&other_thread->range_length)) { const size_t item_id = __sync_sub_and_fetch(&other_thread->range_end, 1); |