76
|
1 #include "types.h"
|
65
|
2 #include "SpeThreads.h"
|
|
3
|
|
4 SpeThreads::SpeThreads(int num) : spe_num(num) {}
|
|
5
|
76
|
6 SpeThreads::~SpeThreads(void)
|
|
7 {
|
|
8 unsigned int mail = MY_SPE_COMMAND_EXIT;
|
|
9 int ret;
|
|
10
|
|
11 for (int i = 0; i < spe_num; i++) {
|
|
12 send_mail(i, &mail);
|
|
13 }
|
|
14
|
|
15 printf("****** SpeThreads 1 ********\n");
|
|
16
|
|
17 for (int i = 0; i < spe_num; i++) {
|
|
18 pthread_join(threads[i], NULL);
|
|
19 ret = spe_context_destroy(spe_ctx[i]);
|
|
20 if (ret) {
|
|
21 perror("[~SpeThreads] spe_context_destroy");
|
|
22 }
|
|
23 }
|
|
24
|
|
25 printf("****** SpeThreads 2 ********\n");
|
|
26
|
|
27 spe_image_close(spe_handle);
|
|
28
|
|
29 delete [] spe_ctx;
|
|
30 delete [] threads;
|
|
31 delete [] args;
|
|
32
|
|
33
|
|
34 printf("****** SpeThreads 3 ********\n");
|
|
35 }
|
|
36
|
65
|
37 void*
|
|
38 SpeThreads::spe_thread_run(void *arg)
|
|
39 {
|
|
40 unsigned int entry = SPE_DEFAULT_ENTRY;
|
|
41 spe_context_ptr_t ctx = (spe_context_ptr_t)arg;
|
|
42
|
|
43 spe_context_run(ctx, &entry, 0, NULL, NULL, NULL);
|
|
44
|
|
45 pthread_exit(NULL);
|
|
46 }
|
|
47
|
|
48 void*
|
|
49 SpeThreads::frontend_thread_run(void *arg)
|
|
50 {
|
|
51 pthread_t thread;
|
|
52 thread_arg_t *arg_t = (thread_arg_t *)arg;
|
|
53
|
|
54 pthread_create(&thread, NULL, &spe_thread_run, (void*)arg_t->ctx);
|
|
55
|
76
|
56 // mail read の blocking ができれば
|
|
57 // ここで呼んだ方が早い。
|
|
58
|
65
|
59 pthread_exit(NULL);
|
|
60 }
|
|
61
|
|
62 void
|
|
63 SpeThreads::init(void)
|
|
64 {
|
|
65 int i;
|
|
66
|
|
67 spe_handle = spe_image_open(SPE_ELF);
|
|
68
|
|
69 spe_ctx = new spe_context_ptr_t[spe_num];
|
|
70 threads = new pthread_t[spe_num];
|
|
71 args = new thread_arg_t[spe_num];
|
|
72
|
|
73 for (i = 0; i < spe_num; i++) {
|
|
74 args[i].speid = i;
|
|
75 spe_ctx[i] = spe_context_create(0, NULL);
|
|
76 spe_program_load(spe_ctx[i], spe_handle);
|
|
77 args[i].ctx = spe_ctx[i];
|
|
78 }
|
|
79
|
|
80 for (i = 0; i < spe_num; i++) {
|
|
81 pthread_create(&threads[i], NULL,
|
|
82 &frontend_thread_run, (void*)&args[i]);
|
|
83 }
|
|
84 }
|
|
85
|
|
86 int
|
|
87 SpeThreads::get_mail(int speid)
|
|
88 {
|
|
89 unsigned int ret;
|
|
90
|
|
91 if (spe_out_mbox_read(spe_ctx[speid], &ret, 1) > 0) {
|
|
92 return (int)ret;
|
|
93 } else {
|
|
94 return -1;
|
|
95 }
|
|
96 }
|
66
|
97
|
|
98 void
|
|
99 SpeThreads::send_mail(int speid, unsigned int *data)
|
|
100 {
|
|
101 spe_in_mbox_write(spe_ctx[speid], data, 1, SPE_MBOX_ANY_NONBLOCKING);
|
|
102 }
|