Mercurial > hg > Game > Cerium
annotate TaskManager/Fifo/FifoTaskManagerImpl.cc @ 538:5641d121818e draft
code_load in read()
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Mon, 12 Oct 2009 02:50:01 +0900 |
parents | 981aa2f89a80 |
children | 92b0d490e839 |
rev | line source |
---|---|
3 | 1 #include <stdio.h> |
2 #include <stdlib.h> | |
3 #include <string.h> | |
4 #include "FifoTaskManagerImpl.h" | |
373 | 5 #include "Scheduler.h" |
47 | 6 #include "types.h" |
109 | 7 #include "error.h" |
3 | 8 |
475
4e0308d2ba73
BufferManager removed.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
373
diff
changeset
|
9 FifoTaskManagerImpl::~FifoTaskManagerImpl() |
109 | 10 { |
11 delete mailManager; | |
12 delete scheduler; | |
475
4e0308d2ba73
BufferManager removed.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
373
diff
changeset
|
13 |
4e0308d2ba73
BufferManager removed.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
373
diff
changeset
|
14 delete taskListImpl ; |
4e0308d2ba73
BufferManager removed.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
373
diff
changeset
|
15 delete taskQueueImpl ; |
4e0308d2ba73
BufferManager removed.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
373
diff
changeset
|
16 delete htaskImpl ; |
4e0308d2ba73
BufferManager removed.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
373
diff
changeset
|
17 |
109 | 18 } |
19 | |
20 /** | |
321 | 21 * MailManager は PPE スケジューラとのメール交換、 |
22 * FifoScheduler は PPE 側のスケジューラ | |
23 * BufferManager は Task、TaskList などのメモリ管理(大層なことしてないが | |
109 | 24 */ |
3 | 25 void |
475
4e0308d2ba73
BufferManager removed.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
373
diff
changeset
|
26 FifoTaskManagerImpl::init() |
3 | 27 { |
109 | 28 mailManager = new MailManager(); |
29 mailManager->init(20); | |
30 | |
31 scheduler = new MainScheduler(); | |
32 scheduler->init(); | |
33 scheduler->set_mailManager(mailManager); | |
194 | 34 scheduler->id = 0; |
42 | 35 |
475
4e0308d2ba73
BufferManager removed.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
373
diff
changeset
|
36 taskListImpl = new TaskListInfo; |
4e0308d2ba73
BufferManager removed.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
373
diff
changeset
|
37 taskQueueImpl = new TaskQueueInfo; |
498
bce667ff20b9
double linked HTaskInfo/HTask
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
494
diff
changeset
|
38 htaskImpl = new HTaskInfo(); |
475
4e0308d2ba73
BufferManager removed.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
373
diff
changeset
|
39 |
4e0308d2ba73
BufferManager removed.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
373
diff
changeset
|
40 mainTaskList = taskListImpl->create(); |
4e0308d2ba73
BufferManager removed.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
373
diff
changeset
|
41 |
109 | 42 } |
43 | |
44 /** | |
321 | 45 * これは CellTaskManagerImpl から呼ばれる。 |
475
4e0308d2ba73
BufferManager removed.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
373
diff
changeset
|
46 * TaskList等 は共用で使うので引数に。 |
321 | 47 * CellTaskManagerImpl と FifoTaskManagerImpl が同時に |
373 | 48 * 上のデータにアクセスする事は(今は)ないのでこれでおk |
109 | 49 */ |
50 void | |
475
4e0308d2ba73
BufferManager removed.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
373
diff
changeset
|
51 FifoTaskManagerImpl::init(MainScheduler *_sched, TaskManagerImpl *tm) |
109 | 52 { |
53 mailManager = new MailManager(); | |
54 mailManager->init(20); | |
55 | |
56 //scheduler = new MainScheduler(); | |
57 scheduler = _sched; | |
58 scheduler->init(); | |
59 scheduler->set_mailManager(mailManager); | |
60 | |
475
4e0308d2ba73
BufferManager removed.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
373
diff
changeset
|
61 taskListImpl = tm-> taskListImpl ; |
4e0308d2ba73
BufferManager removed.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
373
diff
changeset
|
62 taskQueueImpl = tm-> taskQueueImpl ; |
4e0308d2ba73
BufferManager removed.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
373
diff
changeset
|
63 htaskImpl = tm-> htaskImpl ; |
485 | 64 waitTaskQueue = NULL; // mail_check で外から設定される |
486
58b7ac6588b9
activeTaskQueue on Fifo is necessary on Cell
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
485
diff
changeset
|
65 // waitTaskQueue = tm->waitTaskQueue; |
58b7ac6588b9
activeTaskQueue on Fifo is necessary on Cell
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
485
diff
changeset
|
66 // activeTaskQueue = NULL; // CellTaskManagerImple 側を使う |
485 | 67 |
68 // waitTaskQueue = tm->waitTaskQueue; | |
479
bf2d2625485e
Double Linked List base TaskQueue
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
475
diff
changeset
|
69 // activeQueue は? |
109 | 70 |
475
4e0308d2ba73
BufferManager removed.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
373
diff
changeset
|
71 mainTaskList = taskListImpl->create(); |
4e0308d2ba73
BufferManager removed.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
373
diff
changeset
|
72 |
479
bf2d2625485e
Double Linked List base TaskQueue
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
475
diff
changeset
|
73 |
109 | 74 } |
75 | |
76 /** | |
321 | 77 * スケジューラに渡す TaskList を取得する。 |
109 | 78 * |
321 | 79 * @return 実行タスクリスト |
109 | 80 * |
321 | 81 * ActiveTaskQueue (依存条件は満たし済み) のタスクを |
82 * 実行タスクリストに入れる | |
109 | 83 */ |
84 TaskListPtr | |
475
4e0308d2ba73
BufferManager removed.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
373
diff
changeset
|
85 FifoTaskManagerImpl::get_runTaskList() |
109 | 86 { |
87 TaskListPtr list, list_top; | |
321 | 88 TaskPtr task; // Task (SPE に送る Task) |
109 | 89 |
480
75e4afa40da2
TaskQueueInfo initiaization...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
479
diff
changeset
|
90 if (activeTaskQueue->empty()) { |
373 | 91 return NULL; |
109 | 92 } |
93 | |
321 | 94 // PPE 側で実行される TaskList |
109 | 95 list_top = mainTaskList; |
96 | |
321 | 97 // list_top->clear() とかの方がいいかもしれん。 |
475
4e0308d2ba73
BufferManager removed.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
373
diff
changeset
|
98 list_top = taskListImpl->clear_taskList(list_top); |
109 | 99 list = list_top; |
100 | |
499 | 101 // printf("active task queue length = %d\n",activeTaskQueue->length()); |
498
bce667ff20b9
double linked HTaskInfo/HTask
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
494
diff
changeset
|
102 while (HTaskPtr htask = activeTaskQueue->poll()) { |
373 | 103 task = &list->tasks[list->length++]; |
492 | 104 #if 0 |
491 | 105 task->command = htask->command; |
106 task->inData = htask->inData; | |
107 task->outData = htask->outData; | |
108 task->self = (unsigned int)htask; | |
492 | 109 // param は? |
491 | 110 #else |
499 | 111 // inData, outData を内蔵にしたので実は、結構でかくない? |
112 // 268 byte 程度だが... 不要な分(設定してない inData, outData, param | |
113 // とかもコピーしてるね。rbuf/wbuf の意味を変えてしまったわけか。 | |
491 | 114 memcpy(task, (Task*)htask, sizeof(Task)); |
115 #endif | |
109 | 116 |
117 | |
373 | 118 if (list->length >= TASK_MAX_SIZE) { |
475
4e0308d2ba73
BufferManager removed.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
373
diff
changeset
|
119 TaskListPtr newList = taskListImpl->create(); |
373 | 120 list_top = TaskListInfo::append(list_top, newList); |
121 list = newList; | |
122 } | |
109 | 123 |
499 | 124 // activeTaskQueue->free_(htask); ここで free しないで、 |
125 // mail を待つ | |
109 | 126 } |
127 | |
128 mainTaskList = list_top; | |
129 | |
130 return list_top; | |
131 } | |
132 | |
133 | |
134 void | |
475
4e0308d2ba73
BufferManager removed.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
373
diff
changeset
|
135 FifoTaskManagerImpl::run() |
109 | 136 { |
137 TaskListPtr list; | |
138 MailQueuePtr mail; | |
139 | |
140 list = get_runTaskList(); | |
141 | |
142 do { | |
373 | 143 // list を実行する |
144 mail = schedule(list); | |
109 | 145 |
373 | 146 // mail には、ppe scheduler からの mail がある |
147 mail_check(mail); | |
109 | 148 |
373 | 149 // 依存関係を満たしたものは実行可能キューへ |
150 wakeup_waitTask(); | |
151 list = get_runTaskList(); | |
109 | 152 } while (list); |
153 } | |
154 | |
155 /** | |
321 | 156 * @param [list] 実行タスクリスト |
157 * @return FifoScheduler からのメール | |
109 | 158 * |
321 | 159 * [Tasklist] -> [番兵] -> scheduler->run を抜ける |
109 | 160 */ |
161 MailQueuePtr | |
162 FifoTaskManagerImpl::schedule(TaskListPtr list) | |
163 { | |
164 MailQueuePtr list_mail; // task list | |
321 | 165 MailQueuePtr sentinel; // 番兵 |
109 | 166 MailQueuePtr in_mail_list = NULL; |
167 MailQueuePtr out_mail_list = NULL; | |
168 | |
321 | 169 // TaskList のアドレス |
373 | 170 list_mail = mailManager->create((unsigned int)list); |
109 | 171 in_mail_list = MailManager::append_mailQueue(in_mail_list, list_mail); |
172 | |
321 | 173 // EXIT_COMMAND (番兵的な意味で) |
109 | 174 sentinel = mailManager->create(MY_SPE_COMMAND_EXIT); |
175 in_mail_list = MailManager::append_mailQueue(in_mail_list, sentinel); | |
373 | 176 |
321 | 177 // scheduler は受け取ったメールを元に実行する |
109 | 178 scheduler->send_mailList(in_mail_list); |
179 scheduler->run(); | |
373 | 180 out_mail_list = scheduler->recv_mailList(); |
109 | 181 |
182 return out_mail_list; | |
183 } | |
184 | |
185 /** | |
321 | 186 * PPE Scheduler からのメールをチェックする |
109 | 187 * |
188 * @param [mail_list] | |
321 | 189 * PPE 側で動く Scheduler からのメールリスト |
190 * 終了した Task や、その他(今はまだ実装してないけど)の情報が入ってる | |
109 | 191 * |
373 | 192 * @return Scheduler が次に実行する Task List |
321 | 193 * NULL なら全てのタスクが実行終了したということ |
109 | 194 */ |
195 void | |
498
bce667ff20b9
double linked HTaskInfo/HTask
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
494
diff
changeset
|
196 FifoTaskManagerImpl::mail_check(MailQueuePtr mail_list, HTaskInfo *waitQueue) |
485 | 197 { |
198 waitTaskQueue = waitQueue; | |
199 mail_check(mail_list); | |
200 } | |
201 | |
202 void | |
109 | 203 FifoTaskManagerImpl::mail_check(MailQueuePtr mail_list) |
204 { | |
205 MailQueuePtr q = mail_list; | |
206 MailQueuePtr d; | |
207 unsigned int data; | |
208 | |
209 while (q) { | |
373 | 210 data = q->data; |
109 | 211 |
373 | 212 /** |
213 * MY_SPE_STATUS_READY: SPE が持ってた Task 全て終了 | |
214 * MY_SPE_NOP: 特に意味のないコマンド | |
215 * それ以外:終了したタスク(PPEにあるのでアドレス | |
216 * | |
217 * MY_SPE_NOP が 0 なので、 | |
218 * 下のように data > MY_SPE_NOP とかしています。 | |
219 * 一目でよくわからない書き方なんで、直したいところですが。。。 | |
220 */ | |
221 if (data == MY_SPE_STATUS_READY) { | |
222 __debug_ppe("mail_check(): Task List finish\n"); | |
223 } else if (data > MY_SPE_NOP) { | |
224 __debug_ppe("mail_check(): recv from 0x%x\n", data); | |
225 check_task_finish((HTaskPtr)data); | |
226 } | |
109 | 227 |
373 | 228 d = q; |
229 q = q->next; | |
109 | 230 |
373 | 231 mailManager->free(d); |
109 | 232 } |
3 | 233 } |
234 | |
109 | 235 void* |
538 | 236 FifoTaskManagerImpl::allocate(int size, int alignment) |
237 { | |
238 #ifdef __APPLE__ | |
239 return malloc(size); | |
240 #else | |
241 void *buff; | |
242 posix_memalign(&buff, alignment, size); | |
243 return buff; | |
244 #endif | |
245 } | |
246 | |
247 void* | |
109 | 248 FifoTaskManagerImpl::allocate(int size) |
249 { | |
538 | 250 #ifdef __APPLE__ |
109 | 251 return malloc(size); |
538 | 252 #else |
253 void *buff; | |
254 posix_memalign(&buff, DEFAULT_ALIGNMENT, size); | |
255 return buff; | |
256 #endif | |
109 | 257 } |
258 | |
373 | 259 Scheduler* |
260 FifoTaskManagerImpl::get_scheduler() { | |
261 return scheduler; | |
262 } | |
263 | |
109 | 264 /** |
475
4e0308d2ba73
BufferManager removed.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
373
diff
changeset
|
265 * # # # # # # # # |
109 | 266 * Abstract Factory Pattern |
475
4e0308d2ba73
BufferManager removed.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
373
diff
changeset
|
267 * # # # # # # # |
109 | 268 */ |
269 #ifdef __CERIUM_FIFO__ | |
373 | 270 TaskManagerImpl* |
3 | 271 create_impl(int num) |
272 { | |
273 return new FifoTaskManagerImpl(); | |
274 } | |
109 | 275 #endif // __CERIUM_FIFO__ |
475
4e0308d2ba73
BufferManager removed.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
373
diff
changeset
|
276 |