timer: remove unnecessary dependency on likely
[ccan] / ccan / timer / _info
1 #include "config.h"
2 #include <stdio.h>
3 #include <string.h>
4
5 /**
6  * timer - efficient implementation of rarely-expiring timers.
7  *
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.
10  *
11  * This is a common case for timeouts, which must often be set, but
12  * rarely expire.
13  *
14  * Example:
15  *      // Silly example which outputs strings until timers expire.
16  *      #include <ccan/timer/timer.h>
17  *      #include <ccan/time/time.h>
18  *      #include <stdlib.h>
19  *      #include <stdio.h>
20  *
21  *      struct timed_string {
22  *              struct list_node node;
23  *              struct timer timer;
24  *              const char *string;
25  *      };
26  *
27  *      int main(int argc, char *argv[])
28  *      {
29  *              struct timers timers;
30  *              struct list_head strings;
31  *              struct timer *t;
32  *              struct timed_string *s;
33  *
34  *              (void)argc;
35  *              timers_init(&timers, time_mono());
36  *              list_head_init(&strings);
37  *
38  *              while (argv[1]) {
39  *                      s = malloc(sizeof(*s));
40  *                      s->string = argv[1];
41  *                      timer_addrel(&timers, &s->timer,
42  *                                   time_from_msec(atol(argv[2])));
43  *                      list_add_tail(&strings, &s->node);
44  *                      argv += 2;
45  *              }
46  *
47  *              while (!list_empty(&strings)) {
48  *                      struct timemono now = time_mono();
49  *                      list_for_each(&strings, s, node)
50  *                              printf("%s", s->string);
51  *                      while ((t = timers_expire(&timers, now)) != NULL) {
52  *                              s = container_of(t, struct timed_string, timer);
53  *                              list_del_from(&strings, &s->node);
54  *                              free(s);
55  *                      }
56  *              }
57  *
58  *              exit(0);
59  *      }
60  *
61  * License: LGPL (v2.1 or any later version)
62  * Author: Rusty Russell <rusty@rustcorp.com.au>
63  */
64 int main(int argc, char *argv[])
65 {
66         /* Expect exactly one argument */
67         if (argc != 2)
68                 return 1;
69
70         if (strcmp(argv[1], "depends") == 0) {
71                 printf("ccan/array_size\n");
72                 printf("ccan/ilog\n");
73                 printf("ccan/list\n");
74                 printf("ccan/time\n");
75                 return 0;
76         }
77
78         return 1;
79 }