BeeeOn Gateway  v2020.3.1-2-g6f737dc
Platform to interconnect the IoT world
WaitCondition.h
1 #pragma once
2 
3 #include <Poco/AtomicCounter.h>
4 #include <Poco/Condition.h>
5 #include <Poco/Mutex.h>
6 #include <Poco/Timespan.h>
7 
8 namespace BeeeOn {
9 
20 public:
21  WaitCondition(bool repeat = true);
22  ~WaitCondition();
23 
30  class Broadcaster {
31  public:
32  Broadcaster(WaitCondition &condition);
33 
38  ~Broadcaster();
39 
44  void broadcast();
45 
46  private:
47  bool m_broadcasted;
48  WaitCondition &m_condition;
49  };
50 
57  void wait(const Poco::Timespan &timeout = -1);
58 
65  bool tryWait(const Poco::Timespan &timeout = -1);
66 
70  void broadcast();
71 
72 protected:
78  long toMilliseconds(const Poco::Timespan &timeout) const;
79 
83  bool doWait(long ms);
84 
85 private:
86  Poco::FastMutex m_lock;
87  Poco::Condition m_condition;
88  bool m_canRepeat;
89  Poco::AtomicCounter m_broadcasted;
90 };
91 
92 }
void broadcast()
Broadcast that the condition has been met.
Definition: WaitCondition.cpp:75
bool tryWait(const Poco::Timespan &timeout=-1)
Wait until the condition is broadcasted. Negative timeout would block infinitly (until broadcasted)...
Definition: WaitCondition.cpp:54
~Broadcaster()
It calls broadcast() internally to make sure that the given wait condition has been broadcasted...
Definition: WaitCondition.cpp:14
Broadcaster can be used to ensure that the broadcast is called even in cast when an unexpected except...
Definition: WaitCondition.h:30
void wait(const Poco::Timespan &timeout=-1)
Wait until the condition is broadcasted. Negative timeout would block infinitly (until broadcasted)...
Definition: WaitCondition.cpp:48
long toMilliseconds(const Poco::Timespan &timeout) const
Convert the given timeout into milliseconds. Negative value is converted to -1. Timeout shorter then ...
Definition: WaitCondition.cpp:37
WaitCondition works as a barrier that waits until some condition is met. When the condition is met an...
Definition: WaitCondition.h:19
void broadcast()
Call WaitCondition::broadcast() unless it has already been called.
Definition: WaitCondition.cpp:19
bool doWait(long ms)
The actual waiting logic.
Definition: WaitCondition.cpp:59