]>
Dogcows Code - chaz/openbox/blob - otk/timer.hh
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
6 @brief Contains the Timer class, used for timed callbacks.
18 //! The Timer class implements timed callbacks.
20 The Timer class can be used to have a callback fire after a given time
21 interval. A created Timer will fire repetitively until it is destroyed.
25 //! Data type of Timer callback
26 typedef void (*TimeoutHandler
)(void *data
);
29 //! Compares two timeval structs
31 //! Compares two timeval structs
32 inline bool operator()(const Timer
*a
, const Timer
*b
) const {
33 return ((&a
->_timeout
)->tv_sec
== (&b
->_timeout
)->tv_sec
) ?
34 ((&a
->_timeout
)->tv_usec
> (&b
->_timeout
)->tv_usec
) :
35 ((&a
->_timeout
)->tv_sec
> (&b
->_timeout
)->tv_sec
);
38 friend struct TimerCompare
; // give access to _timeout for shitty compilers
41 std::priority_queue
<Timer
*, std::vector
<Timer
*>, TimerCompare
> TimerQ
;
43 //! Milliseconds between timer firings
45 //! Callback for timer expiry
46 TimeoutHandler _action
;
47 //! Data sent to callback
49 //! We overload the delete operator to just set this to true
51 //! The time the last fire should've been at
53 //! When this timer will next trigger
54 struct timeval _timeout
;
56 //! Queue of pending timers
58 //! Time next timer will expire
59 static timeval _nearest_timeout
;
60 //! Time at start of current processing loop
63 //! Really delete something (not just flag for later)
65 @param self Timer to be deleted.
67 static void realDelete(Timer
*self
);
69 //! Adds a millisecond delay to a timeval structure
71 @param a Amount of time to increment.
72 @param msec Number of milliseconds to increment by.
74 static void timevalAdd(timeval
&a
, long msec
);
77 //! Constructs a new running timer and queues it
79 @param delay Time in milliseconds between firings
80 @param cb The function to be called on fire.
81 @param data Data to be passed to the callback on fire.
83 Timer(long delay
, TimeoutHandler cb
, void *data
);
85 //! Overloaded delete so we can leave deleted objects in queue for later reap
87 @param self Pointer to current instance of Timer.
89 void operator delete(void *self
);
91 //! Dispatches all elligible timers, then optionally waits for X events
93 @param wait Whether to wait for X events after processing timers.
95 static void dispatchTimers(bool wait
= true);
97 //! Returns a relative timeval (to pass select) of the next timer
99 @param tm Changed to hold the time until next timer.
100 @return true if there are any timers queued, and the timeout is being
101 returned in 'tm'. false if there are no timers queued.
103 static bool nearestTimeout(struct timeval
&tm
);
105 //! Initializes internal data before use
106 static void initialize(void);
108 //! Deletes all waiting timers
109 static void destroy(void);
This page took 0.041232 seconds and 4 git commands to generate.