]> git.ozlabs.org Git - ccan/blob - ccan/timer/_info
580b115c563d289d6a1195af61cd5e53ca471124
[ccan] / ccan / timer / _info
1 #include <string.h>
2 #include "config.h"
3
4 /**
5  * timer - efficient implementation of rarely-expiring timers.
6  *
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.
9  *
10  * This is a common case for timeouts, which must often be set, but
11  * rarely expire.
12  *
13  * Example:
14  *      // Silly example which outputs strings until timers expire.
15  *      #include <ccan/timer/timer.h>
16  *      #include <ccan/time/time.h>
17  *      #include <stdlib.h>
18  *      #include <stdio.h>
19  *
20  *      struct timed_string {
21  *              struct list_node node;
22  *              struct timer timer;
23  *              const char *string;
24  *      };
25  *
26  *      int main(int argc, char *argv[])
27  *      {
28  *              struct timers timers;
29  *              struct list_head strings;
30  *              struct list_head expired;
31  *              struct timed_string *s;
32  *
33  *              timers_init(&timers, time_now());
34  *              list_head_init(&strings);
35  *
36  *              while (argv[1]) {
37  *                      s = malloc(sizeof(*s));
38  *                      s->string = argv[1];
39  *                      timer_add(&timers, &s->timer,
40  *                                timeabs_add(time_now(),
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 timeabs 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);
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/likely\n");
74                 printf("ccan/list\n");
75                 printf("ccan/time\n");
76                 return 0;
77         }
78
79         return 1;
80 }