/* If we're a child, this is the first call we did ourselves. */
static struct failtest_call *our_history_start = NULL;
/* For printing runtime with --trace. */
-static struct timespec start;
+static struct timeabs start;
/* Set when failtest_hook returns FAIL_PROBE */
static bool probing = false;
/* Table to track duplicates. */
if (child == 0) {
traceindent++;
if (tracef) {
- struct timespec diff;
+ struct timerel diff;
const char *p;
char *failpath;
struct failtest_call *c;
c = tlist_tail(&history, list);
- diff = time_sub(time_now(), start);
+ diff = time_between(time_now(), start);
failpath = failpath_string();
p = strrchr(c->file, '/');
if (p)
p = c->file;
trace("%u->%u (%u.%02u): %s (%s:%u)\n",
getppid(), getpid(),
- (int)diff.tv_sec, (int)diff.tv_nsec / 10000000,
+ (int)diff.ts.tv_sec, (int)diff.ts.tv_nsec / 10000000,
failpath, p, c->line);
free(failpath);
}
void del_listener(struct io_listener *l);
void backend_plan_changed(struct io_conn *conn);
void backend_wait_changed(const void *wait);
-void backend_add_timeout(struct io_conn *conn, struct timespec ts);
+void backend_add_timeout(struct io_conn *conn, struct timerel duration);
void backend_del_timeout(struct io_conn *conn);
void backend_del_conn(struct io_conn *conn);
return conn;
}
-bool io_timeout_(struct io_conn *conn, struct timespec ts,
+bool io_timeout_(struct io_conn *conn, struct timerel t,
struct io_plan (*cb)(struct io_conn *, void *), void *arg)
{
assert(cb);
conn->timeout->next = cb;
conn->timeout->next_arg = arg;
- backend_add_timeout(conn, ts);
+ backend_add_timeout(conn, t);
return true;
}
/**
* io_timeout - set timeout function if the callback doesn't complete.
* @conn: the current connection.
- * @ts: how long until the timeout should be called.
+ * @t: how long until the timeout should be called.
* @cb: callback to call.
* @arg: argument to @cb.
*
(fn), (arg), \
struct io_conn *), \
(arg))
-bool io_timeout_(struct io_conn *conn, struct timespec ts,
+bool io_timeout_(struct io_conn *conn, struct timerel t,
struct io_plan (*fn)(struct io_conn *, void *), void *arg);
/**
return false;
}
-void backend_add_timeout(struct io_conn *conn, struct timespec duration)
+void backend_add_timeout(struct io_conn *conn, struct timerel duration)
{
if (!timeouts.base)
timers_init(&timeouts, time_now());
timer_add(&timeouts, &conn->timeout->timer,
- time_add(time_now(), duration));
+ timeabs_add(time_now(), duration));
conn->timeout->conn = conn;
}
while (!io_loop_return) {
int i, r, timeout = INT_MAX;
- struct timespec now;
+ struct timeabs now;
bool some_timeouts = false;
if (timeouts.base) {
- struct timespec first;
+ struct timeabs first;
struct list_head expired;
struct io_timeout *t;
/* Now figure out how long to wait for the next one. */
if (timer_earliest(&timeouts, &first)) {
- uint64_t f = time_to_msec(time_sub(first, now));
+ uint64_t f = time_to_msec(time_between(first, now));
if (f < INT_MAX)
timeout = f;
}
* s = malloc(sizeof(*s));
* s->string = argv[1];
* timer_add(&timers, &s->timer,
- * time_add(time_now(),
- * time_from_msec(atol(argv[2]))));
+ * timeabs_add(time_now(),
+ * time_from_msec(atol(argv[2]))));
* list_add_tail(&strings, &s->node);
* argv += 2;
* }
*
* while (!list_empty(&strings)) {
- * struct timespec now = time_now();
+ * struct timeabs now = time_now();
* list_for_each(&strings, s, node)
* printf("%s", s->string);
* timers_expire(&timers, now, &expired);
struct timer t;
uint64_t diff;
unsigned int i;
+ struct timeabs epoch = { { 0, 0 } };
/* This is how many tests you plan to run */
plan_tests(2 + (18 + (MAX_ORD - 4) * 3) * (18 + (MAX_ORD - 4) * 3));
- timers_init(&timers, time_from_nsec(0));
+ timers_init(&timers, epoch);
ok1(timers_check(&timers, NULL));
for (i = 0; i < 4; i++)
#include <ccan/timer/timer.c>
#include <ccan/tap/tap.h>
+static struct timeabs timeabs_from_usec(unsigned long long usec)
+{
+ struct timeabs epoch = { { 0, 0 } };
+ return timeabs_add(epoch, time_from_usec(usec));
+}
+
int main(void)
{
struct timers timers;
/* This is how many tests you plan to run */
plan_tests(3);
- timers_init(&timers, time_from_usec(1364726722653919ULL));
- timer_add(&timers, &t, time_from_usec(1364726722703919ULL));
- timers_expire(&timers, time_from_usec(1364726722653920ULL), &expired);
+ timers_init(&timers, timeabs_from_usec(1364726722653919ULL));
+ timer_add(&timers, &t, timeabs_from_usec(1364726722703919ULL));
+ timers_expire(&timers, timeabs_from_usec(1364726722653920ULL), &expired);
ok1(list_empty(&expired));
- timers_expire(&timers, time_from_usec(1364726725454187ULL), &expired);
+ timers_expire(&timers, timeabs_from_usec(1364726725454187ULL), &expired);
ok1(!list_empty(&expired));
ok1(list_top(&expired, struct timer, list) == &t);
#include <ccan/timer/timer.c>
#include <ccan/tap/tap.h>
+static struct timeabs timeabs_from_nsec(unsigned long long nsec)
+{
+ struct timeabs epoch = { { 0, 0 } };
+ return timeabs_add(epoch, time_from_nsec(nsec));
+}
+
int main(void)
{
struct timers timers;
struct timer t[64];
struct list_head expired;
- struct timespec earliest;
+ struct timeabs earliest;
uint64_t i;
+ struct timeabs epoch = { { 0, 0 } };
/* This is how many tests you plan to run */
plan_tests(488);
- timers_init(&timers, time_from_nsec(0));
+ timers_init(&timers, epoch);
ok1(timers_check(&timers, NULL));
ok1(!timer_earliest(&timers, &earliest));
- timer_add(&timers, &t[0], time_from_nsec(1));
+ timer_add(&timers, &t[0], timeabs_from_nsec(1));
ok1(timers_check(&timers, NULL));
ok1(timer_earliest(&timers, &earliest));
- ok1(time_eq(earliest, grains_to_time(t[0].time)));
+ ok1(timeabs_eq(earliest, grains_to_time(t[0].time)));
timer_del(&timers, &t[0]);
ok1(timers_check(&timers, NULL));
ok1(!timer_earliest(&timers, &earliest));
/* Check timer ordering. */
for (i = 0; i < 32; i++) {
- timer_add(&timers, &t[i*2], time_from_nsec(1ULL << i));
+ timer_add(&timers, &t[i*2], timeabs_from_nsec(1ULL << i));
ok1(timers_check(&timers, NULL));
- timer_add(&timers, &t[i*2+1], time_from_nsec((1ULL << i) + 1));
+ timer_add(&timers, &t[i*2+1], timeabs_from_nsec((1ULL << i) + 1));
ok1(timers_check(&timers, NULL));
}
for (i = 0; i < 32; i++) {
uint64_t exp = (uint64_t)TIMER_GRANULARITY << i;
- timer_add(&timers, &t[i*2], time_from_nsec(exp));
+ timer_add(&timers, &t[i*2], timeabs_from_nsec(exp));
ok1(timers_check(&timers, NULL));
- timer_add(&timers, &t[i*2+1], time_from_nsec(exp + 1));
+ timer_add(&timers, &t[i*2+1], timeabs_from_nsec(exp + 1));
ok1(timers_check(&timers, NULL));
}
struct list_head list[PER_LEVEL];
};
-static uint64_t time_to_grains(struct timespec ts)
+static uint64_t time_to_grains(struct timeabs t)
{
- return ts.tv_sec * ((uint64_t)1000000000 / TIMER_GRANULARITY)
- + (ts.tv_nsec / TIMER_GRANULARITY);
+ return t.ts.tv_sec * ((uint64_t)1000000000 / TIMER_GRANULARITY)
+ + (t.ts.tv_nsec / TIMER_GRANULARITY);
}
-static struct timespec grains_to_time(uint64_t grains)
+static struct timeabs grains_to_time(uint64_t grains)
{
- struct timespec ts;
+ struct timeabs t;
- ts.tv_sec = grains / (1000000000 / TIMER_GRANULARITY);
- ts.tv_nsec = (grains % (1000000000 / TIMER_GRANULARITY))
+ t.ts.tv_sec = grains / (1000000000 / TIMER_GRANULARITY);
+ t.ts.tv_nsec = (grains % (1000000000 / TIMER_GRANULARITY))
* TIMER_GRANULARITY;
- return ts;
+ return t;
}
-void timers_init(struct timers *timers, struct timespec start)
+void timers_init(struct timers *timers, struct timeabs start)
{
unsigned int i;
list_add_tail(l, &t->list);
}
-void timer_add(struct timers *timers, struct timer *t, struct timespec when)
+void timer_add(struct timers *timers, struct timer *t, struct timeabs when)
{
t->time = time_to_grains(when);
return true;
}
-bool timer_earliest(struct timers *timers, struct timespec *first)
+bool timer_earliest(struct timers *timers, struct timeabs *first)
{
if (!update_first(timers))
return false;
/* Fills list of expired timers. */
void timers_expire(struct timers *timers,
- struct timespec expire,
+ struct timeabs expire,
struct list_head *list)
{
uint64_t now = time_to_grains(expire);
*
* timers_init(&timeouts, time_now());
*/
-void timers_init(struct timers *timers, struct timespec start);
+void timers_init(struct timers *timers, struct timeabs start);
/**
* timers_cleanup - free allocations within timers struct.
* struct timer t;
*
* // Timeout in 100ms.
- * timer_add(&timeouts, &t, time_add(time_now(), time_from_msec(100)));
+ * timer_add(&timeouts, &t, timeabs_add(time_now(), time_from_msec(100)));
*/
-void timer_add(struct timers *timers, struct timer *timer,
- struct timespec when);
+void timer_add(struct timers *timers, struct timer *timer, struct timeabs when);
/**
* timer_del - remove an unexpired timer.
/**
* timer_earliest - find out the first time when a timer will expire
* @timers: the struct timers
- * @first: the time, only set if there is a timer.
+ * @first: the expiry time, only set if there is a timer.
*
* This returns false, and doesn't alter @first if there are no
* timers. Otherwise, it sets @first to the expiry time of the first
* timer (rounded to TIMER_GRANULARITY nanoseconds), and returns true.
*
* Example:
- * struct timespec next = { (time_t)-1ULL, -1UL };
+ * struct timeabs next = { { (time_t)-1ULL, -1UL } };
* timer_earliest(&timeouts, &next);
*/
-bool timer_earliest(struct timers *timers, struct timespec *first);
+bool timer_earliest(struct timers *timers, struct timeabs *first);
/**
* timers_expire - update timers structure and remove expired timers.
* printf("Timer expired!\n");
*/
void timers_expire(struct timers *timers,
- struct timespec expire,
+ struct timeabs expire,
struct list_head *list);
/**
signal(SIGALRM, killme);
itim.it_interval.tv_sec = itim.it_interval.tv_usec = 0;
- itim.it_value = timespec_to_timeval(time_from_msec(c->time_ms));
+ itim.it_value = timespec_to_timeval(time_from_msec(c->time_ms).ts);
setitimer(ITIMER_REAL, &itim, NULL);
c->status = system(c->command);
int p[2];
struct rbuf in;
int status, ms;
- struct timespec start;
+ struct timeabs start;
*ok = false;
if (pipe(p) != 0)
signal(SIGALRM, killme);
itim.it_interval.tv_sec = itim.it_interval.tv_usec = 0;
- itim.it_value = timespec_to_timeval(time_from_msec(*timeout_ms));
+ itim.it_value = timespec_to_timeval(time_from_msec(*timeout_ms).ts);
setitimer(ITIMER_REAL, &itim, NULL);
status = system(cmd);
if (waitpid(pid, &status, 0) != pid)
err(1, "Failed to wait for child");
- ms = time_to_msec(time_sub(time_now(), start));
+ ms = time_to_msec(time_between(time_now(), start));
if (ms > *timeout_ms)
*timeout_ms = 0;
else