# HG changeset patch # User Tatsuki IHA # Date 1433222227 -32400 # Node ID 83ee9c75115a0688b771918ceb5a8e88c7714718 # Parent 4302e2fb701933ec5a4af99e2f3fa18988509ba1 Add pthread_cond_wait & pthread_cond_signal to synchronizedQueue.c diff -r 4302e2fb7019 -r 83ee9c75115a src/synchronizedQueue/synchronizedQueue.c --- a/src/synchronizedQueue/synchronizedQueue.c Mon May 25 01:53:02 2015 +0900 +++ b/src/synchronizedQueue/synchronizedQueue.c Tue Jun 02 14:17:07 2015 +0900 @@ -80,6 +80,8 @@ context->data[Queue]->queue.count++; printf("Put %d\n\n", context->data[Queue]->queue.last->element.value); + // signal + pthread_cond_signal(&context->data[Queue]->queue.cond); // unlock pthread_mutex_unlock(&context->data[Queue]->queue.mutex); goto (context->code[next])(context); @@ -121,17 +123,15 @@ } __code meta_get(struct Context* context, enum Code next) { - if (context->data[Queue]->queue.count == 0) { - printf("queue is empty\n"); - pthread_mutex_unlock(&context->data[Queue]->queue.mutex); - goto (context->code[next])(context); - } 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); + // thread wait if queue is empty + while (context->data[Queue]->queue.count == 0) { + pthread_cond_wait(&context->data[Queue]->queue.cond, &context->data[Queue]->queue.mutex); } + 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); } __code get(struct Context* context) { diff -r 4302e2fb7019 -r 83ee9c75115a src/synchronizedQueue/synchronizedQueueContext.c --- a/src/synchronizedQueue/synchronizedQueueContext.c Mon May 25 01:53:02 2015 +0900 +++ b/src/synchronizedQueue/synchronizedQueueContext.c Tue Jun 02 14:17:07 2015 +0900 @@ -49,6 +49,7 @@ context->heap += sizeof(struct Queue); context->data[Queue]->queue.first = 0; pthread_mutex_init(&context->data[Queue]->queue.mutex, NULL); + pthread_cond_init(&context->data[Queue]->queue.cond, NULL); context->dataNum = Queue; } diff -r 4302e2fb7019 -r 83ee9c75115a src/synchronizedQueue/synchronizedQueueContext.h --- a/src/synchronizedQueue/synchronizedQueueContext.h Mon May 25 01:53:02 2015 +0900 +++ b/src/synchronizedQueue/synchronizedQueueContext.h Tue Jun 02 14:17:07 2015 +0900 @@ -44,6 +44,7 @@ union Data* last; int count; pthread_mutex_t mutex; + pthread_cond_t cond; } queue; struct Element { int value; diff -r 4302e2fb7019 -r 83ee9c75115a src/synchronizedQueue/synchronizedQueueForSemContext.c --- a/src/synchronizedQueue/synchronizedQueueForSemContext.c Mon May 25 01:53:02 2015 +0900 +++ b/src/synchronizedQueue/synchronizedQueueForSemContext.c Tue Jun 02 14:17:07 2015 +0900 @@ -24,20 +24,20 @@ context->data = malloc(sizeof(union Data*)*ALLOCATE_SIZE); context->heap_start = malloc(context->dataSize); - context->codeNum = Exit; - context->code[Code1] = code1; - context->code[Code2] = code2; - context->code[Code3] = code3; - context->code[Code4] = code4; - context->code[Code5] = code5; - context->code[Code6] = code6; - context->code[Code7] = code7; - context->code[Allocator] = allocate; - context->code[Sender] = sender; - context->code[Put] = put; - context->code[Receiver] = receiver; - context->code[Get] = get; - context->code[Exit] = exit_code; + context->codeNum = Exit; + context->code[Code1] = code1; + context->code[Code2] = code2; + context->code[Code3] = code3; + context->code[Code4] = code4; + context->code[Code5] = code5; + context->code[Code6] = code6; + context->code[Code7] = code7; + context->code[Allocator] = allocate; + context->code[Sender] = sender; + context->code[Put] = put; + context->code[Receiver] = receiver; + context->code[Get] = get; + context->code[Exit] = exit_code; context->code[ThreadExit] = thread_exit; context->heap = context->heap_start;