From 30254da95f93124e4f1806178dbf08eb9b6e08c4 Mon Sep 17 00:00:00 2001 From: Ichthyostega Date: Fri, 16 Dec 2016 19:21:06 +0100 Subject: [PATCH] Looper: implement core operation control logic --- src/proc/control/command-queue.hpp | 14 ++++++- src/proc/control/looper.hpp | 40 ++++++++++++------- src/proc/control/proc-dispatcher.cpp | 8 ++-- .../proc/control/dispatcher-looper-test.cpp | 4 +- 4 files changed, 46 insertions(+), 20 deletions(-) diff --git a/src/proc/control/command-queue.hpp b/src/proc/control/command-queue.hpp index 3a0e48f9e..721c4f70d 100644 --- a/src/proc/control/command-queue.hpp +++ b/src/proc/control/command-queue.hpp @@ -72,8 +72,18 @@ namespace control { /* == diagnostics == */ -// size_t size() const ; -// bool empty() const ; + size_t + size() const + { + TODO ("implement queue"); + return 0; + } + + bool + empty() const + { + return 0 == size(); + } }; ////////////////TODO 12/16 currently just fleshing out the API.... diff --git a/src/proc/control/looper.hpp b/src/proc/control/looper.hpp index ae80effc5..3fea9a8f8 100644 --- a/src/proc/control/looper.hpp +++ b/src/proc/control/looper.hpp @@ -48,7 +48,7 @@ //#include "lib/depend.hpp" //#include -//#include +#include @@ -57,6 +57,7 @@ namespace control { // using lib::Symbol; // using std::bind; +// using std::function; namespace { /** @@ -77,21 +78,38 @@ namespace control { */ class Looper { - bool shutdown = false; + using Predicate = std::function; + + bool shutdown_ = false; + bool disabled_ = false; + Predicate hasCommandsPending_; public: - Looper() + template + Looper(FUN determine_commands_are_waiting) + : hasCommandsPending_(determine_commands_are_waiting) { } - /* == working state == */ + // standard copy acceptable - bool isWorking() const { return false; } - bool isIdle() const { return false; } + + /* == working state logic == */ + + bool isDying() const { return shutdown_; } + bool isDisabled() const { return disabled_ or isDying(); } + bool isWorking() const { return hasCommandsPending_() and not isDisabled(); } bool needBuild() const { return false; } - bool isDisabled() const { return false; } - bool isDying() const { return shutdown; } + bool isIdle() const { return not (isWorking() or needBuild() or isDisabled()); } + /* == operation control == */ + + void + triggerShutdown() + { + shutdown_ = true; + } + /** state fusion to control (timed) wait */ bool requireAction() @@ -115,12 +133,6 @@ namespace control { return wakeTimeout_ms; } - - void - triggerShutdown() - { - shutdown = true; - } /* == diagnostics == */ // size_t size() const ; diff --git a/src/proc/control/proc-dispatcher.cpp b/src/proc/control/proc-dispatcher.cpp index f5ff88161..fb6d5cadf 100644 --- a/src/proc/control/proc-dispatcher.cpp +++ b/src/proc/control/proc-dispatcher.cpp @@ -67,7 +67,10 @@ namespace control { , bind (&DispatcherLoop::run, this, notification)) , commandService_(new SessionCommandService(*this)) , queue_() - , looper_() + , looper_([&]() -> bool + { + return not queue_.empty(); + }) { INFO (session, "Proc-Dispatcher running..."); } @@ -162,8 +165,7 @@ namespace control { void awaitAction() { - Lock(this).wait(looper_, - &Looper::requireAction, + Lock(this).wait(looper_, &Looper::requireAction, looper_.getTimeout()); } diff --git a/tests/core/proc/control/dispatcher-looper-test.cpp b/tests/core/proc/control/dispatcher-looper-test.cpp index 7d8f32974..6c76f95d8 100644 --- a/tests/core/proc/control/dispatcher-looper-test.cpp +++ b/tests/core/proc/control/dispatcher-looper-test.cpp @@ -57,7 +57,7 @@ namespace test { Looper install() { - return Looper{}; + return Looper([&](){ return has_commands_in_queue; }); } }; @@ -143,12 +143,14 @@ namespace test { CHECK ( looper.requireAction()); CHECK ( looper.isWorking()); CHECK (not looper.isIdle()); + CHECK (looper.shallLoop()); setup.has_commands_in_queue = false; CHECK (not looper.requireAction()); CHECK (not looper.isWorking()); CHECK ( looper.isIdle()); + CHECK (looper.shallLoop()); } };