-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask.c
202 lines (143 loc) · 3.42 KB
/
task.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
// SYS86 project
// Task management
#include "types.h"
#include "arch.h"
#include "int.h"
#include "task.h"
#include "heap.h"
// Array of tasks
struct task_s * tasks [TASK_MAX];
struct task_s * task_prev;
struct task_s * task_now;
struct task_s * task_next;
static int sched_lock;
static int sched_need;
//------------------------------------------------------------------------------
// Idle task
static struct task_s task_idle;
static void idle (void)
{
while (1) halt ();
}
//------------------------------------------------------------------------------
// Task scheduler
void task_lock ()
{
word_t flags;
int_save (flags);
sched_lock++;
int_restore (flags);
}
void task_unlock ()
{
word_t flags;
int_save (flags);
// May have to schedule after unlocking
if (!(--sched_lock) && sched_need) task_sched ();
int_restore (flags);
}
void task_sched (void)
{
word_t flags;
int_save (flags);
while (1)
{
// Defer scheduling if locked
if (sched_lock)
{
sched_need++;
break;
}
sched_need = 0;
struct task_s * n = NULL;
// Priority as task array index
// with 0 as highest priority
for (int i = 0; i < TASK_MAX; i++)
{
struct task_s * t = tasks [i];
if (t && t->stat == TASK_RUN)
{
n = t;
break;
}
}
// Default to idle task
// to avoid continuing in waiting task
if (!n) n = &task_idle;
if (n != task_now)
{
task_prev = task_now;
task_next = n;
// No task switch during interrupt
if (!int_level) task_switch ();
}
break;
}
int_restore (flags);
}
//------------------------------------------------------------------------------
// Wait for event occurrence and optional condition
void task_wait (struct wait_s * wait, cond_f test, void * param, int single)
{
word_t flags;
while (1)
{
// Atomic (condition test + prepare to sleep)
int_save (flags);
if (test && test (param))
{
int_restore (flags);
break;
}
wait->t = task_now;
task_now->stat = TASK_WAIT;
int_restore (flags);
// Interrupt enabled for a short time
// to give a latest chance before sleeping
task_sched ();
// No need to test the condition again
// if only this task is waiting for the object
// and no other task would change the condition
if (single) break;
}
}
// May wake up one task on event occurrence
void task_event (struct wait_s * wait)
{
word_t flags;
int_save (flags);
if (wait->t)
{
struct task_s * t = wait->t;
wait->t = NULL;
t->stat = TASK_RUN;
task_sched ();
}
int_restore (flags);
}
//------------------------------------------------------------------------------
void task_init_near (int i, struct task_s * t, void * entry, word_t size)
{
// FIXME: check returned value
t->stack = heap_alloc (size * sizeof (word_t), HEAP_TAG_STACK);
t->ssize = size;
stack_init_near (t, entry, t->stack + t->ssize);
t->stat = TASK_RUN;
if (i >= 0) tasks [i] = t;
}
void task_init_far (int i, struct task_s * t, word_t seg, word_t size)
{
// FIXME: check returned value
t->stack = heap_alloc (size * sizeof (word_t), HEAP_TAG_STACK);
t->ssize = size;
t->inter = 1;
stack_init_far (t, 0, seg, 0, seg);
t->stat = TASK_RUN;
if (i >= 0) tasks [i] = t;
}
//------------------------------------------------------------------------------
void task_init (void)
{
task_init_near (-1, &task_idle, idle, STACK_SIZE);
}
//------------------------------------------------------------------------------