From b10fedde560b7dc7aba8ac9057a0800a700a21e2 Mon Sep 17 00:00:00 2001 From: Alexander Alekhin Date: Tue, 6 Feb 2018 16:10:41 +0300 Subject: [PATCH 1/2] core(parallel_for): cleanup remove 'dont_wait' (can be replaced with has_wake_signal) --- modules/core/src/parallel_impl.cpp | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/modules/core/src/parallel_impl.cpp b/modules/core/src/parallel_impl.cpp index 32e540fb06..5fe9368fb2 100644 --- a/modules/core/src/parallel_impl.cpp +++ b/modules/core/src/parallel_impl.cpp @@ -197,7 +197,6 @@ public: volatile bool stop_thread; volatile bool has_wake_signal; - volatile bool dont_wait; Ptr job; @@ -213,8 +212,7 @@ public: posix_thread(0), is_created(false), stop_thread(false), - has_wake_signal(false), - dont_wait(false) + has_wake_signal(false) #if !defined(CV_USE_GLOBAL_WORKERS_COND_VAR) , isActive(true) #endif @@ -386,7 +384,7 @@ void WorkerThread::thread_body() while (!stop_thread) { - CV_LOG_VERBOSE(NULL, 5, "Thread: ... loop iteration: allow_active_wait=" << allow_active_wait << " has_wake_signal=" << has_wake_signal << " dont_wait=" << dont_wait ); + CV_LOG_VERBOSE(NULL, 5, "Thread: ... loop iteration: allow_active_wait=" << allow_active_wait << " has_wake_signal=" << has_wake_signal); if (allow_active_wait && CV_WORKER_ACTIVE_WAIT > 0) { allow_active_wait = false; @@ -404,7 +402,7 @@ void WorkerThread::thread_body() #ifdef CV_PROFILE_THREADS stat.threadWait = getTickCount(); #endif - while (!has_wake_signal && !dont_wait) // to handle spurious wakeups + while (!has_wake_signal) // to handle spurious wakeups { //CV_LOG_VERBOSE(NULL, 5, "Thread: wait (sleep) ..."); #if defined(CV_USE_GLOBAL_WORKERS_COND_VAR) @@ -416,7 +414,6 @@ void WorkerThread::thread_body() #endif CV_LOG_VERBOSE(NULL, 5, "Thread: wake ... (has_wake_signal=" << has_wake_signal << " stop_thread=" << stop_thread << ")") } - dont_wait = false; #ifdef CV_PROFILE_THREADS stat.threadWake = getTickCount(); #endif @@ -611,12 +608,11 @@ void ThreadPool::run(const Range& range, const ParallelLoopBody& body, double ns #if !defined(CV_USE_GLOBAL_WORKERS_COND_VAR) isActive = threads[i]->isActive; #endif - threads[i]->dont_wait = true; + threads[i]->has_wake_signal = true; #ifdef CV_PROFILE_THREADS threads_stat[i + 1].reset(); #endif pthread_mutex_unlock(&threads[i]->mutex); - threads[i]->has_wake_signal = true; #if !defined(CV_USE_GLOBAL_WORKERS_COND_VAR) if (!isActive) { @@ -628,7 +624,6 @@ void ThreadPool::run(const Range& range, const ParallelLoopBody& body, double ns { CV_Assert(threads[i]->job.empty()); threads[i]->job = job; - threads[i]->dont_wait = true; threads[i]->has_wake_signal = true; #ifdef CV_PROFILE_THREADS threads_stat[i + 1].reset(); From 914f57f28ddc1815454923f676ee9feaa0f2b9b0 Mon Sep 17 00:00:00 2001 From: Alexander Alekhin Date: Tue, 6 Feb 2018 17:56:58 +0300 Subject: [PATCH 2/2] core(parallel_for): fix data race --- modules/core/src/parallel_impl.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/modules/core/src/parallel_impl.cpp b/modules/core/src/parallel_impl.cpp index 5fe9368fb2..662db78e67 100644 --- a/modules/core/src/parallel_impl.cpp +++ b/modules/core/src/parallel_impl.cpp @@ -476,7 +476,6 @@ void WorkerThread::thread_body() } else { - has_wake_signal = false; CV_LOG_VERBOSE(NULL, 5, "Thread: no free job tasks"); } }