From 16c7e4cd4761cce56ee221d90fe6b9f7a762f80f Mon Sep 17 00:00:00 2001 From: Tapish Date: Fri, 26 Jul 2024 16:20:55 +0200 Subject: [PATCH] cv_changes --- redGrapes/scheduler/pool_scheduler.tpp | 10 +++--- redGrapes/sync/cv.hpp | 47 +++++++++++++------------- 2 files changed, 29 insertions(+), 28 deletions(-) diff --git a/redGrapes/scheduler/pool_scheduler.tpp b/redGrapes/scheduler/pool_scheduler.tpp index 0dce91a7..012ee181 100644 --- a/redGrapes/scheduler/pool_scheduler.tpp +++ b/redGrapes/scheduler/pool_scheduler.tpp @@ -84,11 +84,13 @@ namespace redGrapes worker_id = next_worker.fetch_add(1) % n_workers; if(worker_id == *TaskFreeCtx::current_worker_id) worker_id = next_worker.fetch_add(1) % n_workers; + m_worker_pool.get_worker_thread(worker_id).worker.ready_queue.push(&task); + } + else + { + m_worker_pool.get_worker_thread(worker_id).worker.ready_queue.push(&task); + m_worker_pool.get_worker_thread(worker_id).worker.wake(); } - - m_worker_pool.get_worker_thread(worker_id).worker.ready_queue.push(&task); - m_worker_pool.set_worker_state(worker_id, dispatch::thread::WorkerState::BUSY); - m_worker_pool.get_worker_thread(worker_id).worker.wake(); } /* Wakeup some worker or the main thread diff --git a/redGrapes/sync/cv.hpp b/redGrapes/sync/cv.hpp index 7fb443a1..ff62548e 100644 --- a/redGrapes/sync/cv.hpp +++ b/redGrapes/sync/cv.hpp @@ -14,6 +14,11 @@ # define REDGRAPES_CONDVAR_TIMEOUT 0x20'0000 #endif +#ifndef REDGRAPES_CONDVAR_TIMEOUT_MS +# define REDGRAPES_CONDVAR_TIMEOUT_MS 4 +#endif + + namespace redGrapes { @@ -37,7 +42,7 @@ namespace redGrapes unsigned timeout; - CondVar() : CondVar(REDGRAPES_CONDVAR_TIMEOUT) + CondVar() : CondVar(REDGRAPES_CONDVAR_TIMEOUT_MS) { } @@ -47,38 +52,32 @@ namespace redGrapes void wait() { - unsigned count = 0; - while(should_wait.load(std::memory_order_acquire)) + // Only enter the critical section if needed + if(should_wait.load(std::memory_order_acquire)) { - if(++count > timeout) - { - // TODO: check this opmitization with a member std::atomic_flag busy - // busy.clear(std::memory_order_release); - - if(should_wait.load(std::memory_order_acquire)) - { - std::unique_lock l(m); - cv.wait(l, [this] { return !should_wait.load(std::memory_order_acquire); }); - } - } + std::unique_lock l(m); + cv.wait_for( + l, + std::chrono::milliseconds(timeout), + [this] { return !should_wait.load(std::memory_order_acquire); }); + + // Ensure to reset should_wait after exiting the wait loop + should_wait.store(true, std::memory_order_release); } - - should_wait.store(true); } bool notify() { - bool w = true; - should_wait.compare_exchange_strong(w, false, std::memory_order_release); - - // TODO: check this optimization - // if( ! busy.test_and_set(std::memory_order_acquire) ) + bool expected = true; + // Combine load and store in a single atomic operation + if(should_wait + .compare_exchange_strong(expected, false, std::memory_order_release, std::memory_order_relaxed)) { - std::unique_lock l(m); + std::unique_lock l(m); cv.notify_all(); + return true; } - - return w; + return false; } };