-
Notifications
You must be signed in to change notification settings - Fork 351
/
MultiTimer.c
66 lines (55 loc) · 1.92 KB
/
MultiTimer.c
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
#include "MultiTimer.h"
#include <stdio.h>
static MultiTimer* timerList = NULL;
static PlatformTicksFunction_t platformTicksFunction = NULL;
int multiTimerInstall(PlatformTicksFunction_t ticksFunc) {
if (ticksFunc == NULL) {
return -1; // Indicate error if ticksFunc is NULL
}
platformTicksFunction = ticksFunc;
return 0;
}
static void removeTimer(MultiTimer* timer) {
MultiTimer** current = &timerList;
while (*current) {
if (*current == timer) {
*current = timer->next;
break;
}
current = &(*current)->next;
}
}
int multiTimerStart(MultiTimer* timer, uint64_t timing, MultiTimerCallback_t callback, void* userData) {
if (!timer || !callback || platformTicksFunction == NULL) {
return -1; // Return error if any parameter is invalid
}
removeTimer(timer); // Centralize removal logic
timer->deadline = platformTicksFunction() + timing;
timer->callback = callback;
timer->userData = userData;
MultiTimer** current = &timerList;
while (*current && ((*current)->deadline < timer->deadline)) {
current = &(*current)->next;
}
timer->next = *current;
*current = timer;
return 0;
}
int multiTimerStop(MultiTimer* timer) {
removeTimer(timer); // Use centralized removal function
return 0;
}
int multiTimerYield(void) {
if (platformTicksFunction == NULL) {
return -1; // Indicate error if platformTicksFunction is NULL
}
uint64_t currentTicks = platformTicksFunction();
while (timerList && (currentTicks >= timerList->deadline)) {
MultiTimer* timer = timerList;
timerList = timer->next; // Remove expired timer
if (timer->callback) {
timer->callback(timer, timer->userData); // Execute callback
}
}
return timerList ? (int)(timerList->deadline - currentTicks) : 0;
}