-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathses_scheduler.c
157 lines (142 loc) · 3.4 KB
/
ses_scheduler.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
157
/*INCLUDES ************************************************************/
#include "ses_timer.h"
#include "util/atomic.h"
#include "ses_scheduler.h"
/* PRIVATE VARIABLES **************************************************/
/** list of scheduled tasks */
static taskDescriptor* taskList = NULL; // Head node.
volatile static systemTime_t system_clock;
/*FUNCTION DEFINITION *************************************************/
static void scheduler_update(void)
{
system_clock++; // To do, should we use atomic
// set the head node address and loop throgh the list.
taskDescriptor* head = taskList;
ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
{
while (head != NULL)
{
if (head->expire == 0)
{
head->execute = 1;
head->expire = head->period;
}
head->expire = (head->expire) - 1; // subtract ms (each firing of the timer2)
head = head->next;
}
}
}
void scheduler_init()
{
// init time 2 and set call back
timer2_setCallback(&scheduler_update);
timer2_start();
}
void scheduler_run()
{
taskDescriptor * task_address = taskList;
while (1)
{
ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
{
while (task_address != NULL)
{
// loop through each task
if( (task_address->execute) == 1)
{
break;
}
task_address = task_address->next;
}
}
// check weither the task is periodic or not
if ((task_address) == NULL)
{
// the loop has finished, reset
task_address = taskList;
}
// if execute == 1 check for periodicity
else if ((task_address->period) > 1 )
{
task_address->task(task_address->param); // execute the periodic tasks
task_address->execute = 0;
task_address = task_address->next;
}
else if ((task_address->period) == 0)
{
task_address->task(task_address->param);
scheduler_remove(task_address);
task_address = task_address->next;
}
}
}
bool scheduler_add(taskDescriptor * toAdd)
{
// the head was NULL at the begining,
//advacing in time the head changes but the next (last) task will point to NULL as well.
// make sure that the adding process not interrupted.
ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
{
taskDescriptor * holder = taskList;
if (toAdd == NULL)
{
return false;
}
// check if its already in the list
while(holder != NULL)
{
if ( holder == toAdd)
{
return false;
}
holder = holder->next;
}
toAdd->next = taskList;
taskList = toAdd;
}
return true;
}
void scheduler_remove(taskDescriptor * toRemove)
{
taskDescriptor * right;
taskDescriptor * left;
ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
{
right = toRemove->next;
left = taskList;
// find the left node.
while (left != NULL)
{
if (left->next == toRemove)
{
break;
}
left = left->next;
}
// make sure that the removing process not interrupted.
if( toRemove == taskList) // if the head node wanted to be removed
{
// set the head to the next task
taskList = toRemove->next;
}
else if (left == NULL) // toRemove variable is not in the list
{
return; // return and do not change any thing
}
else
{
left->next = right;
}
}
}
systemTime_t scheduler_getTime()
{
return system_clock;
}
void scheduler_setTime(systemTime_t time)
{
ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
{
system_clock = time;
}
}