comparison meta_connector/meta_connect_test.c @ 1:4a172166e6c3

add meta connector script
author Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp>
date Wed, 29 Jul 2015 00:13:12 +0900
parents
children
comparison
equal deleted inserted replaced
0:d66cc78ae824 1:4a172166e6c3
1 #include <stdlib.h>
2 #include <stdio.h>
3
4 #include "synchronizedQueueContext.h"
5
6 #include "allocate.h"
7 #include "origin_cs.h"
8
9 #ifdef CLANG
10 #define _CbC_retrun __return
11 #define _CbC_environment __environment
12 #endif
13
14 #define NUM 100
15
16 meta_connect(
17 meta_sender:sender
18 meta_receiver: receiver
19 )
20
21 extern __code initSynchronizedQueueContext(struct Context* context);
22
23 //__code code1(struct Context* context) {
24 // context->data[Allocate]->allocate.size = sizeof(struct Element);
25 // context->data[Allocate]->allocate.next = Code2;
26 // goto meta(context, Allocator);
27 //}
28
29 __code meta(struct Context* context, enum Code next) {
30 goto (context->code[next])(context);
31 }
32
33 //__code code2(struct Context* context) {
34 // context->data[Allocate]->allocate.after_put = Code3;
35 // context->data[context->dataNum] -> element.value = 1024;
36 // goto meta(context, Sender);
37 //}
38
39 __code code1(struct Context* context, struct Allocate* allocate) {
40 allocate->size = sizeof(long);
41 allocate->next = Code2;
42 goto meta(context, Allocator);
43 }
44
45 __code code1_stub(struct Context* context) {
46 goto code1(context, &context->data[Allocate]->allocate);
47 }
48
49 __code code2(struct Context* context, long* count) {
50 *count = 0;
51 goto meta(context, Code3);
52 }
53
54 __code code2_stub(struct Context* context) {
55 goto code2(context, &context->data[Counter]->count);
56 }
57
58 __code code3(struct Context* context, long* count, struct Allocate* allocate) {
59 long loop = *count;
60 if(loop == NUM) {
61 goto meta(context, ThreadExit);
62 }
63 allocate->size = sizeof(struct Element);
64 allocate->next = Code4;
65 goto meta(context, Allocator);
66 }
67
68 __code code3_stub(struct Context* context) {
69 goto code3(context, &context->data[Counter]->count, &context->data[Allocate]->allocate);
70 }
71
72 __code code4(struct Context* context, long* count, struct Allocate* allocate, struct Element* element) {
73 allocate->after_put = Code3;
74 element->value = (*count)++;
75 goto meta(context, Sender);
76 }
77
78 __code code4_stub(struct Context* context) {
79 goto code4(context, &context->data[Counter]->count, &context->data[Allocate]->allocate, &context->data[context->dataNum]->element);
80 }
81
82 __code meta_sender(struct Context* context, struct Queue* queue, enum Code next) {
83 // lock
84 pthread_mutex_lock(&queue->mutex);
85 goto (context->code[next])(context);
86 }
87
88 __code sender(struct Context* context, struct Queue* queue) {
89 // goto meta_sender(context, queue, Put);
90 // goto meta_sender : put(queue);
91 goto put(queue);
92 }
93
94 __code sender_stub(struct Context* context) {
95 goto sender(context, &context->data[Queue]->queue);
96 }
97
98 __code meta_put(struct Context* context, struct Queue* queue, enum Code next) {
99 // signal
100 pthread_cond_signal(&queue->cond);
101 // unlock
102 pthread_mutex_unlock(&queue->mutex);
103 goto (context->code[next])(context);
104 }
105
106 __code put(struct Context* context, struct Allocate* allocate, struct Queue* queue, struct Element* element) {
107 if(queue->first) {
108 queue->last->next = element;
109 } else {
110 queue->first = element;
111 }
112 queue->last = element;
113 element->next = 0;
114 queue->count++;
115 printf("Put %d\n\n", element->value);
116 goto meta_put(context, queue, allocate->after_put);
117 }
118
119 __code put_stub(struct Context* context) {
120 goto put(context, &context->data[Allocate]->allocate, &context->data[Queue]->queue, &context->data[context->dataNum]->element);
121 }
122
123 __code code5(struct Context* context, struct Allocate* allocate) {
124 allocate->size = sizeof(long);
125 allocate->next = Code6;
126 goto meta(context, Allocator);
127 }
128
129 __code code5_stub(struct Context* context) {
130 goto code5(context, &context->data[Allocate]->allocate);
131 }
132
133 __code code6(struct Context* context, long* count) {
134 *count = 0;
135 goto meta(context, Code7);
136 }
137
138 __code code6_stub(struct Context* context) {
139 goto code6(context, &context->data[Counter]->count);
140 }
141
142 __code code7(struct Context* context, long* count, struct Allocate* allocate) {
143 long loop = *count;
144 if(loop == NUM) {
145 goto meta(context, ThreadExit);
146 }
147 (*count)++;
148 allocate->after_get = Code7;
149 goto meta(context, Receiver);
150 }
151
152 __code code7_stub(struct Context* context) {
153 goto code7(context, &context->data[Counter]->count, &context->data[Allocate]->allocate);
154 }
155
156 __code meta_receiver(struct Context* context, struct Queue* queue, enum Code next) {
157 // lock
158 pthread_mutex_lock(&queue->mutex);
159 goto (context->code[next])(context);
160 }
161
162 __code receiver(struct Context* context, struct Queue* queue) {
163 // goto meta_receiver(context, queue, Get);
164 goto get(queue);
165 }
166
167 __code receiver_stub(struct Context* context) {
168 goto receiver(context, &context->data[Queue]->queue);
169 }
170 __code meta_get(struct Context* context, enum Code next) {
171 pthread_mutex_unlock(&context->data[Queue]->queue.mutex);
172 goto (context->code[next])(context);
173 }
174
175 __code get(struct Context* context, struct Allocate* allocate, struct Queue* queue, struct Element* element) {
176 // thread wait if queue is empty
177 while (queue->count == 0) {
178 pthread_cond_wait(&queue->cond, &queue->mutex);
179 }
180 printf(" Get %d\n\n", queue->first->value);
181 queue->first = (queue->first->next) ? queue->first->next : 0;
182 queue->count--;
183 goto meta_get(context, allocate->after_get);
184 }
185
186 __code get_stub(struct Context* context) {
187 goto get(context, &context->data[Allocate]->allocate, &context->data[Queue]->queue, &context->data[context->dataNum]->element);
188 }
189
190 __code thread_exit(struct Context* context) {
191 free(context->code);
192 free(context->data);
193 free(context->heapStart);
194 pthread_exit(0);
195 }
196
197 __code thread_exit_stub(struct Context* context) {
198 goto thread_exit(context);
199 }
200
201 void* thread_func(void* context) {
202 goto start_code((struct Context*)context, Code1);
203 return 0;
204 }
205
206 void* thread_func2(void* context) {
207 goto start_code((struct Context*)context, Code5);
208 return 0;
209 }
210
211 int main() {
212 struct Context* context1 = (struct Context*)malloc(sizeof(struct Context));
213 initSynchronizedQueueContext(context1);
214 struct Context* context2 = (struct Context*)malloc(sizeof(struct Context));
215 initSynchronizedQueueContext(context2);
216 struct Context* context3 = (struct Context*)malloc(sizeof(struct Context));
217 initSynchronizedQueueContext(context3);
218 struct Context* context4 = (struct Context*)malloc(sizeof(struct Context));
219 initSynchronizedQueueContext(context4);
220 context2->data[Queue] = context1->data[Queue];
221 context3->data[Queue] = context1->data[Queue];
222 context4->data[Queue] = context1->data[Queue];
223 pthread_t thread1, thread2, thread3, thread4;
224 pthread_create(&thread1, NULL, thread_func, (void *)context1);
225 pthread_create(&thread2, NULL, thread_func, (void *)context2);
226 pthread_create(&thread3, NULL, thread_func2, (void *)context3);
227 pthread_create(&thread4, NULL, thread_func2, (void *)context4);
228 pthread_join(thread1, NULL);
229 pthread_join(thread2, NULL);
230 pthread_join(thread3, NULL);
231 pthread_join(thread4, NULL);
232 }