-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimer.c
executable file
·99 lines (88 loc) · 2.04 KB
/
timer.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
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
#include <stdio.h>
#include <stdlib.h>
typedef void (*callback)(void);
/*
* data structure for timer_queue
*/
typedef struct {
int alarm_tick_count;
callback function;
struct timer_node *next;
} timer_node;
/* head points to head of timer_queue */
struct timer_node *head = NULL;
void alarm_set(void (*callback)(void), int alarm_tick_count)
{
struct timer_node *new;
struct timer_node *curr_node, *prev_node;
new = malloc(sizeof(timer_node));
if (!new)
{
return;
}
new->alarm_tick_count = alarm_tick_count;
new->function = callback;
new->next = NULL;
/*
* Sorted insert into timer_queue.
* head->node 1->node 2->NULL where
* node 1.alarm_tick_count <= node 2.alarm_tick_count
*/
if (head == NULL)
{
head = new;
return;
}
curr_node = prev_node = head;
while (curr_node != NULL)
{
if (curr_node->alarm_tick_count >= alarm_tick_count)
{
break;
}
prev_node = curr_node;
curr_node = curr_node->next;
}
if (curr_node != NULL)
{
new->next = curr_node;
}
prev_node->next = new;
return;
}
void alarm_check(int system_tick_count)
{
struct timer_node *curr_node = head;
struct timer_node *remove_node = NULL;
callback function;
while(curr_node != NULL)
{
/*
* current system tick is shorter than any
* alarm ticks
*/
if (curr_node->alarm_tick_count > system_tick_count)
break;
/*
* alarm needs to be fired at current system tick
*/
if (curr_node->alarm_tick_count == system_tick_count)
{
function = curr_node->function;
/* call callback function */
(function)();
head = curr_node->next;
free(curr_node);
curr_node = head;
}
else
{
/* highly unlikely case */
curr_node = curr_node->next;
}
}
}
int main()
{
return 1;
}