-
Notifications
You must be signed in to change notification settings - Fork 0
/
timers.c
156 lines (134 loc) · 4.17 KB
/
timers.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
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
#include <stddef.h>
#include <stdint.h>
#include "timers.h"
#include "clib/stdio.h"
#include "heap.h"
#include "uni_list.h"
#include "threads.h"
#include "clock.h"
#include "heap.h"
// Funkcje statyczne
static void timer_thread(void);
static void timer_insert(TIMER *timer, uint32_t time);
static TIMER *timer_create(TIMER_TASK task, uint32_t time, uint32_t period);
// Lista struktur będących timerami
UNI_LIST_C(timers, TIMER*)
// delta queue
struct list_timers_t *delta_queue = NULL;
uint32_t thread_id = 0;
uint64_t last_time = 0;
// Inicjlizacja timerów
void timer_initialize(void)
{
printf("Timers initialization\n");
delta_queue = list_timers_create();
last_time = clock();
// Tworzy wątek i uruchamia
thread_id = create_thread((uint32_t)timer_thread);
start_thread(thread_id);
}
// Wątek kontrolujący timery, działa w tle przez cały czas
static void timer_thread(void)
{
while(1)
{
uint64_t current_time = clock();
uint32_t ellapsed_time = (uint32_t)(current_time - last_time);
last_time = current_time;
if(delta_queue->size>0)
{
TIMER *front = list_timers_front(delta_queue);
front->time -= ellapsed_time;
if(front->time<=0)
{
front->task();
TIMER *timer = list_timers_pop_front(delta_queue);
// Timer miał był wykonany tylko raz
if(timer->period == 0)
kfree(timer);
// Timer ma być wykonywany w kółko
else
timer_insert(timer, timer->period);
}
}
yield();
}
}
// Funkcja pomocnicza, tworzy nowy timer
static TIMER *timer_create(TIMER_TASK task, uint32_t time, uint32_t period)
{
TIMER *timer = (TIMER*)kmalloc(sizeof(TIMER))
timer->task = task;
timer->time = time;
timer->period = period;
return timer;
}
// Funkcja pomocnicza, wstawia timer w odpowiednia miejsce w kolejce
static void timer_insert(TIMER *timer, uint32_t time)
{
// Proste dodanie węzła na początek kolejki gdy jest ona pusta
if(delta_queue->size == 0)
{
timer->time = time;
list_timers_push_front(delta_queue, timer);
return;
}
// Nieco trudniejszy wariant aktualizacji
else
{
uint32_t current_delay = 0;
struct node_timers_t *current_node = delta_queue->head;
for(uint32_t i=0; i<delta_queue->size; i++)
{
current_delay += current_node->data->time;
if(current_delay >= time)
{
// Wstawienie na początek kolejki
if(current_node == delta_queue->head)
{
current_node->data->time -= time;
timer->time = time;
list_timers_push_front(delta_queue, timer);
return;
}
// Wstawienie w środek kolejki
else
{
int32_t temp = current_node->data->time;
current_node->data->time = current_delay - time;
timer->time = temp-current_node->data->time;
list_timers_insert(delta_queue, timer, i);
return;
}
}
current_node = current_node->next;
}
// Wstawienie na koniec kolejki
timer->time = time-current_delay;
list_timers_push_back(delta_queue, timer);
return;
}
}
// Funkcja dodaje nowy timer
TIMER *timer_add(TIMER_TASK task, uint32_t time, uint32_t period)
{
TIMER *timer = timer_create(task, 0, period);
timer_insert(timer, time);
return timer;
}
// Funkcja usówa timer
void timer_remove(TIMER *timer)
{
struct node_timers_t *current_node = delta_queue->head;
for(uint32_t i=0; i<delta_queue->size; i++)
{
if(current_node->data == timer)
{
if(current_node->next!=NULL)
current_node->next->data->time += current_node->data->time;
list_timers_remove_node(delta_queue, current_node);
return;
}
current_node = current_node->next;
}
}