Commit 0f32456b authored by Xavier Thompson's avatar Xavier Thompson

Refactor scheduler.hpp and worker.hpp

parent 386b5186
...@@ -43,18 +43,12 @@ namespace typon ...@@ -43,18 +43,12 @@ namespace typon
static void push(Continuation task) noexcept static void push(Continuation task) noexcept
{ {
get()._worker[thread_id]._deque.load()->push(task); get()._worker[thread_id].push(task);
} }
static bool pop() noexcept static bool pop() noexcept
{ {
Deque * deque = get()._worker[thread_id]._deque.load(); return get()._worker[thread_id].pop();
bool result = deque->pop();
if (auto garbage = deque->reclaim())
{
get()._gc.retire(garbage);
}
return result;
} }
static auto suspend(std::coroutine_handle<> coroutine) noexcept static auto suspend(std::coroutine_handle<> coroutine) noexcept
...@@ -145,7 +139,7 @@ namespace typon ...@@ -145,7 +139,7 @@ namespace typon
for (uint i = 0; i < _concurrency * 2 + 1; i++) for (uint i = 0; i < _concurrency * 2 + 1; i++)
{ {
uint id = fdt::random::random() % _concurrency; uint id = fdt::random::random() % _concurrency;
work = _worker[id].try_steal(); work = _worker[id].steal();
if (work) if (work)
{ {
break; break;
...@@ -158,7 +152,7 @@ namespace typon ...@@ -158,7 +152,7 @@ namespace typon
auto epoch = _gc.epoch(thread_id); auto epoch = _gc.epoch(thread_id);
for (uint id = 0; id < _concurrency; id++) for (uint id = 0; id < _concurrency; id++)
{ {
work = _worker[id].try_steal(); work = _worker[id].steal();
if (work) if (work)
{ {
break; break;
......
...@@ -61,6 +61,23 @@ namespace typon ...@@ -61,6 +61,23 @@ namespace typon
} }
} }
void add(Deque * deque) noexcept
{
std::lock_guard lock(_mutex);
_pool.push_back(deque);
}
bool try_add(Deque * deque) noexcept
{
if (!_mutex.try_lock())
{
return false;
}
std::lock_guard lock(_mutex, std::adopt_lock);
_pool.push_back(deque);
return true;
}
auto suspend(std::coroutine_handle<> coroutine) noexcept auto suspend(std::coroutine_handle<> coroutine) noexcept
{ {
auto deque = _deque.load(); auto deque = _deque.load();
...@@ -91,24 +108,23 @@ namespace typon ...@@ -91,24 +108,23 @@ namespace typon
} }
} }
void add(Deque * deque) noexcept void push(Continuation task) noexcept
{ {
std::lock_guard lock(_mutex); _deque.load()->push(task);
_pool.push_back(deque);
} }
bool try_add(Deque * deque) noexcept bool pop() noexcept
{ {
if (!_mutex.try_lock()) Deque * deque = _deque.load();
bool result = deque->pop();
if (auto garbage = deque->reclaim())
{ {
return false; _gc->retire(garbage);
} }
std::lock_guard lock(_mutex, std::adopt_lock); return result;
_pool.push_back(deque);
return true;
} }
Work try_steal() noexcept Work steal() noexcept
{ {
if (!_mutex.try_lock()) if (!_mutex.try_lock())
{ {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment