-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutimer.c
More file actions
172 lines (119 loc) · 3.2 KB
/
Copy pathutimer.c
File metadata and controls
172 lines (119 loc) · 3.2 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include "utimer_private.h"
#include <stddef.h>
#include <errno.h>
static void deactivate(utimer_t* timer)
{
if (timer->list.next != NULL)
timer->list.next->list.prev = timer->list.prev;
*timer->list.prev = timer->list.next;
timer->list.prev = NULL;
}
static void start(utimer_scheduler_t* scheduler, utimer_t* timer, ticks_t countdown)
{
timer->timeout = scheduler->now + countdown;
if (scheduler->active == NULL) {
timer->list.next = NULL;
scheduler->active = timer;
timer->list.prev = &scheduler->active;
return;
}
for (utimer_t *iter = scheduler->active; iter != NULL; iter = iter->list.next) {
dticks_t d = iter->timeout - scheduler->now;
if (d < 0)
d = 0;
if (d > (dticks_t)countdown) {
timer->list.prev = iter->list.prev;
timer->list.next = iter;
*iter->list.prev = timer;
iter->list.prev = &timer->list.next;
break;
} else if (iter->list.next == NULL) {
timer->list.next = NULL;
iter->list.next = timer;
timer->list.prev = &iter->list.next;
break;
}
}
}
ticks_t utimer_schedule(ticks_t now)
{
utimer_scheduler_t *scheduler = utimer_scheduler_instance();
scheduler->now = now;
utimer_t *timer = scheduler->active;
while (timer != NULL) {
dticks_t d = timer->timeout - scheduler->now;
if (d > 0) // still running
return d;
ticks_t actual = timer->timeout;
utimer_fn callback = timer->callback;
void *ud = timer->ud;
deactivate(timer);
if (timer->oneshot) {
utimer_free(timer);
callback(ud);
} else {
callback(ud);
if ((timer->interval != 0) && (timer->list.prev == NULL)) { // check if callback function has already set new periodic timer
dticks_t adjust = actual + timer->interval - scheduler->now;
// maybe we already missed an interval,
// so set countdown to next possible time
if (adjust <= 0)
adjust = 1;
start(scheduler, timer, adjust);
}
}
timer = scheduler->active;
}
return 0;
}
utimer_t* utimer_new(utimer_fn callback, void *ud)
{
if (callback == NULL)
return NULL;
utimer_t *timer = utimer_alloc();
if (timer == NULL)
return NULL;
*timer = (utimer_t) {
.callback = callback,
.ud = ud,
};
return timer;
}
void utimer_delete(utimer_t* timer)
{
utimer_stop(timer);
utimer_free(timer);
}
bool utimer_active(const utimer_t* timer)
{
return timer->list.prev != NULL;
}
void utimer_start(utimer_t* timer, ticks_t countdown)
{
utimer_stop(timer);
utimer_scheduler_t *scheduler = utimer_scheduler_instance();
start(scheduler, timer, countdown);
timer->interval = 0;
}
void utimer_start_periodic(utimer_t* timer, ticks_t interval)
{
utimer_stop(timer);
utimer_scheduler_t *scheduler = utimer_scheduler_instance();
start(scheduler, timer, interval);
timer->interval = interval;
}
void utimer_stop(utimer_t* timer)
{
if (!utimer_active(timer))
return;
deactivate(timer);
}
int utimer_oneshot(ticks_t countdown, utimer_fn callback, void *ud)
{
utimer_t *timer = utimer_new(callback, ud);
if (timer == NULL)
return -ENOMEM;
timer->oneshot = true;
utimer_start(timer, countdown);
return 0;
}