comparison src/synchronizedQueue/synchronizedQueue.c @ 40:46917f503bce

Add thread_join to synchronizedQueue
author Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
date Mon, 18 May 2015 15:24:46 +0900
parents 754c90e96e3d
children 4a16cbaab802
comparison
equal deleted inserted replaced
39:754c90e96e3d 40:46917f503bce
9 #ifdef CLANG 9 #ifdef CLANG
10 #define _CbC_retrun __return 10 #define _CbC_retrun __return
11 #define _CbC_environment __environment 11 #define _CbC_environment __environment
12 #endif 12 #endif
13 13
14 #define NUM 50 14 #define NUM 100
15 15
16 extern __code initSynchronizedQueueContext(struct Context* context); 16 extern __code initSynchronizedQueueContext(struct Context* context);
17 17
18 //__code code1(struct Context* context) { 18 //__code code1(struct Context* context) {
19 // context->data[Allocate]->allocate.size = sizeof(struct Element); 19 // context->data[Allocate]->allocate.size = sizeof(struct Element);
29 // context->data[Allocate]->allocate.after_put = Code3; 29 // context->data[Allocate]->allocate.after_put = Code3;
30 // context->data[context->dataNum] -> element.value = 1024; 30 // context->data[context->dataNum] -> element.value = 1024;
31 // goto meta(context, Sender); 31 // goto meta(context, Sender);
32 //} 32 //}
33 33
34 __code meta_sender(struct Context* context, enum Code next) {
35 // lock
36 pthread_mutex_lock(&context->data[Queue]->queue.mutex);
37 goto (context->code[next])(context);
38 }
39
40 __code sender(struct Context* context) {
41 goto meta(context, Put);
42 }
43
44 __code code1(struct Context* context) { 34 __code code1(struct Context* context) {
45 context->data[Allocate]->allocate.size = sizeof(long); 35 context->data[Allocate]->allocate.size = sizeof(long);
46 context->data[Allocate]->allocate.next = Code2; 36 context->data[Allocate]->allocate.next = Code2;
47 goto meta(context, Allocator); 37 goto meta(context, Allocator);
48 } 38 }
53 } 43 }
54 44
55 __code code3(struct Context* context) { 45 __code code3(struct Context* context) {
56 long loop = context->data[Counter]->count; 46 long loop = context->data[Counter]->count;
57 if(loop == NUM) { 47 if(loop == NUM) {
58 goto meta(context, Exit); 48 goto meta(context, ThreadExit);
59 } 49 }
60 context->data[Allocate]->allocate.size = sizeof(struct Element); 50 context->data[Allocate]->allocate.size = sizeof(struct Element);
61 context->data[Allocate]->allocate.next = Code4; 51 context->data[Allocate]->allocate.next = Code4;
62 goto meta(context, Allocator); 52 goto meta(context, Allocator);
63 } 53 }
64 54
65 __code code4(struct Context* context) { 55 __code code4(struct Context* context) {
66 context->data[Allocate]->allocate.after_put = Code3; 56 context->data[Allocate]->allocate.after_put = Code3;
67 context->data[context->dataNum] -> element.value = context->data[Counter]->count++; 57 context->data[context->dataNum] -> element.value = context->data[Counter]->count++;
68 goto meta(context, Sender); 58 goto meta(context, Sender);
59 }
60
61 __code meta_sender(struct Context* context, enum Code next) {
62 // lock
63 pthread_mutex_lock(&context->data[Queue]->queue.mutex);
64 goto (context->code[next])(context);
65 }
66
67 __code sender(struct Context* context) {
68 goto meta_sender(context, Put);
69 } 69 }
70 70
71 __code meta_put(struct Context* context, enum Code next) { 71 __code meta_put(struct Context* context, enum Code next) {
72 if(context->data[Queue]->queue.first) { 72 if(context->data[Queue]->queue.first) {
73 context->data[Queue]->queue.last->element.next = context->data[context->dataNum]; 73 context->data[Queue]->queue.last->element.next = context->data[context->dataNum];
101 } 101 }
102 102
103 __code code7(struct Context* context) { 103 __code code7(struct Context* context) {
104 long loop = context->data[Counter]->count; 104 long loop = context->data[Counter]->count;
105 if(loop == NUM) { 105 if(loop == NUM) {
106 goto meta(context, Exit); 106 goto meta(context, ThreadExit);
107 } 107 }
108 context->data[Counter]->count++; 108 context->data[Counter]->count++;
109 context->data[Allocate]->allocate.after_get = Code7; 109 context->data[Allocate]->allocate.after_get = Code7;
110 goto meta(context, Receiver); 110 goto meta(context, Receiver);
111 } 111 }
115 pthread_mutex_lock(&context->data[Queue]->queue.mutex); 115 pthread_mutex_lock(&context->data[Queue]->queue.mutex);
116 goto (context->code[next])(context); 116 goto (context->code[next])(context);
117 } 117 }
118 118
119 __code receiver(struct Context* context) { 119 __code receiver(struct Context* context) {
120 goto meta(context, Get); 120 goto meta_receiver(context, Get);
121 } 121 }
122 122
123 __code meta_get(struct Context* context, enum Code next) { 123 __code meta_get(struct Context* context, enum Code next) {
124 if (context->data[Queue]->queue.first == context->data[Queue]->queue.last) { 124 if (context->data[Queue]->queue.count == 0) {
125 printf("queue is empty\n"); 125 printf("queue is empty\n");
126 pthread_mutex_unlock(&context->data[Queue]->queue.mutex);
126 goto meta(context, Receiver); 127 goto meta(context, Receiver);
127 } else { 128 } else {
128 printf("Get %d\n\n", context->data[Queue]->queue.first->element.value); 129 printf("Get %d\n\n", context->data[Queue]->queue.first->element.value);
129 context->data[Queue]->queue.first = (context->data[Queue]->queue.first->element.next) ? context->data[Queue]->queue.first->element.next : 0; 130 context->data[Queue]->queue.first = (context->data[Queue]->queue.first->element.next) ? context->data[Queue]->queue.first->element.next : 0;
130 context->data[Queue]->queue.count--; 131 context->data[Queue]->queue.count--;
132 pthread_mutex_unlock(&context->data[Queue]->queue.mutex);
133 goto (context->code[next])(context);
131 } 134 }
132 pthread_mutex_unlock(&context->data[Queue]->queue.mutex);
133 goto (context->code[next])(context);
134 } 135 }
135 136
136 __code get(struct Context* context) { 137 __code get(struct Context* context) {
137 goto meta_get(context, context->data[Allocate]->allocate.after_get); 138 goto meta_get(context, context->data[Allocate]->allocate.after_get);
138 } 139 }
139 140
141 __code thread_exit(struct Context* context) {
142 free(context->code);
143 free(context->data);
144 free(context->heap_start);
145 pthread_exit(NULL);
146 }
147
140 void* thread_func(void* context) { 148 void* thread_func(void* context) {
141 goto start_code((struct Context*)context, Code1); 149 goto start_code((struct Context*)context, Code1);
150 return NULL;
151 }
152
153 void* thread_func2(void* context) {
154 goto start_code((struct Context*)context, Code5);
142 return NULL; 155 return NULL;
143 } 156 }
144 157
145 int main() { 158 int main() {
146 struct Context* context1 = (struct Context*)malloc(sizeof(struct Context)); 159 struct Context* context1 = (struct Context*)malloc(sizeof(struct Context));
147 initSynchronizedQueueContext(context1); 160 initSynchronizedQueueContext(context1);
148 struct Context* context2 = (struct Context*)malloc(sizeof(struct Context)); 161 struct Context* context2 = (struct Context*)malloc(sizeof(struct Context));
149 initSynchronizedQueueContext(context2); 162 initSynchronizedQueueContext(context2);
150 struct Context* context3 = (struct Context*)malloc(sizeof(struct Context)); 163 struct Context* context3 = (struct Context*)malloc(sizeof(struct Context));
151 initSynchronizedQueueContext(context3); 164 initSynchronizedQueueContext(context3);
165 struct Context* context4 = (struct Context*)malloc(sizeof(struct Context));
166 initSynchronizedQueueContext(context4);
152 context2->data[Queue] = context1->data[Queue]; 167 context2->data[Queue] = context1->data[Queue];
153 context3->data[Queue] = context1->data[Queue]; 168 context3->data[Queue] = context1->data[Queue];
154 pthread_t thread1, thread2; 169 context4->data[Queue] = context1->data[Queue];
155 pthread_create(&thread1, NULL, thread_func, (void *)context2); 170 pthread_t thread1, thread2, thread3, thread4;
156 pthread_create(&thread2, NULL, thread_func, (void *)context3); 171 pthread_create(&thread1, NULL, thread_func, (void *)context1);
157 goto start_code(context1, Code5); 172 pthread_create(&thread2, NULL, thread_func, (void *)context2);
173 pthread_create(&thread3, NULL, thread_func2, (void *)context3);
174 pthread_create(&thread4, NULL, thread_func2, (void *)context4);
175 pthread_join(thread1, NULL);
176 pthread_join(thread2, NULL);
177 pthread_join(thread3, NULL);
178 pthread_join(thread4, NULL);
158 } 179 }