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