AquilaOS documentation
How it works, and its code
Loading...
Searching...
No Matches
callout.c
Go to the documentation of this file.
1
11
12#include <core/system.h>
13#include <core/arch.h>
14#include <sys/callout.h>
15
17
23void callout_stop(struct callout *c)
24{
25 if (!c->pending)
26 return;
27
28 for (struct callout **p = &c->list->head; *p; p = &(*p)->next) {
29 if (*p == c) {
30 *p = c->next;
31 break;
32 }
33 }
34
35 c->pending = 0;
36 c->list = NULL;
37}
38
50void callout_set_on(struct callout_list *list, struct callout *c, uint64_t ns,
51 void (*fn)(void *), void *arg)
52{
53 callout_stop(c);
54
55 c->when = arch_rtime_ns() + ns;
56 c->fn = fn;
57 c->arg = arg;
58 c->list = list;
59 c->pending = 1;
60
61 c->next = list->head;
62 list->head = c;
63}
64
74void callout_set(struct callout *c, uint64_t ns, void (*fn)(void *), void *arg)
75{
76 callout_set_on(&timer_callouts, c, ns, fn, arg);
77}
78
87{
88 struct callout **p = &list->head;
89
90 while (*p) {
91 struct callout *c = *p;
92
93 if (now < c->when) {
94 p = &c->next;
95 continue;
96 }
97
98 /* take it off the list before calling it: it may set itself again */
99 *p = c->next;
100 c->pending = 0;
101 c->list = NULL;
102 c->fn(c->arg);
103
104 /* the function may have changed the list: start over */
105 p = &list->head;
106 }
107}
108
116{
117 uint64_t first = 0;
118
119 for (struct callout *c = list->head; c; c = c->next)
120 if (!first || c->when < first)
121 first = c->when;
122
123 return first;
124}
125
132{
134}
static struct callout_list timer_callouts
Definition callout.c:16
void callout_set(struct callout *c, uint64_t ns, void(*fn)(void *), void *arg)
Sets a callout on the timer's list: fn(arg) will be called from the timer interrupt once ns ns have p...
Definition callout.c:74
void callout_run(uint64_t now)
Calls the timer's callouts that are due, from the timer interrupt.
Definition callout.c:131
void callout_list_run(struct callout_list *list, uint64_t now)
Calls the callouts of a list that are due, taking each off the list first.
Definition callout.c:86
uint64_t callout_list_next(struct callout_list *list)
Finds when the first callout of a list is due.
Definition callout.c:115
void callout_stop(struct callout *c)
Cancels a callout, if it is pending.
Definition callout.c:23
void callout_set_on(struct callout_list *list, struct callout *c, uint64_t ns, void(*fn)(void *), void *arg)
Sets a callout on a list: fn(arg) will be called once ns ns have passed, by whoever runs the list.
Definition callout.c:50
#define NULL
Definition stddef.h:27
unsigned long long int uint64_t
Definition stddef.h:23
A list of callouts called from one context: the timer's interrupt (callout_set()),...
Definition callout.h:24
struct callout * head
Definition callout.h:25
A function to call later, with its argument.
Definition callout.h:13
void * arg
Definition callout.h:16
void(*) fn(void *arg)
Definition callout.h:15
struct callout_list * list
Definition callout.h:18
struct callout * next
Definition callout.h:17
uint64_t when
Definition callout.h:14
int pending
Definition callout.h:19
A list: its first and last nodes, and how many there are.
Definition list.h:37
struct list_node * head
Definition list.h:38
uint64_t arch_rtime_ns(void)
Reads the monotonic clock, which starts at boot.
Definition timer.c:56