#ifndef UCORE_CLOCK_H_ #define UCORE_CLOCK_H_ #include #include /** * 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 ucore_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 ucore_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 ucore_timeofday_clock; /** * A clock where now() calls time(). * This clock will just have seconds resolution. */ extern struct UCoreClock 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 UCoreCachedClock *uclock, struct UCoreClock *real_clock); /** Update the cached clock from the real_clock */ void ucore_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 ucore_settable_clock_init(struct UCoreSettableClock *uclock); /** * Set the new timeval this clock will return **/ void ucore_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 ucore_settable_clock_set(struct UCoreSettableClock *uclock, time_t secs, suseconds_t usecs); #endif