]> git.ozlabs.org Git - ccan/blob - ccan/timer/_info
4c58d9e5f5bee7da8dce493b53946f84931b8975
[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 list_head expired;
32  *              struct timed_string *s;
33  *
34  *              timers_init(&timers, time_now());
35  *              list_head_init(&strings);
36  *
37  *              while (argv[1]) {
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);
44  *                      argv += 2;
45  *              }
46  *
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);
55  *                              free(s);
56  *                      }
57  *              }
58  *
59  *              exit(0);
60  *      }
61  *
62  * License: LGPL (v2.1 or any later version)
63  * Author: Rusty Russell <rusty@rustcorp.com.au>
64  */
65 int main(int argc, char *argv[])
66 {
67         /* Expect exactly one argument */
68         if (argc != 2)
69                 return 1;
70
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");
77                 return 0;
78         }
79
80         return 1;
81 }