flow  3.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Pages
timer.h
Go to the documentation of this file.
1 #if !defined(FLOW_TIMER_H)
2  #define FLOW_TIMER_H
3 
4 #include <atomic>
5 #include <chrono>
6 #include <condition_variable>
7 #include <functional>
8 #include <map>
9 #include <mutex>
10 #include <thread>
11 #include <vector>
12 
16 
17 namespace flow
18 {
19 
21 class timer
22 {
23  std::atomic<bool> d_stop_a;
24 
25 protected:
26  typedef std::map<size_t, const std::function<void ()>> listeners_t;
28  size_t d_next_index;
29 
30  std::mutex d_listeners_m;
31 
32 public:
33  timer() : d_stop_a(false), d_next_index(0) {}
34 
35  virtual ~timer() {}
36 
38  virtual bool stopped() const
39  {
40  return d_stop_a;
41  }
42 
44  virtual void stop()
45  {
46  d_stop_a = true;
47  }
48 
53  virtual size_t listen(const std::function<void ()>& listener)
54  {
55  std::lock_guard<std::mutex> lg(d_listeners_m);
56  d_listeners.insert(std::make_pair(d_next_index, listener));
57  return d_next_index++;
58  }
59 
63  virtual void ignore(const size_t token)
64  {
65  std::lock_guard<std::mutex> lg(d_listeners_m);
66  d_listeners.erase(token);
67  }
68 
72  virtual void operator()() = 0;
73 };
74 
76 class monotonous_timer : public timer
77 {
78  std::chrono::milliseconds d_interval;
79 
80  std::condition_variable d_stopped_cv;
81  std::mutex d_stopped_m;
82 
83 public:
85  template<typename Duration>
86  monotonous_timer(const Duration& interval) : d_interval(std::chrono::duration_cast<std::chrono::milliseconds>(interval)) {}
87 
88  virtual ~monotonous_timer() {}
89 
90  virtual void stop()
91  {
92  timer::stop();
93 
94  d_stopped_cv.notify_one();
95  }
96 
98  virtual void operator()()
99  {
100  while(!stopped())
101  {
102  {
103  std::lock_guard<std::mutex> lg(d_listeners_m);
104  for(auto& listener : d_listeners)
105  {
106  listener.second();
107  }
108  }
109 
110  // Wait until time has expired OR timer is stopped.
111  std::unique_lock<std::mutex> l_stopped(d_stopped_m);
112  d_stopped_cv.wait_for(l_stopped, d_interval);
113  }
114  }
115 };
116 
117 }
118 
119 #endif
120 
121 /*
122  (C) Copyright Thierry Seegers 2010-2012. Distributed under the following license:
123 
124  Boost Software License - Version 1.0 - August 17th, 2003
125 
126  Permission is hereby granted, free of charge, to any person or organization
127  obtaining a copy of the software and accompanying documentation covered by
128  this license (the "Software") to use, reproduce, display, distribute,
129  execute, and transmit the Software, and to prepare derivative works of the
130  Software, and to permit third-parties to whom the Software is furnished to
131  do so, all subject to the following:
132 
133  The copyright notices in the Software and this entire statement, including
134  the above license grant, this restriction and the following disclaimer,
135  must be included in all copies of the Software, in whole or in part, and
136  all derivative works of the Software, unless such copies or derivative
137  works are solely in the form of machine-executable object code generated by
138  a source language processor.
139 
140  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
141  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
142  FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
143  SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
144  FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
145  ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
146  DEALINGS IN THE SOFTWARE.
147 */