-
Notifications
You must be signed in to change notification settings - Fork 57
/
raw_pend.c
374 lines (238 loc) · 8.27 KB
/
raw_pend.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/*
raw os - Copyright (C) Lingjun Chen(jorya_txj).
This file is part of raw os.
raw os is free software; you can redistribute it it under the terms of the
GNU General Public License as published by the Free Software Foundation;
either version 3 of the License, or (at your option) any later version.
raw os is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. if not, write email to [email protected]
---
A special exception to the LGPL can be applied should you wish to distribute
a combined work that includes raw os, without being obliged to provide
the source code for any proprietary components. See the file exception.txt
for full details of how and when the exception can be applied.
*/
/* 2012-4 Created by jorya_txj
* xxxxxx please added here
*/
#include <raw_api.h>
void run_queue_init(RAW_RUN_QUEUE *rq)
{
RAW_U8 i;
/*Inisialize the lowest priority to run queue*/
rq->highest_priority = CONFIG_RAW_PRIO_MAX;
/*List init for run queue*/
for (i = 0; i < CONFIG_RAW_PRIO_MAX; i++ ) {
list_init(&rq->task_ready_list[i]);
}
}
RAW_INLINE void add_to_priority_list(LIST *head, RAW_TASK_OBJ *task_ptr)
{
RAW_U8 val;
LIST *q, *list_start, *list_end;
list_start = list_end = head;
val = task_ptr->priority;
/*Find the right position to insert*/
for (q = list_start->next; q != list_end; q = q->next) {
if (raw_list_entry(q, RAW_TASK_OBJ, task_list)->priority > val) {
break;
}
}
list_insert(q, &task_ptr->task_list);
}
RAW_INLINE void bit_clear(void *base, RAW_U8 offset)
{
register RAW_U8 *cp, mask;
cp = (RAW_U8 *)base;
cp += offset >> 3;
mask = RAW_BIT_SET_N(offset);
*cp &= (RAW_U8)(~mask);
}
RAW_INLINE void bit_set( void *base, RAW_U8 offset)
{
register RAW_U8 *cp, mask;
cp = (RAW_U8 *)base;
cp += offset >> 3;
mask = RAW_BIT_SET_N(offset);
*cp |= mask;
}
/*add task_ptr to the ready list head*/
void add_ready_list_head(RAW_RUN_QUEUE *rq, RAW_TASK_OBJ *task_ptr)
{
RAW_U8 priority = task_ptr->priority;
list_insert(rq->task_ready_list[priority].next, &task_ptr->task_list);
bit_set(rq->task_bit_map, priority);
/*update highest_priority if task has higher priority*/
if (priority < rq->highest_priority) {
rq->highest_priority = priority;
}
}
/*add task_ptr to the ready list end*/
void add_ready_list_end(RAW_RUN_QUEUE *rq, RAW_TASK_OBJ *task_ptr)
{
RAW_U8 priority = task_ptr->priority;
list_insert(&rq->task_ready_list[priority], &task_ptr->task_list);
bit_set(rq->task_bit_map, priority);
/*update highest_priority if current task has higher priority*/
if (priority < rq->highest_priority) {
rq->highest_priority = priority;
}
}
void add_ready_list(RAW_RUN_QUEUE *rq, RAW_TASK_OBJ *task_ptr)
{
/*if task priority is equal current task priority then add to the end*/
if (task_ptr->priority == raw_task_active->priority) {
add_ready_list_end(rq, task_ptr);
}
/*if not add to the list front*/
else {
add_ready_list_head(rq, task_ptr);
}
}
void remove_ready_list(RAW_RUN_QUEUE *rq, RAW_TASK_OBJ *task_ptr)
{
RAW_S32 i;
RAW_U8 priority = task_ptr->priority;
list_delete(&task_ptr->task_list);
/*if the ready list is not empty, we do not need to update the highest priority*/
if (!is_list_empty(&rq->task_ready_list[priority]) ) {
return;
}
bit_clear(rq->task_bit_map, priority);
/*If task priority not equal to the highest priority, then we do not need to update the highest priority*/
/*This condition happens when a current high priority task to suspend a low priotity task*/
if (priority != rq->highest_priority) {
return;
}
/*Find the highest ready task*/
i = bit_search_first_one(rq->task_bit_map, priority, CONFIG_RAW_PRIO_MAX - priority);
/*Update the next highest priority task*/
if (i >= 0) {
rq->highest_priority = priority + i;
}
else {
port_system_error_process(RAW_SYSTEM_CRITICAL_ERROR, 0, 0, 0, 0, 0, 0);
}
}
void move_to_ready_list_end(RAW_RUN_QUEUE *rq, RAW_TASK_OBJ *task_ptr)
{
LIST *head = &rq->task_ready_list[task_ptr->priority];
/*delete it first than add to list end again*/
list_delete(&task_ptr->task_list);
list_insert(head, &task_ptr->task_list);
}
void get_ready_task(RAW_RUN_QUEUE *rq)
{
LIST *node ;
RAW_U8 highest_pri = rq->highest_priority;
/*Highest priority task must be the first element on the list*/
node = rq->task_ready_list[highest_pri].next;
/*Get the highest priority task object*/
high_ready_obj = raw_list_entry(node, RAW_TASK_OBJ, task_list);
}
static void pend_task_wake_up(RAW_TASK_OBJ *task_ptr)
{
/*wake up task depend on the different state of task*/
switch (task_ptr->task_state) {
case RAW_PEND:
case RAW_PEND_TIMEOUT:
/*remove task on the block list because task is waken up*/
list_delete(&task_ptr->task_list);
/*add to the ready list again*/
add_ready_list(&raw_ready_queue, task_ptr);
task_ptr->task_state = RAW_RDY;
break;
case RAW_PEND_SUSPENDED:
case RAW_PEND_TIMEOUT_SUSPENDED:
/*remove task on the block list because task is waken up*/
list_delete(&task_ptr->task_list);
task_ptr->task_state = RAW_SUSPENDED;
break;
default:
port_system_error_process(RAW_SYSTEM_CRITICAL_ERROR, 0, 0, 0, 0, 0, 0);
break;
}
/*remove task on the tick list because task is waken up*/
tick_list_remove(task_ptr);
task_ptr->block_status = RAW_B_OK;
/*task is nothing blocked on so reset it to 0*/
task_ptr->block_obj = 0;
}
void raw_wake_object(RAW_TASK_OBJ *task_ptr)
{
pend_task_wake_up(task_ptr);
}
void wake_send_msg(RAW_TASK_OBJ *task_ptr, void *msg)
{
task_ptr->msg = msg;
pend_task_wake_up(task_ptr);
}
void wake_send_msg_size(RAW_TASK_OBJ *task_ptr, void *msg, RAW_U32 msg_size)
{
task_ptr->msg = msg;
task_ptr->msg_size = msg_size;
pend_task_wake_up(task_ptr);
}
void raw_pend_object(RAW_COMMON_BLOCK_OBJECT *block_common_obj, RAW_TASK_OBJ *task_ptr, RAW_TICK_TYPE timeout)
{
/*task need to remember which object is blocked on*/
task_ptr->block_obj = block_common_obj;
if (timeout == RAW_WAIT_FOREVER) {
task_ptr->task_state = RAW_PEND;
}
/*task is blocked with timeout*/
else {
/*add to time sorted tick list */
tick_list_insert(task_ptr,timeout);
task_ptr->task_state = RAW_PEND_TIMEOUT;
}
/*Remove from the ready list*/
remove_ready_list(&raw_ready_queue, task_ptr);
if (block_common_obj->block_way == RAW_BLOCKED_WAY_FIFO) {
/*add to the end of blocked objet list*/
list_insert(&block_common_obj->block_list, &task_ptr->task_list);
}
else {
/*add to the priority sorted block list*/
add_to_priority_list(&block_common_obj->block_list, task_ptr);
}
}
void delete_pend_obj(RAW_TASK_OBJ *task_ptr)
{
switch (task_ptr->task_state) {
case RAW_PEND:
case RAW_PEND_TIMEOUT:
/*remove task on the block list because task is waken up*/
list_delete(&task_ptr->task_list);
/*add to the ready list again*/
add_ready_list(&raw_ready_queue, task_ptr);
task_ptr->task_state = RAW_RDY;
break;
case RAW_PEND_SUSPENDED:
case RAW_PEND_TIMEOUT_SUSPENDED:
/*remove task on the block list because task is waken up*/
list_delete(&task_ptr->task_list);
task_ptr->task_state = RAW_SUSPENDED;
break;
default:
port_system_error_process(RAW_SYSTEM_CRITICAL_ERROR, 0, 0, 0, 0, 0, 0);
break;
}
/*remove task on the tick list because task is waken up*/
tick_list_remove(task_ptr);
task_ptr->block_status = RAW_B_DEL;
/*task is nothing blocked on so reset it to 0*/
task_ptr->block_obj = 0;
}
void change_pend_list_priority(RAW_TASK_OBJ *task_ptr)
{
RAW_COMMON_BLOCK_OBJECT *temp = task_ptr->block_obj;
if (temp->block_way == RAW_BLOCKED_WAY_PRIO) {
/*remove it first and add it again in priority sorted list*/
list_delete(&task_ptr->task_list);
add_to_priority_list(&task_ptr->block_obj->block_list, task_ptr);
}
}