xref: /iperf/src/timer.h (revision aee0b741)
1 /*
2  * iperf, Copyright (c) 2014, The Regents of the University of
3  * California, through Lawrence Berkeley National Laboratory (subject
4  * to receipt of any required approvals from the U.S. Dept. of
5  * Energy).  All rights reserved.
6  *
7  * If you have questions about your rights to use or distribute this
8  * software, please contact Berkeley Lab's Technology Transfer
9  * Department at [email protected].
10  *
11  * NOTICE.  This software is owned by the U.S. Department of Energy.
12  * As such, the U.S. Government has been granted for itself and others
13  * acting on its behalf a paid-up, nonexclusive, irrevocable,
14  * worldwide license in the Software to reproduce, prepare derivative
15  * works, and perform publicly and display publicly.  Beginning five
16  * (5) years after the date permission to assert copyright is obtained
17  * from the U.S. Department of Energy, and subject to any subsequent
18  * five (5) year renewals, the U.S. Government is granted for itself
19  * and others acting on its behalf a paid-up, nonexclusive,
20  * irrevocable, worldwide license in the Software to reproduce,
21  * prepare derivative works, distribute copies to the public, perform
22  * publicly and display publicly, and to permit others to do so.
23  *
24  * This code is distributed under a BSD style license, see the LICENSE
25  * file for complete information.
26  *
27  * Based on timers.h by Jef Poskanzer. Used with permission.
28  */
29 
30 #ifndef __TIMER_H
31 #define __TIMER_H
32 
33 #include <sys/time.h>
34 
35 /* TimerClientData is an opaque value that tags along with a timer.  The
36 ** client can use it for whatever, and it gets passed to the callback when
37 ** the timer triggers.
38 */
39 typedef union
40 {
41     void* p;
42     int i;
43     long l;
44 } TimerClientData;
45 
46 extern TimerClientData JunkClientData;	/* for use when you don't care */
47 
48 /* The TimerProc gets called when the timer expires.  It gets passed
49 ** the TimerClientData associated with the timer, and a timeval in case
50 ** it wants to schedule another timer.
51 */
52 typedef void TimerProc( TimerClientData client_data, struct timeval* nowP );
53 
54 /* The Timer struct. */
55 typedef struct TimerStruct
56 {
57     TimerProc* timer_proc;
58     TimerClientData client_data;
59     int64_t usecs;
60     int periodic;
61     struct timeval time;
62     struct TimerStruct* prev;
63     struct TimerStruct* next;
64     int hash;
65 } Timer;
66 
67 /* Set up a timer, either periodic or one-shot. Returns (Timer*) 0 on errors. */
68 extern Timer* tmr_create(
69     struct timeval* nowP, TimerProc* timer_proc, TimerClientData client_data,
70     int64_t usecs, int periodic );
71 
72 /* Returns a timeout indicating how long until the next timer triggers.  You
73 ** can just put the call to this routine right in your select().  Returns
74 ** (struct timeval*) 0 if no timers are pending.
75 */
76 extern struct timeval* tmr_timeout( struct timeval* nowP ) /* __attribute__((hot)) */;
77 
78 /* Run the list of timers. Your main program needs to call this every so often,
79 ** or as indicated by tmr_timeout().
80 */
81 extern void tmr_run( struct timeval* nowP ) /* __attribute__((hot)) */;
82 
83 /* Reset the clock on a timer, to current time plus the original timeout. */
84 extern void tmr_reset( struct timeval* nowP, Timer* timer );
85 
86 /* Deschedule a timer.  Note that non-periodic timers are automatically
87 ** descheduled when they run, so you don't have to call this on them.
88 */
89 extern void tmr_cancel( Timer* timer );
90 
91 /* Clean up the timers package, freeing any unused storage. */
92 extern void tmr_cleanup( void );
93 
94 /* Cancel all timers and free storage, usually in preparation for exiting. */
95 extern void tmr_destroy( void );
96 
97 #endif /* __TIMER_H */
98