]>
Dogcows Code - chaz/yoink/blob - src/moof/timer.hh
2 /*] Copyright (c) 2009-2010, Charles McGarvey [**************************
3 **] All rights reserved.
7 * Distributable under the terms and conditions of the 2-clause BSD license;
8 * see the file COPYING for a complete text of the license.
10 **************************************************************************/
12 #ifndef _MOOF_TIMER_HH_
13 #define _MOOF_TIMER_HH_
17 * Functions for measuring time in a friendly unit.
20 #include <boost/bind.hpp>
21 #include <boost/function.hpp>
23 #include <moof/hash.hh>
24 #include <moof/math.hh>
31 * A class to represent a timer for scheduled events. The timer events
32 * will be executed on or sometime after their schedculed time. The
33 * runloop associated with the current running view will make an attempt to
34 * fire any expired timers as close to their scheduled times as possible.
41 * A type for the state of a timer.
45 invalid
= -1, /// Timer is not scheduled.
46 relative
= 0, /// Timer is scheduled by a relative time.
47 absolute
= 1, /// Timer is scheduled by an absolute time.
48 repeat
= 2 /// Timer is scheduled by a periodic time.
52 * Function protocol for a timer event handler. A function matching
53 * this protocol will be called when the timer expires. The function
54 * takes two parameters: the timer object that just expired, and the
57 typedef boost::function
<void(timer
&,scalar
)> function
;
61 * Construct an invalid (uninitialized) timer.
65 absolute_(SCALAR(0.0)) {}
68 * Construct a scheduled timer.
69 * \param function The event handler.
70 * \param seconds The number of seconds; the meaning of this depends on
71 * the mode of the timer.
72 * \param mode The timer mode. If the mode is relative (default), the
73 * seconds parameter is the number of seconds from the current time to
74 * wait before expiring the timer. If the mode is absolute, the
75 * seconds parameter is the number of seconds from some arbitrary,
76 * fixed time in the past. If the mode is repeat, the seconds
77 * parameter is the number of seconds from now to wait before expiring
78 * the timer, at which time the timer will be rescheduled to expired
79 * again at that many seconds from the expiration time. A repeating
80 * timer can be invalidated manually using invalidate().
82 timer(const function
& function
, scalar seconds
, mode mode
= relative
)
84 init(function
, seconds
, mode
);
88 * Deconstruct a timer. This will automagically invalidate the timer,
89 * so it will not expire or fire an event.
98 * Initialize a timer with a scheduled time. If the timer is already
99 * scheduled, its prior schedule will be invalidated and replaced by
101 * \param function The event handler.
102 * \param seconds The number of seconds; the meaning of this depends on
103 * the mode of the timer.
104 * \param mode The timer mode. If the mode is relative (default), the
105 * seconds parameter is the number of seconds from the current time to
106 * wait before expiring the timer. If the mode is absolute, the
107 * seconds parameter is the number of seconds from some arbitrary,
108 * fixed time in the past. If the mode is repeat, the seconds
109 * parameter is the number of seconds from now to wait before expiring
110 * the timer, at which time the timer will be rescheduled to expired
111 * again at that many seconds from the expiration time. A repeating
112 * timer can be invalidated manually using invalidate().
114 void init(const function
& function
,
116 mode mode
= relative
);
120 * Get whether or not the timer is valid. If a timer is valid, it is
121 * still scheduled to expired. You can get the time remaining from
122 * seconds_remaining().
124 bool is_valid() const;
127 * Manually invalidated the timer, removing any schedule such that the
128 * timer will not expired and no event will be fired.
134 * Manually fire the timer event. Usually, the timer event will be
135 * fired when the timer expires, but this can be used to fire it
136 * prematurely. If the timer is scheduled, it will be invalidated. If
137 * the timer is already invalid (but is initialized with an event
138 * handler), the event will be fired and the timer will remain invalid.
144 * Get the number of seconds remaining before the timer is scheduled to
145 * expired. If the timer is invalid, the retured value will be
149 scalar
seconds_remaining() const;
152 * Get whether or not the timer is expired. A timer on a repeating
153 * schedule will never be expired since it will always have a scheduled
154 * expiration time in the future. If the timer is expired but not
155 * invalid, the timer event has not yet fired; the timer will be
156 * invalidated when it does fire.
157 * \return True if the timer is expired, false otherwise.
159 bool is_expired() const;
162 * Get whether or not the timer is on a repeating schedule.
163 * \return True if the timer is repeating, false otherwise.
165 bool is_repeating() const;
169 * Get the number of seconds since a fixed, arbitrary point in the
173 static scalar
ticks();
177 * Put the thread to sleep for a certain period of time. If absolute
178 * is true, then it will sleep until seconds after the fixed time in
179 * the past. If absolute is false, it will sleep for seconds starting
180 * now. Unlike system sleep functions, this one automatically resumes
181 * sleep if sleep was interrupted by a signal. Therefore, calling this
182 * function is guaranteed to sleep for the requested amount of time
183 * (and maybe longer).
184 * \param seconds Number of seconds.
185 * \param mode The timer mode.
187 static void sleep(scalar seconds
, mode mode
= relative
);
191 * Get the absolute time when the next timer is scheduled to expire.
192 * \return Absolute time, in seconds.
194 static scalar
next_expiration()
196 return next_expiration_
;
201 * Fire any timers which are not yet invalidated but have an expiration
204 static void fire_expired_timers();
207 * Fire any timers which are not yet invalidated but have an expiration
208 * time before a given absolute time.
210 static void fire_expired_timers(scalar t
);
215 static unsigned new_identifier();
216 static scalar
find_next_expiration();
224 static scalar next_expiration_
;
225 static hash
<unsigned,timer
*,hash_function
> timers_
;
231 #endif // _MOOF_TIMER_HH_
This page took 0.044928 seconds and 4 git commands to generate.