Skip to content

Commit

Permalink
removed JobWorker fetch order
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathan Phipps committed Jan 18, 2024
1 parent 3cb2fe2 commit dedf22e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 87 deletions.
44 changes: 12 additions & 32 deletions include/NP-Engine/JobSystem/JobWorker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,22 @@ namespace np::jsys
private:
friend class JobSystem;

public:
enum class Fetch : ui32
{
None = 0,
Immediate,
PriorityBased,
Steal
};

using FetchOrderArray = con::array<Fetch, 3>;

constexpr static FetchOrderArray DEFAULT_FETCH_ORDER{Fetch::Immediate, Fetch::PriorityBased, Fetch::Steal};
constexpr static FetchOrderArray IMMEDIATE_ONLY_FETCH_ORDER{Fetch::Immediate, Fetch::Immediate, Fetch::Immediate};
constexpr static FetchOrderArray PRIORITY_BASED_ONLY_FETCH_ORDER{Fetch::PriorityBased, Fetch::PriorityBased, Fetch::PriorityBased};
constexpr static FetchOrderArray STEAL_ONLY_FETCH_ORDER{Fetch::Steal, Fetch::Steal, Fetch::Steal};

private:
struct WorkPayload
{
JobWorker* self = nullptr;
JobSystem* system = nullptr;
};

struct IsAwakeFunctor
{
JobWorker& worker;

bl operator()() const
{
return worker.IsAwake();
}
};

constexpr static siz SLEEP_STATE = 0;
constexpr static siz LOWEST_AWAKE_STATE = 1;

Expand All @@ -58,7 +51,6 @@ namespace np::jsys
mem::sptr<condition> _sleep_condition;
mutexed_wrapper<con::queue<mem::sptr<Job>>> _immediate_jobs;
mutexed_wrapper<con::vector<JobWorker*>> _coworkers;
mutexed_wrapper<FetchOrderArray> _fetch_order;

static void WorkProcedure(const WorkPayload& payload);

Expand Down Expand Up @@ -178,8 +170,7 @@ namespace np::jsys
_thread(nullptr),
_sleep_condition(nullptr),
_immediate_jobs(),
_coworkers(),
_fetch_order(DEFAULT_FETCH_ORDER)
_coworkers()
{}

JobWorker(JobWorker&& other) noexcept:
Expand All @@ -189,8 +180,7 @@ namespace np::jsys
_thread(::std::move(other._thread)),
_sleep_condition(::std::move(other._sleep_condition)),
_immediate_jobs(::std::move(other._immediate_jobs)),
_coworkers(::std::move(other._coworkers)),
_fetch_order(::std::move(other._fetch_order))
_coworkers(::std::move(other._coworkers))
{}

virtual ~JobWorker()
Expand All @@ -210,16 +200,6 @@ namespace np::jsys
}
}

FetchOrderArray GetFetchOrder()
{
return *_fetch_order.get_access();
}

void SetFetchOrder(const FetchOrderArray& fetch_order)
{
*_fetch_order.get_access() = fetch_order;
}

void AddCoworker(JobWorker& coworker)
{
// intentionally not checking if we have this coworker already
Expand Down
44 changes: 2 additions & 42 deletions src/JobSystem/JobWorker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,59 +12,19 @@

namespace np::jsys
{
void JobWorker::WorkProcedure(const JobWorker::WorkPayload& payload)
void JobWorker::WorkProcedure(const WorkPayload& payload)
{
NP_ENGINE_PROFILE_SCOPE("WorkerThreadProcedure: " + to_str(payload.self->_id));

struct IsAwakeFunctor
{
JobWorker& worker;

bl operator()() const
{
return worker.IsAwake();
}
};

JobWorker& self = *payload.self;
JobSystem& system = *payload.system;
mutex sleep_mutex;
general_lock sleep_lock(sleep_mutex);
IsAwakeFunctor is_awake_functor{self};
FetchOrderArray fetch_order;
bl successful_try;

while (self._keep_working.load(mo_acquire))
{
successful_try = false;
fetch_order = self.GetFetchOrder();
for (const Fetch& fetch : fetch_order)
{
switch (fetch)
{
case Fetch::Immediate:
successful_try |= self.TryImmediateJob();
break;

case Fetch::PriorityBased:
successful_try |= self.TryPriorityBasedJob(system);
break;

case Fetch::Steal:
successful_try |= self.TryStealingJob();
break;

case Fetch::None:
default:
successful_try |= false; // redundant - if true, the worker would never sleep
break;
}

if (successful_try || !self._keep_working.load(mo_acquire))
break;
}

if (!successful_try && self.ShouldSleep())
if (!(self.TryImmediateJob() || self.TryPriorityBasedJob(system) || self.TryStealingJob()) && self.ShouldSleep())
self._sleep_condition->wait(sleep_lock, ::std::ref(is_awake_functor)); // preference: no lambda - ew

self.ResetWakeCounter();
Expand Down
13 changes: 0 additions & 13 deletions test/include/NP-Engine-Tester.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -753,21 +753,8 @@ namespace np::app
{
jsys::JobSystem& job_system = _services->GetJobSystem();
con::vector<jsys::JobWorker>& job_workers = job_system.GetJobWorkers();
using Fetch = jsys::JobWorker::Fetch;
using FetchOrderArray = jsys::JobWorker::FetchOrderArray;

NP_ENGINE_ASSERT(thr::Thread::HardwareConcurrency() >= 4, "NP Engine Test requires at least four cores");

// common JobWorker fetch orders:
FetchOrderArray default_order = jsys::JobWorker::DEFAULT_FETCH_ORDER;
FetchOrderArray steal_only_order = jsys::JobWorker::STEAL_ONLY_FETCH_ORDER;
FetchOrderArray immediate_only_order = jsys::JobWorker::IMMEDIATE_ONLY_FETCH_ORDER;
FetchOrderArray priority_only_order = jsys::JobWorker::PRIORITY_BASED_ONLY_FETCH_ORDER;

// custom JobWorker fetch orders:
FetchOrderArray nosy_coworker_order{Fetch::Steal, Fetch::Immediate, Fetch::None};
FetchOrderArray lousy_order{Fetch::None, Fetch::None, Fetch::None};
FetchOrderArray others_then_me_order{Fetch::Steal, Fetch::PriorityBased, Fetch::Immediate};
}

public:
Expand Down

0 comments on commit dedf22e

Please sign in to comment.