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)
5  2009, Joel Holdsworth <joel@airwebreathe.org.uk>,
6  Hermann Vosseler <Ichthyostega@web.de>
7 
8   **Lumiera** is free software; you can redistribute it and/or modify it
9   under the terms of the GNU General Public License as published by the
10   Free Software Foundation; either version 2 of the License, or (at your
11   option) any later version. See the file COPYING for further details.
12 
13 */
14 
28 #ifndef STEAM_ENGINE_WORKER_DUMMY_TICK_H
29 #define STEAM_ENGINE_WORKER_DUMMY_TICK_H
30 
31 
32 #include "lib/error.hpp"
33 #include "lib/thread.hpp"
34 
35 #include <functional>
36 #include <limits>
37 
38 
39 namespace steam {
40 namespace node {
41 
42  using std::function;
43  using std::bind;
44 
45 
46 
47  /********************************************************/
51  class DummyTick
53  {
54  typedef function<void(void)> Tick;
55  volatile uint timespan_;
56 
58  static const uint POLL_TIMEOUT = 1000;
59 
60  public:
61  DummyTick (Tick callback)
62  : ThreadJoinable("Tick generator (dummy)"
63  , bind (&DummyTick::timerLoop, this, callback)
64  )
65  {
66  INFO (steam, "TickService started.");
67  }
68 
69  ~DummyTick ()
70  {
71  timespan_ = 0;
72  this->join();
73  usleep (200000); // additional delay allowing GTK to dispatch the last output
74 
75  INFO (steam, "TickService shutdown.");
76  }
77 
78 
84  void activate (uint fps)
85  {
86  REQUIRE ( 0==fps
87  ||( 1000000/fps < std::numeric_limits<uint>::max()
88  && 1000000/fps > POLL_TIMEOUT));
89  if (fps)
90  timespan_ = 1000000/fps; // microseconds per tick
91  else
92  timespan_ = POLL_TIMEOUT;
93  }
94 
95 
96  private:
97  void timerLoop(Tick periodicFun)
98  {
99  timespan_ = POLL_TIMEOUT;
100  while (0 < timespan_)
101  {
102  if (timespan_ > POLL_TIMEOUT)
103  periodicFun();
104 
105  usleep (timespan_);
106  }
107  TRACE (proc_dbg, "Tick Thread timer loop exiting...");
108  }
109 
110  };
111 
112 
113 
114 
115 }} // namespace steam::node
116 #endif
117 
Variant of the standard case, requiring to wait and join() on the termination of this thread...
Definition: thread.hpp:668
Tick generating service for a periodic callback, with adjustable frequency.
Definition: dummy-tick.hpp:51
void activate(uint fps)
set the periodic timer to run with a given frequency, starting now.
Definition: dummy-tick.hpp:84
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:685
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:58