115 lines
2.9 KiB
C
115 lines
2.9 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);
|
|
};
|
|
|
|
/** Convenience macro for geting the time from a UCoreClock
|
|
* @param uclock pointer to the clock
|
|
* @param tv struct timeval pointer that will be filled in.
|
|
*/
|
|
#define UCORE_CLOCK_NOW(uclock, tv) (uclock)->now((uclock), (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;
|
|
/** 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 whose now() is a cached
|
|
* value of the real_clock.
|
|
*
|
|
* @param uclock clock to initialize
|
|
* @param real_clock underlying clock that updates
|
|
* the cache.
|
|
*
|
|
*/
|
|
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 clock, where now()
|
|
* returns the user set time.
|
|
*
|
|
* @param clock uclock to initialize
|
|
*/
|
|
void uc_settable_clock_init(struct UCoreSettableClock *uclock);
|
|
|
|
/**
|
|
* Set the new timeval this clock will return
|
|
*
|
|
* @param tv new timeval to set the clock to
|
|
**/
|
|
void uc_settable_clock_set_tv(struct UCoreSettableClock *uclock,
|
|
const struct timeval *tv);
|
|
|
|
/**
|
|
* Set the new time this clock will return
|
|
* Convenience function that takes seconds/microseconds as arguments
|
|
* instead of a struct timeval.
|
|
*
|
|
* @param secs seconds portion of the clock to set
|
|
* @param usecs microseconds portion of the clock to set.
|
|
**/
|
|
void uc_settable_clock_set(struct UCoreSettableClock *uclock,
|
|
time_t secs, suseconds_t usecs);
|
|
|
|
#endif
|
|
|