annotate TaskManager/Cell/SpeThreads.cc @ 76:5a1a5f4c28fd

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