Lumiera  0.pre.03
»edit your freedom«
tick-service.hpp
Go to the documentation of this file.
1 /*
2  TICK-SERVICE.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_PLAY_TICKSERVICE_H
29 #define STEAM_PLAY_TICKSERVICE_H
30 
31 
32 #include "lib/error.hpp"
33 #include "lib/thread.hpp"
34 
35 #include <unistd.h> // for usleep()
36 
37 #include <functional>
38 #include <limits>
39 
40 
41 namespace steam {
42 namespace node {
43 
44  using std::function;
45  using std::bind;
46 
47 
48 
49  /********************************************************/
55  {
56  typedef function<void(void)> Tick;
57  volatile uint timespan_;
58 
60  static const uint POLL_TIMEOUT = 1000;
61 
62  public:
63  TickService (Tick callback)
64  : ThreadJoinable("Tick generator (dummy)"
65  , bind (&TickService::timerLoop, this, callback)
66  )
67  {
68  INFO (steam, "TickService started.");
69  }
70 
71  ~TickService ()
72  {
73  timespan_ = 0;
74  this->join();
75  usleep (200000); // additional delay allowing GTK to dispatch the last output
76 
77  INFO (steam, "TickService shutdown.");
78  }
79 
80 
86  void activate (uint fps)
87  {
88  REQUIRE ( 0==fps
89  ||( 1000000/fps < std::numeric_limits<uint>::max()
90  && 1000000/fps > POLL_TIMEOUT));
91  if (fps)
92  timespan_ = 1000000/fps; // microseconds per tick
93  else
94  timespan_ = POLL_TIMEOUT;
95  }
96 
97 
98  private:
99  void timerLoop(Tick periodicFun)
100  {
101  timespan_ = POLL_TIMEOUT;
102  while (0 < timespan_)
103  {
104  if (timespan_ > POLL_TIMEOUT)
105  periodicFun();
106 
107  usleep (timespan_);
108  }
109  TRACE (proc_dbg, "Tick Thread timer loop exiting...");
110  }
111 
112  };
113 
114 
115 
116 
117 }} // namespace steam::node
118 #endif
119 
Variant of the standard case, requiring to wait and join() on the termination of this thread...
Definition: thread.hpp:668
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.
Tick generating service for a periodic callback, with adjustable frequency.
void activate(uint fps)
set the periodic timer to run with a given frequency, starting now.
Lumiera error handling (C++ interface).
static const uint POLL_TIMEOUT
poll interval for new settings in wait state