From e116797352f390cf70aaf83a82c6ebe2e2ef0718 Mon Sep 17 00:00:00 2001 From: Florian Schmaus <flow@cs.fau.de> Date: Tue, 10 Nov 2020 17:03:28 +0100 Subject: [PATCH] Use "if constexpr" for WORKER_SLEEP --- emper/Dispatcher.cpp | 11 +++++++++++ emper/Dispatcher.hpp | 2 ++ emper/Emper.hpp | 9 +++++++++ emper/strategies/laws/LawsDispatcher.cpp | 11 +++-------- emper/strategies/laws/LawsScheduler.cpp | 7 +++---- emper/strategies/ws/WsDispatcher.cpp | 10 +++------- emper/strategies/ws/WsScheduler.cpp | 7 +++---- 7 files changed, 34 insertions(+), 23 deletions(-) diff --git a/emper/Dispatcher.cpp b/emper/Dispatcher.cpp index 0bef0cc5..0cc1fb33 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 2a181fb5..aeccd68e 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 4e5545d2..76fa35e3 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 1c21d2b4..04bfbde1 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 57a33406..2c867f18 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 14202e7b..9e3394ac 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 e7cd5ce3..6b081a26 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* { -- GitLab