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) 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_PLAY_TICKSERVICE_H
38 #define STEAM_PLAY_TICKSERVICE_H
39 
40 
41 #include "lib/error.hpp"
42 #include "lib/thread.hpp"
43 
44 #include <unistd.h> // for usleep()
45 
46 #include <functional>
47 #include <limits>
48 
49 
50 namespace steam {
51 namespace node {
52 
53  using std::function;
54  using std::bind;
55 
56 
57 
58  /********************************************************/
64  {
65  typedef function<void(void)> Tick;
66  volatile uint timespan_;
67 
69  static const uint POLL_TIMEOUT = 1000;
70 
71  public:
72  TickService (Tick callback)
73  : ThreadJoinable("Tick generator (dummy)"
74  , bind (&TickService::timerLoop, this, callback)
75  )
76  {
77  INFO (steam, "TickService started.");
78  }
79 
80  ~TickService ()
81  {
82  timespan_ = 0;
83  this->join();
84  usleep (200000); // additional delay allowing GTK to dispatch the last output
85 
86  INFO (steam, "TickService shutdown.");
87  }
88 
89 
95  void activate (uint fps)
96  {
97  REQUIRE ( 0==fps
98  ||( 1000000/fps < std::numeric_limits<uint>::max()
99  && 1000000/fps > POLL_TIMEOUT));
100  if (fps)
101  timespan_ = 1000000/fps; // microseconds per tick
102  else
103  timespan_ = POLL_TIMEOUT;
104  }
105 
106 
107  private:
108  void timerLoop(Tick periodicFun)
109  {
110  timespan_ = POLL_TIMEOUT;
111  while (0 < timespan_)
112  {
113  if (timespan_ > POLL_TIMEOUT)
114  periodicFun();
115 
116  usleep (timespan_);
117  }
118  TRACE (proc_dbg, "Tick Thread timer loop exiting...");
119  }
120 
121  };
122 
123 
124 
125 
126 }} // namespace steam::node
127 #endif
128 
Variant of the standard case, requiring to wait and join() on the termination of this thread...
Definition: thread.hpp:676
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.
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