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