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