Skip to content

Commit

Permalink
cv_changes
Browse files Browse the repository at this point in the history
  • Loading branch information
ikbuibui committed Jul 26, 2024
1 parent f33ec50 commit 16c7e4c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 28 deletions.
10 changes: 6 additions & 4 deletions redGrapes/scheduler/pool_scheduler.tpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
47 changes: 23 additions & 24 deletions redGrapes/sync/cv.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
{

Expand All @@ -37,7 +42,7 @@ namespace redGrapes

unsigned timeout;

CondVar() : CondVar(REDGRAPES_CONDVAR_TIMEOUT)
CondVar() : CondVar(REDGRAPES_CONDVAR_TIMEOUT_MS)
{
}

Expand All @@ -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<CVMutex> l(m);
cv.wait(l, [this] { return !should_wait.load(std::memory_order_acquire); });
}
}
std::unique_lock<CVMutex> 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<std::mutex> l(m);
std::unique_lock<CVMutex> l(m);
cv.notify_all();
return true;
}

return w;
return false;
}
};

Expand Down

0 comments on commit 16c7e4c

Please sign in to comment.