From 5676443139b4153b33eec51af04e083cc01fe1f7 Mon Sep 17 00:00:00 2001 From: Kamil Rakoczy Date: Mon, 11 Sep 2023 19:43:26 +0200 Subject: [PATCH] Internals: Rename and slightly change other threads stopping/resuming methods. (#4478) Co-authored-by: Mariusz Glebocki --- src/V3ThreadPool.cpp | 7 ++++--- src/V3ThreadPool.h | 22 +++++++++++++--------- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/src/V3ThreadPool.cpp b/src/V3ThreadPool.cpp index 8eadc7b61..36a6d6c50 100644 --- a/src/V3ThreadPool.cpp +++ b/src/V3ThreadPool.cpp @@ -84,11 +84,11 @@ void V3ThreadPool::workerJobLoop(int id) VL_MT_SAFE { bool V3ThreadPool::waitIfStopRequested() VL_MT_SAFE VL_EXCLUDES(m_stoppedJobsMutex) { if (!stopRequested()) return false; V3LockGuard stoppedJobLock(m_stoppedJobsMutex); - waitStopRequested(); + waitForResumeRequest(); return true; } -void V3ThreadPool::waitStopRequested() VL_REQUIRES(m_stoppedJobsMutex) { +void V3ThreadPool::waitForResumeRequest() VL_REQUIRES(m_stoppedJobsMutex) { ++m_stoppedJobs; m_stoppedJobsCV.notify_all(); m_stoppedJobsCV.wait(m_stoppedJobsMutex, [&]() VL_REQUIRES(m_stoppedJobsMutex) { @@ -98,8 +98,9 @@ void V3ThreadPool::waitStopRequested() VL_REQUIRES(m_stoppedJobsMutex) { m_stoppedJobsCV.notify_all(); } -void V3ThreadPool::waitOtherThreads() VL_MT_SAFE_EXCLUDES(m_mutex) +void V3ThreadPool::stopOtherThreads() VL_MT_SAFE_EXCLUDES(m_mutex) VL_REQUIRES(m_stoppedJobsMutex) { + m_stopRequested = true; ++m_stoppedJobs; m_stoppedJobsCV.notify_all(); m_cv.notify_all(); diff --git a/src/V3ThreadPool.h b/src/V3ThreadPool.h index 7d21d157a..ad47ec093 100644 --- a/src/V3ThreadPool.h +++ b/src/V3ThreadPool.h @@ -192,11 +192,17 @@ private: return m_stopRequested; } - // Waits until exclusive access job completes its job - void waitStopRequested() VL_REQUIRES(m_stoppedJobsMutex); + // Waits until `resumeOtherThreads()` is called or exclusive access scope end. + void waitForResumeRequest() VL_REQUIRES(m_stoppedJobsMutex); - // Waits until all other jobs are stopped - void waitOtherThreads() VL_MT_SAFE_EXCLUDES(m_mutex) VL_REQUIRES(m_stoppedJobsMutex); + // Sends stop request to other threads and waits until they stop. + void stopOtherThreads() VL_MT_SAFE_EXCLUDES(m_mutex) VL_REQUIRES(m_stoppedJobsMutex); + + // Resumes threads stopped through previous call to `stopOtherThreads()`. + void resumeOtherThreads() VL_MT_SAFE_EXCLUDES(m_mutex) VL_REQUIRES(m_stoppedJobsMutex) { + m_stopRequested = false; + m_stoppedJobsCV.notify_all(); + } void workerJobLoop(int id) VL_MT_SAFE; @@ -209,9 +215,8 @@ public: if (!V3ThreadPool::s().willExecuteSynchronously()) { V3ThreadPool::s().m_stoppedJobsMutex.lock(); - if (V3ThreadPool::s().stopRequested()) { V3ThreadPool::s().waitStopRequested(); } - V3ThreadPool::s().m_stopRequested = true; - V3ThreadPool::s().waitOtherThreads(); + if (V3ThreadPool::s().stopRequested()) { V3ThreadPool::s().waitForResumeRequest(); } + V3ThreadPool::s().stopOtherThreads(); V3ThreadPool::s().m_exclusiveAccess = true; } else { V3ThreadPool::s().m_stoppedJobsMutex.assumeLocked(); @@ -221,8 +226,7 @@ public: // Can't use `willExecuteSynchronously`, we're still in exclusive execution state. if (V3ThreadPool::s().m_exclusiveAccess) { V3ThreadPool::s().m_exclusiveAccess = false; - V3ThreadPool::s().m_stopRequested = false; - V3ThreadPool::s().m_stoppedJobsCV.notify_all(); + V3ThreadPool::s().resumeOtherThreads(); V3ThreadPool::s().m_stoppedJobsMutex.unlock(); } else {