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 TTD@lbl.gov.
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 <time.h>
34 #include "iperf_time.h"
35 
36 /* TimerClientData is an opaque value that tags along with a timer.  The
37 ** client can use it for whatever, and it gets passed to the callback when
38 ** the timer triggers.
39 */
40 typedef union
41 {
42     void* p;
43     int i;
44     long l;
45 } TimerClientData;
46 
47 extern TimerClientData JunkClientData;	/* for use when you don't care */
48 
49 /* The TimerProc gets called when the timer expires.  It gets passed
50 ** the TimerClientData associated with the timer, and a iperf_time in case
51 ** it wants to schedule another timer.
52 */
53 typedef void TimerProc( TimerClientData client_data, struct iperf_time* nowP );
54 
55 /* The Timer struct. */
56 typedef struct TimerStruct
57 {
58     TimerProc* timer_proc;
59     TimerClientData client_data;
60     int64_t usecs;
61     int periodic;
62     struct iperf_time time;
63     struct TimerStruct* prev;
64     struct TimerStruct* next;
65     int hash;
66 } Timer;
67 
68 /* Set up a timer, either periodic or one-shot. Returns (Timer*) 0 on errors. */
69 extern Timer* tmr_create(
70     struct iperf_time* nowP, TimerProc* timer_proc, TimerClientData client_data,
71     int64_t usecs, int periodic );
72 
73 /* Returns a timeout indicating how long until the next timer triggers.  You
74 ** can just put the call to this routine right in your select().  Returns
75 ** (struct timeval*) 0 if no timers are pending.
76 */
77 extern struct timeval* tmr_timeout( struct iperf_time* nowP ) /* __attribute__((hot)) */;
78 
79 /* Run the list of timers. Your main program needs to call this every so often,
80 ** or as indicated by tmr_timeout().
81 */
82 extern void tmr_run( struct iperf_time* nowP ) /* __attribute__((hot)) */;
83 
84 /* Reset the clock on a timer, to current time plus the original timeout. */
85 extern void tmr_reset( struct iperf_time* nowP, Timer* timer );
86 
87 /* Deschedule a timer.  Note that non-periodic timers are automatically
88 ** descheduled when they run, so you don't have to call this on them.
89 */
90 extern void tmr_cancel( Timer* timer );
91 
92 /* Clean up the timers package, freeing any unused storage. */
93 extern void tmr_cleanup( void );
94 
95 /* Cancel all timers and free storage, usually in preparation for exiting. */
96 extern void tmr_destroy( void );
97 
98 #endif /* __TIMER_H */
99