817
|
1 #include <stdlib.h>
|
|
2 #include "types.h"
|
|
3 #include "CpuThreads.h"
|
|
4 #include "MainScheduler.h"
|
|
5 #include "SysFunc.h"
|
|
6 #include "SchedNop.h"
|
|
7
|
|
8 SchedExternTask(ShowTime);
|
|
9 SchedExternTask(StartProfile);
|
|
10
|
|
11
|
|
12 CpuThreads::CpuThreads(int num, int start_id) : cpu_num(num), id_offset(start_id) {}
|
|
13
|
|
14 CpuThreads::~CpuThreads()
|
|
15 {
|
|
16 memaddr mail = (memaddr)MY_SPE_COMMAND_EXIT;
|
|
17
|
|
18 for (int i = 0; i < cpu_num; i++) {
|
|
19 send_mail(i, 1, &mail);
|
|
20 }
|
|
21
|
|
22 for (int i = 0; i < cpu_num; i++) {
|
|
23 pthread_join(threads[i], NULL);
|
|
24 }
|
|
25
|
|
26 delete [] threads;
|
|
27 delete [] args;
|
|
28 }
|
|
29
|
|
30 void *
|
|
31 CpuThreads::cpu_thread_run(void *args)
|
|
32 {
|
|
33 cpu_thread_arg_t *argt = (cpu_thread_arg_t *) args;
|
|
34 Scheduler *c_scheduler = argt->scheduler;
|
|
35
|
|
36 SchedRegister(ShowTime);
|
|
37 SchedRegister(StartProfile);
|
|
38
|
|
39 c_scheduler->init(argt->manager);
|
|
40 c_scheduler->id = (int)argt->cpuid;
|
|
41
|
|
42 c_scheduler->run(new SchedNop());
|
|
43 c_scheduler->finish();
|
|
44
|
|
45 return NULL;
|
|
46 }
|
|
47
|
|
48 void
|
|
49 CpuThreads::init()
|
|
50 {
|
|
51 threads = new pthread_t[cpu_num];
|
|
52 args = new cpu_thread_arg_t[cpu_num];
|
|
53
|
|
54 for (int i = 0; i < cpu_num; i++) {
|
|
55 args[i].cpuid = i + id_offset;
|
|
56 args[i].scheduler = new MainScheduler();
|
|
57 }
|
|
58
|
|
59 for (int i = 0; i < cpu_num; i++) {
|
|
60 pthread_create(&threads[i], NULL,
|
|
61 &cpu_thread_run, (void*)&args[i]);
|
|
62 }
|
|
63 }
|
|
64
|
|
65 /**
|
|
66 * このCPU からのメールを受信する。
|
|
67 *
|
|
68 * @param [cpuid] SPE ID
|
|
69 *
|
|
70 * @return Received 32-bit mailbox messages
|
|
71 * if ([ret] < 0) no data read
|
|
72 */
|
|
73 int
|
|
74 CpuThreads::get_mail(int cpuid, int count, memaddr *ret)
|
|
75 {
|
|
76 // need synchronization
|
|
77 *ret = args[cpuid-id_offset].scheduler->mail_read_from_host();
|
|
78 return 1;
|
|
79 }
|
|
80
|
|
81 int
|
|
82 CpuThreads::has_mail(int cpuid, int count, memaddr *ret)
|
|
83 {
|
|
84 // need synchronization
|
|
85 return args[cpuid-id_offset].scheduler->has_mail_from_host();
|
|
86 }
|
|
87
|
|
88 /**
|
|
89 * Inbound Mailbox
|
|
90 * メール送信 Front End -> CPU
|
|
91 *
|
|
92 * なるべく NONBLOCKING なんだけど、
|
|
93 * Inbound Mailbox キューに空きがないと送信できないので
|
|
94 * 送信する数だけ空いているか確認してから送る。空いて無い場合は待つ。
|
|
95 *
|
|
96 * 結局待つんだよな。しかも ALL_BLOCKING って実は busy wait だったりするし
|
|
97 *
|
|
98 * @param [cpuid] SPE ID
|
|
99 * @param [data] Send 32-bit mailbox messages
|
|
100 * @param [num] The number of messages
|
|
101 */
|
|
102 void
|
|
103 CpuThreads::send_mail(int cpuid, int num, memaddr *data)
|
|
104
|
|
105 {
|
|
106 // need synchronization
|
|
107 args[cpuid-id_offset].scheduler->mail_write_from_host(*data);
|
|
108 }
|
|
109
|
|
110
|
|
111 /* end */
|