diff --git a/emper/Dispatcher.cpp b/emper/Dispatcher.cpp index 0bef0cc50eb8ac37a53e42e9528cac5689749684..0cc1fb33c7cdfb87d5aa5d942d04b64ff2c2e171 100644 --- a/emper/Dispatcher.cpp +++ b/emper/Dispatcher.cpp @@ -2,6 +2,9 @@ // Copyright © 2020 Florian Schmaus #include "Dispatcher.hpp" +#include <pthread.h> // for pthread_yield + +#include "Emper.hpp" #include "Runtime.hpp" // for Runtime thread_local const Fiber* Dispatcher::currentFiber; @@ -11,3 +14,11 @@ auto Dispatcher::getDispatchLoop() -> func_t { } void Dispatcher::putRuntimeWorkerToSleep() { runtime.dispatcherLoopSleep(); } + +void Dispatcher::dispatchLoopDoSleep() { + if constexpr (emper::WORKER_SLEEP) { + putRuntimeWorkerToSleep(); + } else { + pthread_yield(); + } +} diff --git a/emper/Dispatcher.hpp b/emper/Dispatcher.hpp index 2a181fb53ff903a5cafe26059580b011f4c70e33..aeccd68e9433e87b93ae876bc81623fbe72fa281 100644 --- a/emper/Dispatcher.hpp +++ b/emper/Dispatcher.hpp @@ -19,6 +19,8 @@ class Dispatcher : public Logger<LogSubsystem::DISP> { Runtime& runtime; + void dispatchLoopDoSleep(); + virtual void dispatchLoop() = 0; auto getDispatchLoop() -> func_t; diff --git a/emper/Emper.hpp b/emper/Emper.hpp index 4e5545d2a76d888d1d60fb4ee3f2c2bbdf60e475..76fa35e31ccb4ed0defe0e1511cba81f1b05e5d1 100644 --- a/emper/Emper.hpp +++ b/emper/Emper.hpp @@ -5,6 +5,7 @@ #include <emper-config.h> namespace emper { + static const bool STATS = #ifdef EMPER_STATS true @@ -13,4 +14,12 @@ static const bool STATS = #endif ; +static const bool WORKER_SLEEP = +#ifdef EMPER_WORKER_SLEEP + true +#else + false +#endif + ; + } // namespace emper diff --git a/emper/strategies/laws/LawsDispatcher.cpp b/emper/strategies/laws/LawsDispatcher.cpp index 1c21d2b40b205a72c1a02dbaceabfb1d1430a8e8..04bfbde1be06b7b5c9c19e54abd2fe7c812f1f10 100644 --- a/emper/strategies/laws/LawsDispatcher.cpp +++ b/emper/strategies/laws/LawsDispatcher.cpp @@ -9,17 +9,13 @@ #include "Fiber.hpp" // for Fiber #include "LawsStrategy.hpp" // for LawsStrategy, LawsStrategy::FiberSource #include "Runtime.hpp" -#include "emper-config.h" // for EMPER_WORKER_SLEEP void LawsDispatcher::dispatchLoop() { while (true) { Fiber* const fiber = runtime.nextFiber(); if (!fiber) { -#ifdef EMPER_WORKER_SLEEP - putRuntimeWorkerToSleep(); -#else - pthread_yield(); -#endif + dispatchLoopDoSleep(); + continue; } @@ -28,8 +24,7 @@ void LawsDispatcher::dispatchLoop() { // is runnable. if (isRunnable(fiber)) { if constexpr (emper::STATS) { - auto fiberSource = - static_cast<LawsStrategy::FiberSource>(fiber->getFlag()); + auto fiberSource = static_cast<LawsStrategy::FiberSource>(fiber->getFlag()); switch (fiberSource) { case LawsStrategy::FiberSource::fromPriority: lawsStrategy.dispatchedFiberFromPriority.fetch_add(1, std::memory_order_relaxed); diff --git a/emper/strategies/laws/LawsScheduler.cpp b/emper/strategies/laws/LawsScheduler.cpp index 57a33406870d8df8f73064549f3719705288d2ef..2c867f1893d098c54277030868883a962c44a88d 100644 --- a/emper/strategies/laws/LawsScheduler.cpp +++ b/emper/strategies/laws/LawsScheduler.cpp @@ -9,7 +9,6 @@ #include "Emper.hpp" #include "LawsStrategy.hpp" // IWYU pragma: keep #include "Runtime.hpp" -#include "emper-config.h" // for EMPER_WORKER_SLEEP #define EMPER_OVERFLOW_QUEUE @@ -69,9 +68,9 @@ scheduleToLocalWsQueue: } } -#ifdef EMPER_WORKER_SLEEP - notifyRuntimeAboutNewWork(); -#endif + if constexpr (emper::WORKER_SLEEP) { + notifyRuntimeAboutNewWork(); + } } auto LawsScheduler::nextFiber() -> Fiber* { diff --git a/emper/strategies/ws/WsDispatcher.cpp b/emper/strategies/ws/WsDispatcher.cpp index 14202e7bce39766a4aec7abb1d111fbcb42cadde..9e3394acd2c5ab66bd3bb8511bde2a16989d9fdc 100644 --- a/emper/strategies/ws/WsDispatcher.cpp +++ b/emper/strategies/ws/WsDispatcher.cpp @@ -2,8 +2,7 @@ // Copyright © 2020 Florian Schmaus #include "WsDispatcher.hpp" -#include "Runtime.hpp" // for Runtime -#include "emper-config.h" // for EMPER_WORKER_SLEEP +#include "Runtime.hpp" // for Runtime class Fiber; @@ -11,11 +10,8 @@ void WsDispatcher::dispatchLoop() { while (true) { const Fiber* fiber = runtime.nextFiber(); if (!fiber) { -#ifdef EMPER_WORKER_SLEEP - putRuntimeWorkerToSleep(); -#else - pthread_yield(); -#endif + dispatchLoopDoSleep(); + continue; } diff --git a/emper/strategies/ws/WsScheduler.cpp b/emper/strategies/ws/WsScheduler.cpp index e7cd5ce30fa58644c2cb64faf51f1d81bd3cfe13..6b081a264be5d768b49cf92ed3623b22a73e1c22 100644 --- a/emper/strategies/ws/WsScheduler.cpp +++ b/emper/strategies/ws/WsScheduler.cpp @@ -9,7 +9,6 @@ #include "Debug.hpp" #include "Emper.hpp" #include "Runtime.hpp" -#include "emper-config.h" #include "strategies/ws/WsStrategy.hpp" class Fiber; @@ -42,9 +41,9 @@ void WsScheduler::schedule(Fiber& fiber) { wsStrategy.scheduledFibers.fetch_add(1, std::memory_order_relaxed); } -#ifdef EMPER_WORKER_SLEEP - notifyRuntimeAboutNewWork(); -#endif + if constexpr (emper::WORKER_SLEEP) { + notifyRuntimeAboutNewWork(); + } } auto WsScheduler::nextFiber() -> Fiber* {