-
Notifications
You must be signed in to change notification settings - Fork 0
/
task.c
282 lines (240 loc) · 4.19 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
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
/* Copyright (c) 2005 Russ Cox, MIT; see COPYRIGHT */
#include <u.h>
#include <libc.h>
#include "task.h"
#include "taskimpl.h"
Taskconf *taskconf;
int taskcount;
int tasknswitch;
int taskexitval;
Task *taskrunning;
Tasklist taskrunqueue;
static int taskidgen;
/* alignptr adjusts the memory region defined by the provided pointer and size
* to 8-byte boundaries; it returns the adjusted pointer, and updates the
* size argument. The number of bytes the pointer was adjusted is returned in na0.
*/
static void*
alignptr(void *p, int *sz, int *na0)
{
uintptr p0, pa;
int n;
/* align p */
p0 = (uintptr)p;
pa = (p0 + 7) & ~(uintptr)7;
n = pa-p0;
if (n > *sz) {
*sz = 0;
} else {
*sz -= n;
}
*na0 = n;
/* align size */
*sz &= ~7;
return (void*)pa;
}
static Task*
setuptask(void (*fn)(void*), void *arg, void *stk, int stksize)
{
Task *t;
void *origstk;
int na0;
if (stk == nil) {
return nil;
}
origstk = stk;
stk = alignptr(stk, &stksize, &na0);
if (stk == nil || stksize == 0) {
return nil;
}
/* allocate a Task struct from the end of the aligned stack */
stksize -= sizeof *t;
stksize &= ~7;
t = (Task*)&((uchar*)stk)[stksize];
if (stksize <= 0) {
return nil;
}
t->stk = stk;
t->stksize = stksize;
t->id = ++taskidgen;
if (taskconf->memfill != nil) {
(*taskconf->memfill)(origstk, na0 + stksize);
}
/* setup stack with initial context */
taskcm_setuptask(t, fn, arg);
return t;
}
int
taskcreate(void (*fn)(void*), void *arg, void *stk, uint stksize)
{
Task *t;
int id;
t = setuptask(fn, arg, stk, stksize);
if (t == nil) {
return -1;
}
taskcount++;
id = t->id;
taskready(t);
return id;
}
void
taskready(Task *t)
{
t->ready = 1;
addtask(&taskrunqueue, t);
}
int
taskyield(void)
{
int n;
n = tasknswitch;
taskready(taskrunning);
taskswitch();
return tasknswitch - n - 1;
}
int
anyready(void)
{
return taskrunqueue.head != nil;
}
void
taskexit(int val)
{
taskexitval = val;
// taskrunning->exiting = 1;
taskcount--;
taskswitch();
}
static Task *sq;
static int32 sqbasetime;
void
taskswitch(void)
{
int32 now;
int32 left;
Task *t;
// taskdebug("scheduler enter");
for(;;){
if (sq != nil) {
now = taskconf->usec() - taskconf->usec_uncertainty;
/* add a task that is done sleeping to the front of the runqueue */
if (now-sqbasetime >= sq->delay) {
t = sq;
sqbasetime += t->delay;
sq = t->next;
addtasktofront(&taskrunqueue, t);
}
}
// if(taskcount == 0)
// exit(taskexitval);
t = taskrunqueue.head;
if(t == nil){
left = 0;
if (sq != nil) {
left = sq->delay - (now - sqbasetime);
}
taskconf->pauseusecs(left);
continue;
}
deltask(&taskrunqueue, t);
t->ready = 0;
if (t == taskrunning) {
/* t was already running, don't perform a switch */
break;
}
tasknswitch++;
taskcm_contextswitch(taskrunning, t);
break;
}
}
void**
taskdata(void)
{
return &taskrunning->udata;
}
/*
* hooray for linked lists
*/
void
addtask(Tasklist *l, Task *t)
{
if(l->tail){
l->tail->next = t;
t->prev = l->tail;
}else{
l->head = t;
t->prev = nil;
}
l->tail = t;
t->next = nil;
}
void
addtasktofront(Tasklist *l, Task *t)
{
if (l->head) {
l->head->prev = t;
t->next = l->head;
} else {
l->tail = t;
t->next = nil;
}
l->head = t;
t->prev = nil;
}
void
deltask(Tasklist *l, Task *t)
{
if(t->prev)
t->prev->next = t->next;
else
l->head = t->next;
if(t->next)
t->next->prev = t->prev;
else
l->tail = t->prev;
}
unsigned int
taskid(void)
{
return taskrunning->id;
}
static void
addsleeptask(Task *t, int32 us)
{
Task *q, *qprev;
int32 now;
now = taskconf->usec();
t->delay = us;
if (sq == nil) {
/* set new base time */
sqbasetime = now;
}
/* add to sleep queue */
qprev = nil;
for (q=sq; q != nil; qprev = q, q = q->next) {
if ((t->delay - q->delay) < 0) {
/* t will finish earlier than the next - insert here */
break;
} else {
/* t will finish later - adjust delay */
t->delay -= q->delay;
}
}
if (q != nil) {
/* cut delay time between this sleep task and the next */
q->delay -= t->delay;
}
t->next = q;
if (qprev == nil) {
sq = t;
} else {
qprev->next = t;
}
}
void
taskdelay(int32 us)
{
addsleeptask(taskrunning, us);
taskswitch();
}