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