Lumiera  0.pre.03
»edityourfreedom«
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 PROC_ENGINE_WORKER_DUMMY_TICK_H
38 #define PROC_ENGINE_WORKER_DUMMY_TICK_H
39 
40 
41 #include "lib/error.hpp"
43 
44 #include <functional>
45 #include <limits>
46 
47 
48 namespace proc {
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 (proc, "TickService started.");
76  }
77 
79  {
80  timespan_ = 0;
81  this->join();
82  usleep (200000); // additional delay allowing GTK to dispatch the last output
83 
84  INFO (proc, "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 proc::node
125 #endif
126 
lib::Result< void > join()
put the caller into a blocking wait until this thread has terminated.
volatile uint timespan_
Definition: dummy-tick.hpp:64
constexpr X const & max(X const &a, X const &b)
void activate(uint fps)
set the periodic timer to run with a given frequency, starting now.
Definition: dummy-tick.hpp:93
DummyTick(Tick callback)
Definition: dummy-tick.hpp:70
ThreadJoinable(Literal purpose, FUN &&operation, NoBugFlag logging_flag=&NOBUG_FLAG(thread))
TRACE(test,"inserted %d", data)
static const uint POLL_TIMEOUT
poll interval for new settings in wait state
Definition: dummy-tick.hpp:67
void timerLoop(Tick periodicFun)
Definition: dummy-tick.hpp:106
Tick generating service for a periodic callback, with adjustable frequency.
Definition: dummy-tick.hpp:60
Convenience front-end for basic thread handling needs.
Lumiera error handling (C++ interface).
Variant of the standard case, allowing additionally to join on the termination of this thread...
Proc-Layer implementation namespace root.
Definition: id-scheme.hpp:63
function< void(void)> Tick
Definition: dummy-tick.hpp:63