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