Lumiera  0.pre.03
»edit your freedom«
dummy-tick.hpp
Go to the documentation of this file.
1 /*
2  DUMMY-TICK.hpp - issuing timed callbacks
3 
4  Copyright (C) Lumiera.org
5  2009, Joel Holdsworth <joel@airwebreathe.org.uk>,
6  Hermann Vosseler <Ichthyostega@web.de>
7 
8  This program is free software; you can redistribute it and/or
9  modify it under the terms of the GNU General Public License as
10  published by the Free Software Foundation; either version 2 of
11  the License, or (at your option) any later version.
12 
13  This program is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with this program; if not, write to the Free Software
20  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 
22 */
23 
37 #ifndef STEAM_ENGINE_WORKER_DUMMY_TICK_H
38 #define STEAM_ENGINE_WORKER_DUMMY_TICK_H
39 
40 
41 #include "lib/error.hpp"
42 #include "lib/thread.hpp"
43 
44 #include <functional>
45 #include <limits>
46 
47 
48 namespace steam {
49 namespace node {
50 
51  using std::function;
52  using std::bind;
53 
54 
55 
56  /********************************************************/
60  class DummyTick
62  {
63  typedef function<void(void)> Tick;
64  volatile uint timespan_;
65 
67  static const uint POLL_TIMEOUT = 1000;
68 
69  public:
70  DummyTick (Tick callback)
71  : ThreadJoinable("Tick generator (dummy)"
72  , bind (&DummyTick::timerLoop, this, callback)
73  )
74  {
75  INFO (steam, "TickService started.");
76  }
77 
78  ~DummyTick ()
79  {
80  timespan_ = 0;
81  this->join();
82  usleep (200000); // additional delay allowing GTK to dispatch the last output
83 
84  INFO (steam, "TickService shutdown.");
85  }
86 
87 
93  void activate (uint fps)
94  {
95  REQUIRE ( 0==fps
96  ||( 1000000/fps < std::numeric_limits<uint>::max()
97  && 1000000/fps > POLL_TIMEOUT));
98  if (fps)
99  timespan_ = 1000000/fps; // microseconds per tick
100  else
101  timespan_ = POLL_TIMEOUT;
102  }
103 
104 
105  private:
106  void timerLoop(Tick periodicFun)
107  {
108  timespan_ = POLL_TIMEOUT;
109  while (0 < timespan_)
110  {
111  if (timespan_ > POLL_TIMEOUT)
112  periodicFun();
113 
114  usleep (timespan_);
115  }
116  TRACE (proc_dbg, "Tick Thread timer loop exiting...");
117  }
118 
119  };
120 
121 
122 
123 
124 }} // namespace steam::node
125 #endif
126 
Variant of the standard case, requiring to wait and join() on the termination of this thread...
Definition: thread.hpp:676
Tick generating service for a periodic callback, with adjustable frequency.
Definition: dummy-tick.hpp:60
void activate(uint fps)
set the periodic timer to run with a given frequency, starting now.
Definition: dummy-tick.hpp:93
Steam-Layer implementation namespace root.
ThreadJoinable(string const &, FUN &&, ARGS &&...) -> ThreadJoinable< std::invoke_result_t< FUN, ARGS... >>
deduction guide: find out about result value to capture from a generic callable.
lib::Result< void > join()
put the caller into a blocking wait until this thread has terminated
Definition: thread.hpp:693
Convenience front-end to simplify and codify basic thread handling.
Lumiera error handling (C++ interface).
static const uint POLL_TIMEOUT
poll interval for new settings in wait state
Definition: dummy-tick.hpp:67