]> git.ozlabs.org Git - ccan/blob - ccan/timer/_info
timer: change to use time_mono (api break!)
[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  *              timers_init(&timers, time_mono());
35  *              list_head_init(&strings);
36  *
37  *              while (argv[1]) {
38  *                      s = malloc(sizeof(*s));
39  *                      s->string = argv[1];
40  *                      timer_addrel(&timers, &s->timer,
41  *                                   time_from_msec(atol(argv[2])));
42  *                      list_add_tail(&strings, &s->node);
43  *                      argv += 2;
44  *              }
45  *
46  *              while (!list_empty(&strings)) {
47  *                      struct timemono now = time_mono();
48  *                      list_for_each(&strings, s, node)
49  *                              printf("%s", s->string);
50  *                      while ((t = timers_expire(&timers, now)) != NULL) {
51  *                              s = container_of(t, struct timed_string, timer);
52  *                              list_del_from(&strings, &s->node);
53  *                              free(s);
54  *                      }
55  *              }
56  *
57  *              exit(0);
58  *      }
59  *
60  * License: LGPL (v2.1 or any later version)
61  * Author: Rusty Russell <rusty@rustcorp.com.au>
62  */
63 int main(int argc, char *argv[])
64 {
65         /* Expect exactly one argument */
66         if (argc != 2)
67                 return 1;
68
69         if (strcmp(argv[1], "depends") == 0) {
70                 printf("ccan/array_size\n");
71                 printf("ccan/ilog\n");
72                 printf("ccan/likely\n");
73                 printf("ccan/list\n");
74                 printf("ccan/time\n");
75                 return 0;
76         }
77
78         return 1;
79 }