100 lines
2.5 KiB
C
100 lines
2.5 KiB
C
#ifndef UCORE_CLOCK_H_
|
|
#define UCORE_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 ucore_clock {
|
|
/**
|
|
* Name of this clock
|
|
*/
|
|
const char *name;
|
|
/**
|
|
* Wrapper for getting the current time.
|
|
* @param tv timeval that will be filled in
|
|
*/
|
|
void (*now)(struct ucore_clock *clock, struct timeval *tv);
|
|
};
|
|
|
|
|
|
/** A cached clock where clock.now() returns
|
|
* the cached value. The cache is updated
|
|
* from the real_clock whenever ucore_cached_clock_update()
|
|
* is called.
|
|
*/
|
|
struct ucore_cached_clock {
|
|
/** The clock*/
|
|
struct ucore_clock clock;
|
|
/** The underlying real clock*/
|
|
struct ucore_clock *real_clock;
|
|
/** cached value */
|
|
struct timeval cache;
|
|
};
|
|
|
|
/** A settable clock where clock.now() returns
|
|
* the user settable value. The user updates the clock
|
|
* with ucore_settable_clock_set().
|
|
*/
|
|
struct ucore_settable_clock {
|
|
/** The clock*/
|
|
struct ucore_clock clock;
|
|
/** The underlying real clock*/
|
|
/** Current value */
|
|
struct timeval now;
|
|
};
|
|
|
|
|
|
/** A clock where now() calls gettimeofday()
|
|
*/
|
|
extern struct ucore_clock ucore_timeofday_clock;
|
|
|
|
/**
|
|
* A clock where now() calls time().
|
|
* This clock will this just have seconds resolution.
|
|
*/
|
|
extern struct ucore_clock ucore_time_clock;
|
|
|
|
/**
|
|
* Initialize a cached clock where now() calls time().
|
|
* This clock will this just have seconds resolution.
|
|
* @param clock clock to initialize
|
|
*/
|
|
void ucore_cached_clock_init(struct ucore_cached_clock *clock,
|
|
struct ucore_clock *real_clock);
|
|
|
|
/** Update the cached clock from the real_clock
|
|
*/
|
|
void ucore_cached_clock_update(struct ucore_cached_clock *clock);
|
|
|
|
|
|
/**
|
|
* Initialize a user settable where now() calls time().
|
|
* This clock will this just have seconds resolution.
|
|
* @param clock clock to initialize
|
|
*/
|
|
void ucore_settable_clock_init(struct ucore_settable_clock *clock);
|
|
|
|
/**
|
|
* Set the new timeval this clock will return
|
|
**/
|
|
void ucore_cached_clock_set_tv(struct ucore_settable_clock *clock,
|
|
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 ucore_cached_clock_set(struct ucore_settable_clock *clock,
|
|
time_t secs, suseconds_t usecs);
|
|
|
|
#endif
|
|
|