83 lines
2.0 KiB
C
83 lines
2.0 KiB
C
#include "ucore/clock.h"
|
|
#include "ucore/utils.h"
|
|
|
|
#ifdef __GNUC__
|
|
#define UNUSED __attribute__((__unused__))
|
|
#else
|
|
#define UNUSED
|
|
#endif
|
|
|
|
static void uc_timeofday_now(const struct UCoreClock *uclock UNUSED, struct timeval *tv)
|
|
{
|
|
gettimeofday(tv, NULL);
|
|
}
|
|
|
|
static void uc_time_now(const struct UCoreClock *uclock UNUSED, struct timeval *tv)
|
|
{
|
|
tv->tv_sec = time(NULL);
|
|
tv->tv_usec = 0;
|
|
}
|
|
|
|
struct UCoreClock uc_timeofday_clock = {
|
|
.name = "gettimeofday",
|
|
.now = uc_timeofday_now,
|
|
};
|
|
|
|
struct UCoreClock uc_time_clock = {
|
|
.name = "time",
|
|
.now = uc_time_now,
|
|
};
|
|
|
|
static void uc_cached_clock_now(const struct UCoreClock *uclock,
|
|
struct timeval *tv)
|
|
{
|
|
const struct UCoreCachedClock *cached_clock;
|
|
cached_clock = UC_CONST_CONTAINER_OF(uclock, const struct UCoreCachedClock, clock),
|
|
|
|
*tv = cached_clock->cache;
|
|
}
|
|
|
|
void uc_cached_clock_init(struct UCoreCachedClock *uclock, struct UCoreClock *real_clock)
|
|
{
|
|
uclock->clock.name = "cached clock";
|
|
uclock->clock.now = uc_cached_clock_now;
|
|
|
|
uclock->real_clock = real_clock;
|
|
|
|
uc_cached_clock_update(uclock);
|
|
}
|
|
|
|
void uc_cached_clock_update(struct UCoreCachedClock *uclock)
|
|
{
|
|
uclock->real_clock->now(&uclock->clock, &uclock->cache);
|
|
}
|
|
|
|
static void uc_settable_clock_now(const struct UCoreClock *uclock, struct timeval *tv)
|
|
{
|
|
const struct UCoreSettableClock *cached_clock;
|
|
cached_clock = UC_CONST_CONTAINER_OF(uclock, const struct UCoreSettableClock, clock),
|
|
|
|
*tv = cached_clock->now;
|
|
}
|
|
|
|
void uc_settable_clock_init(struct UCoreSettableClock *uclock)
|
|
{
|
|
uclock->clock.name = "settable clock";
|
|
uclock->clock.now = uc_settable_clock_now;
|
|
|
|
uclock->now.tv_sec = 0;
|
|
uclock->now.tv_usec = 0;
|
|
}
|
|
|
|
void uc_settable_clock_set_tv(struct UCoreSettableClock *uclock, const struct timeval *tv)
|
|
{
|
|
uclock->now = *tv;
|
|
}
|
|
|
|
void uc_settable_clock_set(struct UCoreSettableClock *uclock, time_t secs, suseconds_t usecs)
|
|
{
|
|
uclock->now.tv_sec = secs;
|
|
uclock->now.tv_usec = usecs;
|
|
}
|
|
|