]>
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
22 # include <sys/types.h>
24 #endif // HAVE_UNISTD_H
32 //! The Timer class implements timed callbacks.
34 The Timer class can be used to have a callback fire after a given time
35 interval. A created Timer will fire repetitively until it is destroyed.
39 //! Data type of Timer callback
40 typedef void (*TimeoutHandler
)(void *data
);
43 //! Compares two timeval structs
45 //! Compares two timeval structs
46 inline bool operator()(const Timer
*a
, const Timer
*b
) const {
47 return timercmp(&a
->_timeout
, &b
->_timeout
, >);
52 std::priority_queue
<Timer
*, std::vector
<Timer
*>, TimerCompare
> TimerQ
;
54 //! Milliseconds between timer firings
56 //! Callback for timer expiry
57 TimeoutHandler _action
;
58 //! Data sent to callback
60 //! We overload the delete operator to just set this to true
62 //! The time the last fire should've been at
64 //! When this timer will next trigger
65 struct timeval _timeout
;
67 //! Queue of pending timers
69 //! Time next timer will expire
70 static timeval _nearest_timeout
;
71 //! Time at start of current processing loop
74 //! Really delete something (not just flag for later)
76 @param self Timer to be deleted.
78 static void realDelete(Timer
*self
);
80 //! Adds a millisecond delay to a timeval structure
82 @param a Amount of time to increment.
83 @param msec Number of milliseconds to increment by.
85 static void timevalAdd(timeval
&a
, long msec
);
88 //! Constructs a new running timer and queues it
90 @param delay Time in milliseconds between firings
91 @param cb The function to be called on fire.
92 @param data Data to be passed to the callback on fire.
94 Timer(long delay
, TimeoutHandler cb
, void *data
);
96 //! Overloaded delete so we can leave deleted objects in queue for later reap
98 @param self Pointer to current instance of Timer.
100 void operator delete(void *self
);
102 //! Dispatches all elligible timers, then optionally waits for X events
104 @param wait Whether to wait for X events after processing timers.
106 static void dispatchTimers(bool wait
= true);
108 //! Returns a relative timeval (to pass select) of the next timer
110 @param tm Changed to hold the time until next timer.
111 @return true if there are any timers queued, and the timeout is being
112 returned in 'tm'. false if there are no timers queued.
114 static bool nearestTimeout(struct timeval
&tm
);
116 //! Initializes internal data before use
117 static void initialize(void);
119 //! Deletes all waiting timers
120 static void destroy(void);
This page took 0.039706 seconds and 5 git commands to generate.