BeeeOn Gateway  v2020.3.1-2-g6f737dc
Platform to interconnect the IoT world
LoopRunner.h
1 #pragma once
2 
3 #include <list>
4 
5 #include <Poco/Mutex.h>
6 #include <Poco/SharedPtr.h>
7 
8 #include "loop/StoppableRunnable.h"
9 #include "loop/StoppableLoop.h"
10 #include "util/Loggable.h"
11 
12 namespace BeeeOn {
13 
14 class LoopRunner : public StoppableLoop, public Loggable {
15 public:
16  typedef Poco::SharedPtr<LoopRunner> Ptr;
17 
18  LoopRunner();
19  ~LoopRunner();
20 
21  void addRunnable(StoppableRunnable::Ptr runnable);
22  void addLoop(StoppableLoop::Ptr loop);
23  void setAutoStart(bool enable);
24 
28  void setStopParallel(bool parallel);
29 
30  void start() override;
31  void stop() override;
32  void autoStart();
33 
34 protected:
39  class Stopper : public Poco::Runnable, Loggable {
40  public:
41  Stopper();
42  Stopper(StoppableLoop::Ptr loop);
43 
44  void run() override;
45 
46  private:
47  StoppableLoop::Ptr m_loop;
48  };
49 
57  void stopAll(std::list<Stopper> &list);
58 
63  void stopParallel(std::list<Stopper> &list);
64 
65 private:
66  bool m_autoStart;
67  bool m_stopParallel;
68  Poco::FastMutex m_lock;
69  std::list<StoppableLoop::Ptr> m_loops;
70  std::list<Stopper> m_started;
71 };
72 
73 }
void setStopParallel(bool parallel)
Set whether stopAll() should run in parallel.
Definition: LoopRunner.cpp:62
void stopAll(std::list< Stopper > &list)
Stop all loop in reverse order. If the property stopParallel is true then the loops are stopped in pa...
Definition: LoopRunner.cpp:83
Wrapper around StoppableLoop that allows to stop it from inside a thread.
Definition: LoopRunner.h:39
Definition: StoppableLoop.h:17
void stopParallel(std::list< Stopper > &list)
Stops the given list of loops in parallel. Loops that have been stopped are removed from the list...
Definition: LoopRunner.cpp:98
void stop() override
Definition: LoopRunner.cpp:67
Definition: LoopRunner.h:14
Definition: Loggable.h:19
void start() override
Definition: LoopRunner.cpp:130