Mercurial > hg > Members > Moririn
diff 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 |
line wrap: on
line diff
--- a/src/synchronizedQueue/synchronizedQueue.c Sat May 16 20:17:58 2015 +0900 +++ b/src/synchronizedQueue/synchronizedQueue.c Mon May 18 15:24:46 2015 +0900 @@ -11,7 +11,7 @@ #define _CbC_environment __environment #endif -#define NUM 50 +#define NUM 100 extern __code initSynchronizedQueueContext(struct Context* context); @@ -31,16 +31,6 @@ // goto meta(context, Sender); //} -__code meta_sender(struct Context* context, enum Code next) { - // lock - pthread_mutex_lock(&context->data[Queue]->queue.mutex); - goto (context->code[next])(context); -} - -__code sender(struct Context* context) { - goto meta(context, Put); -} - __code code1(struct Context* context) { context->data[Allocate]->allocate.size = sizeof(long); context->data[Allocate]->allocate.next = Code2; @@ -55,7 +45,7 @@ __code code3(struct Context* context) { long loop = context->data[Counter]->count; if(loop == NUM) { - goto meta(context, Exit); + goto meta(context, ThreadExit); } context->data[Allocate]->allocate.size = sizeof(struct Element); context->data[Allocate]->allocate.next = Code4; @@ -68,6 +58,16 @@ goto meta(context, Sender); } +__code meta_sender(struct Context* context, enum Code next) { + // lock + pthread_mutex_lock(&context->data[Queue]->queue.mutex); + goto (context->code[next])(context); +} + +__code sender(struct Context* context) { + goto meta_sender(context, Put); +} + __code meta_put(struct Context* context, enum Code next) { if(context->data[Queue]->queue.first) { context->data[Queue]->queue.last->element.next = context->data[context->dataNum]; @@ -103,7 +103,7 @@ __code code7(struct Context* context) { long loop = context->data[Counter]->count; if(loop == NUM) { - goto meta(context, Exit); + goto meta(context, ThreadExit); } context->data[Counter]->count++; context->data[Allocate]->allocate.after_get = Code7; @@ -117,31 +117,44 @@ } __code receiver(struct Context* context) { - goto meta(context, Get); + goto meta_receiver(context, Get); } __code meta_get(struct Context* context, enum Code next) { - if (context->data[Queue]->queue.first == context->data[Queue]->queue.last) { + if (context->data[Queue]->queue.count == 0) { printf("queue is empty\n"); + pthread_mutex_unlock(&context->data[Queue]->queue.mutex); goto meta(context, Receiver); } else { printf("Get %d\n\n", context->data[Queue]->queue.first->element.value); context->data[Queue]->queue.first = (context->data[Queue]->queue.first->element.next) ? context->data[Queue]->queue.first->element.next : 0; context->data[Queue]->queue.count--; + pthread_mutex_unlock(&context->data[Queue]->queue.mutex); + goto (context->code[next])(context); } - pthread_mutex_unlock(&context->data[Queue]->queue.mutex); - goto (context->code[next])(context); } __code get(struct Context* context) { goto meta_get(context, context->data[Allocate]->allocate.after_get); } +__code thread_exit(struct Context* context) { + free(context->code); + free(context->data); + free(context->heap_start); + pthread_exit(NULL); +} + void* thread_func(void* context) { goto start_code((struct Context*)context, Code1); return NULL; } +void* thread_func2(void* context) { + goto start_code((struct Context*)context, Code5); + return NULL; +} + int main() { struct Context* context1 = (struct Context*)malloc(sizeof(struct Context)); initSynchronizedQueueContext(context1); @@ -149,10 +162,18 @@ initSynchronizedQueueContext(context2); struct Context* context3 = (struct Context*)malloc(sizeof(struct Context)); initSynchronizedQueueContext(context3); + struct Context* context4 = (struct Context*)malloc(sizeof(struct Context)); + initSynchronizedQueueContext(context4); context2->data[Queue] = context1->data[Queue]; context3->data[Queue] = context1->data[Queue]; - pthread_t thread1, thread2; - pthread_create(&thread1, NULL, thread_func, (void *)context2); - pthread_create(&thread2, NULL, thread_func, (void *)context3); - goto start_code(context1, Code5); + context4->data[Queue] = context1->data[Queue]; + pthread_t thread1, thread2, thread3, thread4; + pthread_create(&thread1, NULL, thread_func, (void *)context1); + pthread_create(&thread2, NULL, thread_func, (void *)context2); + pthread_create(&thread3, NULL, thread_func2, (void *)context3); + pthread_create(&thread4, NULL, thread_func2, (void *)context4); + pthread_join(thread1, NULL); + pthread_join(thread2, NULL); + pthread_join(thread3, NULL); + pthread_join(thread4, NULL); }