Mercurial > hg > Game > Cerium
annotate TaskManager/kernel/schedule/SchedTask.cc @ 310:364abe879376 draft
remove smanager
author | e065746@localhost.localdomain |
---|---|
date | Mon, 08 Jun 2009 20:31:29 +0900 |
parents | b85b920628e2 |
children | 74a1e261e734 |
rev | line source |
---|---|
308 | 1 #include <stdlib.h> |
2 #include <string.h> | |
42 | 3 #include "SchedTask.h" |
308 | 4 #include "SchedTask.h" |
5 #include "SchedTaskList.h" | |
6 #include "SchedNop2Ready.h" | |
7 #include "DmaManager.h" | |
8 #include "error.h" | |
9 #include "TaskManager.h" | |
180
e3b7776b1420
いろいろ fix 。詳しくは TaskManager/Changelog、test_render/Changelog を
gongo@localhost.localdomain
parents:
109
diff
changeset
|
10 |
308 | 11 extern Scheduler::TaskObject task_list[MAX_TASK_OBJECT]; |
12 | |
13 //#define NO_PIPELINE | |
14 | |
15 SchedTask * | |
302 | 16 createSchedTask(TaskPtr task) |
17 { | |
308 | 18 return task_list[task->command](); |
19 } | |
20 | |
21 | |
22 SchedTask::SchedTask() | |
23 { | |
24 __list = NULL; | |
25 __task = NULL; | |
26 __inListData = NULL; | |
27 __outListData = NULL; | |
28 __readbuf = NULL; | |
29 __writebuf = NULL; | |
30 __scheduler = NULL; | |
31 __taskGroup = NULL; | |
32 __renew_flag = 0; | |
33 __cur_index = 0; | |
34 __flag_renewTask = SCHED_TASK_NORMAL; | |
35 | |
36 ex_init = &SchedTask::ex_init_normal; | |
37 ex_read = &SchedTask::ex_read_normal; | |
38 ex_exec = &SchedTask::ex_exec_normal; | |
39 ex_write = &SchedTask::ex_write_normal; | |
40 ex_next = &SchedTask::ex_next_normal; | |
41 | |
42 run_func = &SchedTask::run; | |
43 } | |
44 | |
45 /** | |
46 * dma_store の wait を行う | |
47 * このタスクが RenewTask だった場合、 | |
48 * __inListData や __outListData は | |
49 * Scheduler の持つ、使い回しの buffer ではなく | |
50 * 新たに allocate されたものなので、ここで free する | |
51 */ | |
52 SchedTask::~SchedTask(void) | |
53 { | |
54 if (__flag_renewTask == SCHED_TASK_RENEW) { | |
55 free(__inListData); | |
56 free(__outListData); | |
57 | |
58 /** | |
59 * __list != NULL の場合、 | |
60 * この Task が __list の最後の Task になるので (SchedTask::next 参照) | |
61 * このタイミングで __list を解放する | |
62 * (free に渡されるアドレスが正しいものとなる)。 | |
63 * それ以外の Task では当然解放しない。 | |
64 * __list == NULL なので、free に渡しても無問題 | |
65 */ | |
66 free(__list); | |
67 } | |
68 | |
310 | 69 |
308 | 70 } |
71 | |
72 /** | |
73 * このタスクを Renew Task とし、それに応じた関数をセットする | |
74 */ | |
75 void | |
76 SchedTask::__setRenew(void) | |
77 { | |
78 __flag_renewTask = SCHED_TASK_RENEW; | |
79 | |
80 ex_init = &SchedTask::ex_init_renew; | |
81 ex_read = &SchedTask::ex_read_renew; | |
82 ex_exec = &SchedTask::ex_exec_renew; | |
83 ex_write = &SchedTask::ex_write_renew; | |
84 ex_next = &SchedTask::ex_next_renew; | |
85 } | |
86 | |
87 void | |
88 SchedTask::__init__(TaskListPtr _list, TaskPtr _task, int index, | |
89 ListDataPtr rbuf, ListDataPtr wbuf, Scheduler* sc) | |
90 { | |
91 __list = _list; | |
92 __task = _task; | |
93 __inListData = rbuf; | |
94 __outListData = wbuf; | |
95 __scheduler = sc; | |
96 __cur_index = index; | |
97 | |
310 | 98 |
308 | 99 |
100 __scheduler->mainMem_wait(); | |
101 | |
102 (this->*ex_init)(); | |
103 } | |
104 | |
105 /** | |
106 * PPE 内で生成されたタスクの ex_init() | |
107 */ | |
108 void | |
109 SchedTask::ex_init_normal(void) | |
110 { | |
111 __scheduler->dma_load(__inListData, (uint32)__task->inData, | |
112 sizeof(ListData), DMA_READ_IN_LIST); | |
113 __scheduler->dma_load(__outListData, (uint32)__task->outData, | |
114 sizeof(ListData), DMA_READ_OUT_LIST); | |
115 #if defined(NO_PIPELINE) | |
116 __scheduler->dma_wait(DMA_READ_IN_LIST); | |
117 __scheduler->dma_wait(DMA_READ_OUT_LIST); | |
118 #endif | |
119 | |
120 __taskGroup = new TaskGroup; | |
121 __taskGroup->command = __task->self; | |
122 } | |
123 | |
124 /** | |
125 * SPE 内で生成されたタスクの ex_init() | |
126 * 各データは SPE 内の create_task 時に生成もしくは引き継がれているので | |
127 * ex_init_normal() と違い、ここでは値を渡すだけ | |
128 */ | |
129 void | |
130 SchedTask::ex_init_renew(void) | |
131 { | |
132 __inListData = __task->inData; | |
133 __outListData = __task->outData; | |
134 __taskGroup = (TaskGroupPtr)__task->self; | |
135 } | |
136 | |
137 /** | |
138 * [Todo] | |
139 * データの読み込み場所を readbuf ではなく、 | |
140 * ユーザ自身で決めれるようになるといいかもしれない。 | |
141 * | |
142 * # TaskManager が勝手に消すことなく、 | |
143 * # ユーザが SPE 上に持ち続けることができるため。 | |
144 * # もちろん管理はユーザに任せるわけだ。 | |
145 */ | |
146 void | |
147 SchedTask::read(void) | |
148 { | |
149 __debug("[SchedTask:%s]\n", __FUNCTION__); | |
150 | |
151 #if !defined(NO_PIPELINE) | |
152 __scheduler->dma_wait(DMA_READ_IN_LIST); | |
153 __scheduler->dma_wait(DMA_READ_OUT_LIST); | |
154 #endif | |
155 | |
156 __writebuf = __scheduler->allocate(__outListData->size); | |
157 | |
158 // 読むデータが一つもなければ無視 | |
159 if (__inListData->length == 0) return; | |
160 | |
161 // load Input Data | |
162 __readbuf = __scheduler->allocate(__inListData->size); | |
163 __scheduler->dma_loadList(__inListData, __readbuf, DMA_READ); | |
164 | |
165 #if defined(NO_PIPELINE) | |
166 __scheduler->dma_wait(DMA_READ); | |
167 #endif | |
168 | |
169 (this->*ex_read)(); | |
170 } | |
171 | |
172 void | |
173 SchedTask::exec(void) | |
174 { | |
175 __debug("[SchedTask:%s]\n", __FUNCTION__); | |
176 | |
177 #if !defined(NO_PIPELINE) | |
178 __scheduler->dma_wait(DMA_READ); | |
179 #endif | |
180 | |
181 //run(__readbuf, __writebuf); | |
182 (this->*run_func)(__readbuf, __writebuf); | |
183 | |
184 free(__readbuf); | |
185 | |
186 if (__taskGroup->status() != 0) { | |
187 __task->self = __taskGroup->command; | |
188 delete __taskGroup; | |
189 __taskGroup = NULL; | |
190 } | |
191 | |
192 | |
193 // 書き込む領域がなければ無視 | |
194 if (__outListData->length > 0) { | |
195 __scheduler->dma_storeList(__outListData, __writebuf, DMA_WRITE); | |
196 | |
197 #if defined(NO_PIPELINE) | |
198 __scheduler->dma_wait(DMA_WRITE); | |
199 free(__writebuf); | |
200 #endif | |
201 } | |
202 | |
203 (this->*ex_exec)(); | |
204 } | |
205 | |
206 void | |
207 SchedTask::write(void) | |
208 { | |
209 __debug("[SchedTask:%s]\n", __FUNCTION__); | |
210 | |
211 #if !defined(NO_PIPELINE) | |
212 __scheduler->dma_wait(DMA_WRITE); | |
213 free(__writebuf); | |
214 #endif | |
215 | |
216 if (__task->self == MY_SPE_NOP) return; | |
217 | |
218 (this->*ex_write)(); | |
219 } | |
220 | |
221 /** | |
222 * PPE 内で生成されたタスクの ex_read() | |
223 */ | |
224 void | |
225 SchedTask::ex_read_normal(void) | |
226 { | |
227 } | |
228 | |
229 /** | |
230 * SPE 内で生成されたタスクの ex_read() | |
231 */ | |
232 void | |
233 SchedTask::ex_read_renew(void) | |
234 { | |
235 } | |
236 | |
237 /** | |
238 * PPE 内で生成されたタスクの ex_exec() | |
239 */ | |
240 void | |
241 SchedTask::ex_exec_normal(void) | |
242 { | |
243 } | |
244 | |
245 /** | |
246 * SPE 内で生成されたタスクの ex_exec() | |
247 */ | |
248 void | |
249 SchedTask::ex_exec_renew(void) | |
250 { | |
251 } | |
252 | |
253 | |
254 | |
255 /** | |
256 * PPE 内で生成されたタスクの ex_write() | |
257 * | |
258 * このタスク内で新たにタスクが生成され、 | |
259 * 且つそのタスクの終了を待つ必要がある場合、 | |
260 * PPE に終了したことは知らせない(command は送信しない) | |
261 */ | |
262 void | |
263 SchedTask::ex_write_normal(void) | |
264 { | |
265 /** | |
266 * このタスク内で新たにタスクが生成されなかった | |
267 * or 生成されたが、そのタスクの終了を待つ必要は無い | |
268 */ | |
269 if (__renew_flag == 0) { | |
270 __scheduler->mail_write(__task->self); | |
271 } | |
302 | 272 } |
273 | |
308 | 274 /** |
275 * SPE 内で生成されたタスクの ex_write() | |
276 * | |
277 * A <- 親タスク | |
278 * | \ | |
279 * B C <- SPE 内で生成されたタスク | |
280 * | |
281 * A は SPE 内で B, C を生成したとする。 | |
282 * B と C が終了したら、A が PPE に送るはずだったコマンドが | |
283 * 子タスクに引き継がれているので、最後に実行された子タスクが | |
284 * PPE に mail 送信する。 | |
285 */ | |
286 void | |
287 SchedTask::ex_write_renew(void) | |
288 { | |
289 uint32 cmd; | |
290 | |
291 __taskGroup->remove(__task); | |
292 cmd = __taskGroup->status(); | |
293 | |
294 // タスク内で作られた全てのタスクが終了した | |
295 if (cmd != 0) { | |
296 delete __taskGroup; | |
297 __scheduler->mail_write(cmd); | |
298 } | |
299 } | |
300 | |
301 SchedTaskBase* | |
302 SchedTask::next(Scheduler *m, SchedTaskBase *p) | |
303 { | |
304 __debug("[SchedTask:%s]\n", __FUNCTION__); | |
305 | |
306 delete p; | |
307 | |
308 return (this->*ex_next)(); | |
309 } | |
310 | |
311 SchedTaskBase* | |
312 SchedTask::ex_next_normal(void) | |
313 { | |
314 if (__cur_index < __list->length) { | |
315 SchedTaskBase *nextSched; | |
316 | |
317 nextSched = __scheduler->get_nextRenewTaskList(); | |
318 | |
319 // RenewTask がある | |
320 if (nextSched) { | |
321 __scheduler->set_backupTaskList(__list); | |
322 __scheduler->set_backupTaskListIndex(__cur_index); | |
323 return nextSched; | |
324 } else { | |
325 TaskPtr nextTask = &__list->tasks[__cur_index++]; | |
326 nextSched = createSchedTask(nextTask); | |
327 ((SchedTask*)nextSched)->__init__(__list, nextTask, __cur_index, | |
328 __scheduler->get_curReadBuf(), | |
329 __scheduler->get_curWriteBuf(), | |
330 __scheduler); | |
331 return nextSched; | |
332 } | |
333 } else { | |
334 uint32 nextList = (uint32)__list->next; | |
335 | |
336 if (nextList == 0) { | |
337 return new SchedNop2Ready(__scheduler); | |
338 } else { | |
339 return createSchedTaskList(nextList, __scheduler, | |
340 SCHED_TASKLIST_NORMAL); | |
341 } | |
342 } | |
343 } | |
344 | |
345 /** | |
346 * | |
347 */ | |
348 SchedTaskBase* | |
349 SchedTask::ex_next_renew(void) | |
350 { | |
351 TaskPtr nextTask; | |
352 SchedTask *nextSched; | |
353 | |
354 if (__cur_index < __list->length) { | |
355 nextTask = &__list->tasks[__cur_index++]; | |
356 nextSched = createSchedTask(nextTask); | |
357 | |
358 // RenewTaskList を実行中なので | |
359 nextSched->__setRenew(); | |
360 nextSched->__init__(__list, nextTask, __cur_index, | |
361 __scheduler->get_curReadBuf(), | |
362 __scheduler->get_curWriteBuf(), | |
363 __scheduler); | |
364 | |
365 /** | |
366 * この理由は SchedTask:~SchedTask() で | |
367 */ | |
368 __list = NULL; | |
369 return nextSched; | |
370 } else { | |
371 SchedTaskBase *nextList; | |
372 | |
373 nextList = __scheduler->get_nextRenewTaskList(); | |
374 | |
375 if (nextList) { | |
376 return nextList; | |
377 } else { | |
378 TaskListPtr nextList = __scheduler->get_backupTaskList(); | |
379 | |
380 // 中断した TaskList がある | |
381 if (nextList) { | |
382 __cur_index = __scheduler->get_backupTaskListIndex(); | |
383 | |
384 nextTask = &nextList->tasks[__cur_index++]; | |
385 nextSched = createSchedTask(nextTask); | |
386 | |
387 nextSched->__init__(nextList, nextTask, __cur_index, | |
388 __scheduler->get_curReadBuf(), | |
389 __scheduler->get_curWriteBuf(), | |
390 __scheduler); | |
391 return nextSched; | |
392 } else { | |
393 return new SchedNop2Ready(__scheduler); | |
394 } | |
395 } | |
396 } | |
397 } | |
398 | |
399 int | |
400 SchedTask::get_cpuid(void) | |
401 { | |
402 return __scheduler->id; | |
403 } | |
404 | |
405 /** | |
406 * task->add_inData で与えられた順番に対応する index (0〜n-1) で、 | |
407 * buffer から対応するデータを返す。 | |
408 */ | |
409 void* | |
410 SchedTask::get_input(void *buff, int index) | |
411 { | |
412 if (buff != NULL) { | |
413 return (void*)((int)buff + __inListData->bound[index]); | |
414 } else { | |
415 return NULL; | |
416 } | |
417 } | |
418 | |
419 /** | |
420 * get_input(index) のアドレスを返す | |
421 */ | |
422 uint32 | |
423 SchedTask::get_inputAddr(int index) | |
424 { | |
425 return __inListData->element[index].addr; | |
426 } | |
427 | |
428 /** | |
429 * get_input(index) のサイズを返す | |
430 */ | |
431 int | |
432 SchedTask::get_inputSize(int index) | |
433 { | |
434 return __inListData->element[index].size; | |
435 } | |
436 | |
437 /** | |
438 * write buffer の領域を返す。 | |
439 */ | |
440 void* | |
441 SchedTask::get_output(void *buff, int index) | |
442 { | |
443 if (buff != NULL) { | |
444 return (void*)((int)buff + __outListData->bound[index]); | |
445 } else { | |
446 return NULL; | |
447 } | |
448 } | |
449 | |
450 /** | |
451 * get_output(index) のアドレスを返す | |
452 */ | |
453 uint32 | |
454 SchedTask::get_outputAddr(int index) | |
455 { | |
456 return __outListData->element[index].addr; | |
457 } | |
458 | |
459 /** | |
460 * get_output(index) のサイズを返す | |
461 */ | |
462 int | |
463 SchedTask::get_outputSize(int index) | |
464 { | |
465 return __outListData->element[index].size; | |
466 } | |
467 | |
468 int | |
469 SchedTask::get_param(int index) | |
470 { | |
471 return __task->param[index]; | |
472 } | |
473 | |
474 TaskPtr | |
475 SchedTask::create_task(int cmd) | |
476 { | |
477 TaskListPtr taskList = __scheduler->get_renewListBuf(); | |
478 TaskPtr p = &taskList->tasks[taskList->length++]; | |
479 p->command = cmd; | |
480 | |
481 p->inData = (ListData*)__scheduler->allocate(sizeof(ListData)); | |
482 p->outData = (ListData*)__scheduler->allocate(sizeof(ListData)); | |
483 | |
484 p->inData->clear(); | |
485 p->outData->clear(); | |
486 | |
487 p->self = MY_SPE_NOP; | |
488 p->param_size = 0; | |
489 | |
490 return p; | |
491 } | |
492 | |
493 /** | |
494 * 生成したタスクが終了してから、メインスケジューラ(PPE) に | |
495 * タスクが終了した旨を知らせる。 | |
496 * | |
497 * @param[in] waitTask タスク内で生成したタスク | |
498 */ | |
499 void | |
500 SchedTask::wait_task(TaskPtr waitTask) | |
501 { | |
502 waitTask->self = (uint32)__taskGroup; | |
503 | |
504 __scheduler->add_groupTask(__taskGroup, waitTask); | |
505 | |
506 __renew_flag++; | |
507 } | |
508 | |
509 void* | |
510 SchedTask::global_alloc(int id, int size) { | |
511 return __scheduler->global_alloc(id, size); | |
512 } | |
513 | |
514 void* | |
515 SchedTask::global_get(int id) { | |
516 return __scheduler->global_get(id); | |
517 } | |
518 | |
519 void | |
520 SchedTask::global_free(int id) { | |
521 __scheduler->global_free(id); | |
522 } | |
523 | |
524 void | |
525 SchedTask::mainMem_alloc(int id, int size) { | |
526 __scheduler->mainMem_alloc(id, size); | |
527 } | |
528 | |
529 void | |
530 SchedTask::mainMem_wait(void) { | |
531 __scheduler->mainMem_wait(); | |
532 } | |
533 | |
534 void* | |
535 SchedTask::mainMem_get(int id) { | |
536 return __scheduler->mainMem_get(id); | |
537 } | |
538 | |
539 void* | |
540 SchedTask::allocate(int size) { | |
541 return __scheduler->allocate(size); | |
542 } | |
543 | |
544 void | |
545 SchedTask::dma_load(void *buf, uint32 addr, uint32 size, uint32 mask) { | |
546 __scheduler->dma_load(buf, addr, size, mask); | |
547 } | |
548 | |
549 void | |
550 SchedTask::dma_store(void *buf,uint32 addr, uint32 size, uint32 mask) { | |
551 __scheduler->dma_store(buf, addr, size, mask); | |
552 } | |
553 | |
554 void | |
555 SchedTask::dma_wait(uint32 mask) { | |
556 __scheduler->dma_wait(mask); | |
557 } | |
558 | |
559 /* end */ |