100 lines
2.5 KiB
C
100 lines
2.5 KiB
C
#ifndef uc_CLOCK_H_
|
|
#define uc_CLOCK_H_
|
|
|
|
#include <sys/time.h>
|
|
#include <time.h>
|
|
|
|
|
|
/**
|
|
* An abstract clock.
|
|
* Instead of hardcoding calls to time()/gettimeofday() and similar,
|
|
* it is useful tp have a level of indirection to better facilitate
|
|
* testing and simulation of code that's dependent on time.
|
|
*/
|
|
struct UCoreClock {
|
|
/**
|
|
* Name of this clock
|
|
*/
|
|
const char *name;
|
|
/**
|
|
* Wrapper for getting the current time.
|
|
* @param tv timeval that will be filled in
|
|
*/
|
|
void (*now)(struct UCoreClock *clock, struct timeval *tv);
|
|
};
|
|
|
|
|
|
/** A cached clock where clock.now() returns
|
|
* the cached value. The cache is updated
|
|
* from the real_clock whenever uc_cached_clock_update()
|
|
* is called.
|
|
*/
|
|
struct UCoreCachedClock {
|
|
/** The clock*/
|
|
struct UCoreClock clock;
|
|
/** The underlying real clock*/
|
|
struct UCoreClock *real_clock;
|
|
/** cached value */
|
|
struct timeval cache;
|
|
};
|
|
|
|
/** A settable clock where clock.now() returns
|
|
* the user settable value. The user updates the clock
|
|
* with uc_settable_clock_set().
|
|
*/
|
|
struct UCoreSettableClock {
|
|
/** The clock*/
|
|
struct UCoreClock clock;
|
|
/** The underlying real clock*/
|
|
/** Current value */
|
|
struct timeval now;
|
|
};
|
|
|
|
|
|
/** A clock where now() calls gettimeofday()
|
|
*/
|
|
extern struct UCoreClock uc_timeofday_clock;
|
|
|
|
/**
|
|
* A clock where now() calls time().
|
|
* This clock will just have seconds resolution.
|
|
*/
|
|
extern struct UCoreClock uc_time_clock;
|
|
|
|
/**
|
|
* Initialize a cached clock where now() calls time().
|
|
* This clock will this just have seconds resolution.
|
|
* @param clock clock to initialize
|
|
*/
|
|
void uc_cached_clock_init(struct UCoreCachedClock *uclock,
|
|
struct UCoreClock *real_clock);
|
|
|
|
/** Update the cached clock from the real_clock
|
|
*/
|
|
void uc_cached_clock_update(struct UCoreCachedClock *uclock);
|
|
|
|
|
|
/**
|
|
* Initialize a user settable where now() calls time().
|
|
* This clock will this just have seconds resolution.
|
|
* @param clock clock to initialize
|
|
*/
|
|
void uc_settable_clock_init(struct UCoreSettableClock *uclock);
|
|
|
|
/**
|
|
* Set the new timeval this clock will return
|
|
**/
|
|
void uc_settable_clock_set_tv(struct UCoreSettableClock *uclock,
|
|
const struct timeval *tv);
|
|
|
|
/**
|
|
* Set the new timeval this clock will return
|
|
* Convenience function that takes seconds/microseconds as arguments
|
|
* instead of a struct timeval.
|
|
**/
|
|
void uc_settable_clock_set(struct UCoreSettableClock *uclock,
|
|
time_t secs, suseconds_t usecs);
|
|
|
|
#endif
|
|
|