6 * timer - efficient implementation of rarely-expiring timers.
8 * This is a lazy implementation of timers: you can add and delete timers
9 * very quickly, and they are only sorted as their expiry approaches.
11 * This is a common case for timeouts, which must often be set, but
15 * // Silly example which outputs strings until timers expire.
16 * #include <ccan/timer/timer.h>
17 * #include <ccan/time/time.h>
21 * struct timed_string {
22 * struct list_node node;
27 * int main(int argc, char *argv[])
29 * struct timers timers;
30 * struct list_head strings;
31 * struct list_head expired;
32 * struct timed_string *s;
34 * timers_init(&timers, time_now());
35 * list_head_init(&strings);
38 * s = malloc(sizeof(*s));
39 * s->string = argv[1];
40 * timer_add(&timers, &s->timer,
41 * timeabs_add(time_now(),
42 * time_from_msec(atol(argv[2]))));
43 * list_add_tail(&strings, &s->node);
47 * while (!list_empty(&strings)) {
48 * struct timeabs now = time_now();
49 * list_for_each(&strings, s, node)
50 * printf("%s", s->string);
51 * timers_expire(&timers, now, &expired);
52 * while ((s = list_pop(&expired, struct timed_string,
53 * timer.list)) != NULL) {
54 * list_del_from(&strings, &s->node);
62 * License: LGPL (v2.1 or any later version)
63 * Author: Rusty Russell <rusty@rustcorp.com.au>
65 int main(int argc, char *argv[])
67 /* Expect exactly one argument */
71 if (strcmp(argv[1], "depends") == 0) {
72 printf("ccan/array_size\n");
73 printf("ccan/ilog\n");
74 printf("ccan/likely\n");
75 printf("ccan/list\n");
76 printf("ccan/time\n");