From dedf22e1e8c4048f7d35a34ceca8ed8fb5a63390 Mon Sep 17 00:00:00 2001 From: Nathan Phipps Date: Wed, 17 Jan 2024 22:53:01 -0600 Subject: [PATCH] removed JobWorker fetch order --- include/NP-Engine/JobSystem/JobWorker.hpp | 44 +++++++---------------- src/JobSystem/JobWorker.cpp | 44 ++--------------------- test/include/NP-Engine-Tester.hpp | 13 ------- 3 files changed, 14 insertions(+), 87 deletions(-) diff --git a/include/NP-Engine/JobSystem/JobWorker.hpp b/include/NP-Engine/JobSystem/JobWorker.hpp index 7a16986..dbe9dee 100644 --- a/include/NP-Engine/JobSystem/JobWorker.hpp +++ b/include/NP-Engine/JobSystem/JobWorker.hpp @@ -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; - - 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; @@ -58,7 +51,6 @@ namespace np::jsys mem::sptr _sleep_condition; mutexed_wrapper>> _immediate_jobs; mutexed_wrapper> _coworkers; - mutexed_wrapper _fetch_order; static void WorkProcedure(const WorkPayload& payload); @@ -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: @@ -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() @@ -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 diff --git a/src/JobSystem/JobWorker.cpp b/src/JobSystem/JobWorker.cpp index 878fd68..8f286c4 100644 --- a/src/JobSystem/JobWorker.cpp +++ b/src/JobSystem/JobWorker.cpp @@ -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(); diff --git a/test/include/NP-Engine-Tester.hpp b/test/include/NP-Engine-Tester.hpp index 6bc0fee..2aa62ec 100644 --- a/test/include/NP-Engine-Tester.hpp +++ b/test/include/NP-Engine-Tester.hpp @@ -753,21 +753,8 @@ namespace np::app { jsys::JobSystem& job_system = _services->GetJobSystem(); con::vector& 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: