-
Notifications
You must be signed in to change notification settings - Fork 0
/
TaskManager.c
200 lines (176 loc) · 4.01 KB
/
TaskManager.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include "TaskManager.h"
/**
* @brief Task head
*/
static TIBH_Node node;
static inline Time_t getTimeDif(Time_t now, Time_t last) {
if ((int)((int)now - (int)last) >= 0) {
return (now - last);
}
else {
return ((TIME_CNT_MAX - last + 1) + now);
}
}
inline int getTaskCnt(TIBH_Node * node){
return node->TaskCnt;
}
/**
* @brief Get the point to task head
* @param None
* @return Point to task head
*/
TIBH_Node* GetHead(void) {
return &node;
}
/**
* @brief Init the point to task head
* @param None
*/
static void TaskManagerInit(void)
{
node.TaskCnt = 0;
node.next = NULL;
TM_LOG("TM Init successed\r\n");
}
/**
* @brief Register a new task
* @param hook Callback of the new task
* @param exetime execute task time
*/
void TaskRegister(TaskFuncCallback hook, Time_t exetime, void * usrdat
){
ExecuteOnce(TaskManagerInit);
if (!hook){
TM_LOG("TM TaskRegister don't find hook\r\n");
return;
}
TaskInfoBlock * Tib = (TaskInfoBlock *)malloc(sizeof(TaskInfoBlock));
if(!Tib){
TM_LOG("TM TaskRegister create Tib Failed because malloc failed\r\n");
return;
}
Tib->TaskFlag = 0;
Tib->userdat = usrdat;
Tib->ExeTime = exetime;
Tib->hook = hook;
Tib->LastTime = 0;
Tib->ErrorTime = 0;
Tib->next = NULL;
node.TaskCnt++;
if (node.next) {
TaskInfoBlock* ptr = node.next;
while (ptr->next) {
ptr = ptr->next;
}
ptr->next = Tib;
}
else {
node.next = Tib;
}
TM_LOG("TM TaskRegister create Tib succeed\r\n");
}
/**
* @brief Free all task
* @param None
*/
void TaskFree(void)
{
TaskInfoBlock* task = node.next;
TaskInfoBlock* ptr = task;
while (task) {
ptr = task;
task = task->next;
free(ptr);
}
node.TaskCnt = 0;
TM_LOG("TM TaskFree free Tib succeed\r\n");
}
/**
* @brief Search a node according to func
* @param TaskFuncCallback
*/
inline static TaskInfoBlock* SearchPoint(TaskFuncCallback func)
{
TaskInfoBlock* task = node.next;
while (task) {
if(task->hook == func){
return task;
}
task = task->next;
}
return NULL;
}
/**
* @brief Free a task
* @param TaskFuncCallback
*/
int TaskFreeOne(TaskFuncCallback del)
{
TaskInfoBlock* task = node.next;
if(task == NULL){
return 1;
}
while (task->next) {
if(task->next->hook == del){
TaskInfoBlock* ptr = task->next;
task->next = ptr->next;
free(ptr);
node.TaskCnt --;
TM_LOG("TM TaskFree Task free\r\n");
return 0;
}
task = task->next;
}
TM_LOG("TM TaskFree Task don't find\r\n");
return 1;
}
/**
* @brief set a task suspend
* @param TaskFuncCallback
* @param Flag_Enum
*/
int TaskSetSuspend(TaskFuncCallback sus,Flag_Enum flag)
{
TaskInfoBlock* task = SearchPoint(sus);
if(task == NULL){
TM_LOG("TM TaskSetSuspend Tib don't find\r\n");
return 1;
}
if(flag == TASK_SUSPEND){
task->TaskFlag |= flag;
}
else if(flag == TASK_CANCEL_SUSPEND){
task->TaskFlag &= flag;
}
else{
TM_LOG("TM TaskSetSuspend Tib Flag_Enum don't know\r\n");
return 1;
}
TM_LOG("TM TaskSetSuspend Tib set succeed\r\n");
return 0;
}
/**
* @brief Task running
* @param gettime Nowtime
*/
int TaskRunning(Time_t gettime)
{
if (!node.TaskCnt){
TM_LOG("TM TaskRunning Tib failed because no tasks\r\n");
return 1;
}
TaskInfoBlock* task = node.next;
Time_t timedif = 0;
while(task){
timedif = getTimeDif(gettime, task->LastTime);
if(timedif >= task->ExeTime && !(task->TaskFlag & 0x00000001)){
task->ErrorTime = timedif - task->ExeTime;
task->LastTime = gettime;
/* set task Suspend */
if(task->hook(task)){ return 1;}
}
task = task->next;
}
TM_LOG("TM TaskRunning Tib succeed\r\n");
return 0;
}