BeeeOn Gateway  v2020.3.1-2-g6f737dc
Platform to interconnect the IoT world
ParallelExecutor.h
1 #pragma once
2 
3 #include <queue>
4 #include <string>
5 
6 #include <Poco/AtomicCounter.h>
7 #include <Poco/AutoPtr.h>
8 #include <Poco/Event.h>
9 #include <Poco/Mutex.h>
10 #include <Poco/RefCountedObject.h>
11 #include <Poco/Timespan.h>
12 
13 #include "loop/StoppableLoop.h"
14 #include "util/AsyncExecutor.h"
15 #include "util/HavingThreadPool.h"
16 
17 namespace BeeeOn {
18 
29  public AsyncExecutor,
30  public StoppableRunnable,
31  public HavingThreadPool {
32 public:
35 
36  typedef Poco::SharedPtr<ParallelExecutor> Ptr;
37 
38  void setBaseName(const std::string &baseName);
39  void setContentedDelay(const Poco::Timespan &delay);
40 
44  void run() override;
45 
49  void stop() override;
50 
57  size_t flushDeferred();
58 
59  void invoke(std::function<void()> f) override;
60 
61 private:
65  class ThreadLambda :
66  public Poco::Runnable,
67  public Poco::RefCountedObject,
68  Loggable {
69  public:
70  typedef Poco::AutoPtr<ThreadLambda> Ptr;
71 
72  ThreadLambda(
73  std::function<void()> f,
74  Poco::Event &event,
75  const std::string &baseName);
76 
77  void start(Poco::ThreadPool &pool);
78  void run() override;
79 
80  private:
81  std::function<void()> m_func;
82  Poco::Event &m_event;
83  std::string m_baseName;
84  };
85 
91  void defer(ThreadLambda::Ptr tl);
92 
99  void startOrDefer(ThreadLambda::Ptr tl);
100 
101 private:
102  std::string m_baseName;
103  Poco::Timespan m_contentedDelay;
104  std::queue<ThreadLambda::Ptr> m_queue;
105  Poco::AtomicCounter m_stop;
106  Poco::FastMutex m_lock;
107  Poco::Event m_event;
108 };
109 
110 }
void invoke(std::function< void()> f) override
Definition: ParallelExecutor.cpp:186
Implementation of AsyncExecutor interface that invokes given procedures in parallel.
Definition: ParallelExecutor.h:28
size_t flushDeferred()
Definition: ParallelExecutor.cpp:166
Definition: HavingThreadPool.h:11
void run() override
Definition: ParallelExecutor.cpp:90
Definition: AsyncExecutor.h:12
Definition: Loggable.h:19
void stop() override
Definition: ParallelExecutor.cpp:136
Definition: StoppableRunnable.h:8