xref: /iperf/src/timer.h (revision ba7b91d2)
1 /*
2  * Copyright (c) 2009-2011, The Regents of the University of California,
3  * through Lawrence Berkeley National Laboratory (subject to receipt of any
4  * required approvals from the U.S. Dept. of Energy).  All rights reserved.
5  *
6  * This code is distributed under a BSD style license, see the LICENSE file
7  * for complete information.
8  *
9  * Based on timers.h by Jef Poskanzer. Used with permission.
10  */
11 
12 #ifndef __TIMER_H
13 #define __TIMER_H
14 
15 #include <sys/time.h>
16 
17 /* TimerClientData is an opaque value that tags along with a timer.  The
18 ** client can use it for whatever, and it gets passed to the callback when
19 ** the timer triggers.
20 */
21 typedef union
22 {
23     void* p;
24     int i;
25     long l;
26 } TimerClientData;
27 
28 extern TimerClientData JunkClientData;	/* for use when you don't care */
29 
30 /* The TimerProc gets called when the timer expires.  It gets passed
31 ** the TimerClientData associated with the timer, and a timeval in case
32 ** it wants to schedule another timer.
33 */
34 typedef void TimerProc( TimerClientData client_data, struct timeval* nowP );
35 
36 /* The Timer struct. */
37 typedef struct TimerStruct
38 {
39     TimerProc* timer_proc;
40     TimerClientData client_data;
41     int64_t usecs;
42     int periodic;
43     struct timeval time;
44     struct TimerStruct* prev;
45     struct TimerStruct* next;
46     int hash;
47 } Timer;
48 
49 /* Set up a timer, either periodic or one-shot. Returns (Timer*) 0 on errors. */
50 extern Timer* tmr_create(
51     struct timeval* nowP, TimerProc* timer_proc, TimerClientData client_data,
52     int64_t usecs, int periodic );
53 
54 /* Returns a timeout indicating how long until the next timer triggers.  You
55 ** can just put the call to this routine right in your select().  Returns
56 ** (struct timeval*) 0 if no timers are pending.
57 */
58 extern struct timeval* tmr_timeout( struct timeval* nowP ) /* __attribute__((hot)) */;
59 
60 /* Run the list of timers. Your main program needs to call this every so often,
61 ** or as indicated by tmr_timeout().
62 */
63 extern void tmr_run( struct timeval* nowP ) /* __attribute__((hot)) */;
64 
65 /* Reset the clock on a timer, to current time plus the original timeout. */
66 extern void tmr_reset( struct timeval* nowP, Timer* timer );
67 
68 /* Deschedule a timer.  Note that non-periodic timers are automatically
69 ** descheduled when they run, so you don't have to call this on them.
70 */
71 extern void tmr_cancel( Timer* timer );
72 
73 /* Clean up the timers package, freeing any unused storage. */
74 extern void tmr_cleanup( void );
75 
76 /* Cancel all timers and free storage, usually in preparation for exitting. */
77 extern void tmr_destroy( void );
78 
79 #endif /* __TIMER_H */
80