]>
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.
10 #ifdef TIME_WITH_SYS_TIME
11 # include <sys/time.h>
13 #else // !TIME_WITH_SYS_TIME
14 # ifdef HAVE_SYS_TIME_H
15 # include <sys/time.h>
16 # else // !HAVE_SYS_TIME_H
18 # endif // HAVE_SYS_TIME_H
19 #endif // TIME_WITH_SYS_TIME
27 //! The Timer class implements timed callbacks.
29 The Timer class can be used to have a callback fire after a given time
30 interval. A created Timer will fire repetitively until it is destroyed.
34 //! Data type of Timer callback
35 typedef void (*TimeoutHandler
)(void *data
);
38 //! Compares two timeval structs
40 //! Compares two timeval structs
41 inline bool operator()(const Timer
*a
, const Timer
*b
) const {
42 return timercmp(&a
->_timeout
, &b
->_timeout
, >);
47 std::priority_queue
<Timer
*, std::vector
<Timer
*>, TimerCompare
> TimerQ
;
49 //! Milliseconds between timer firings
51 //! Callback for timer expiry
52 TimeoutHandler _action
;
53 //! Data sent to callback
55 //! We overload the delete operator to just set this to true
57 //! The time the last fire should've been at
59 //! When this timer will next trigger
60 struct timeval _timeout
;
62 //! Queue of pending timers
64 //! Time next timer will expire
65 static timeval _nearest_timeout
;
66 //! Time at start of current processing loop
69 //! Really delete something (not just flag for later)
71 @param self Timer to be deleted.
73 static void realDelete(Timer
*self
);
75 //! Adds a millisecond delay to a timeval structure
77 @param a Amount of time to increment.
78 @param msec Number of milliseconds to increment by.
80 static void timevalAdd(timeval
&a
, long msec
);
83 //! Constructs a new running timer and queues it
85 @param delay Time in milliseconds between firings
86 @param cb The function to be called on fire.
87 @param data Data to be passed to the callback on fire.
89 Timer(long delay
, TimeoutHandler cb
, void *data
);
91 //! Overloaded delete so we can leave deleted objects in queue for later reap
93 @param self Pointer to current instance of Timer.
95 void operator delete(void *self
);
97 //! Dispatches all elligible timers, then optionally waits for X events
99 @param wait Whether to wait for X events after processing timers.
101 static void dispatchTimers(bool wait
= true);
103 //! Returns a relative timeval (to pass select) of the next timer
105 @param tm Changed to hold the time until next timer.
106 @return true if there are any timers queued, and the timeout is being
107 returned in 'tm'. false if there are no timers queued.
109 static bool nearestTimeout(struct timeval
&tm
);
111 //! Initializes internal data before use
112 static void initialize(void);
114 //! Deletes all waiting timers
115 static void destroy(void);
This page took 0.038654 seconds and 4 git commands to generate.