Mercurial > hg > Members > Moririn
changeset 52:83ee9c75115a
Add pthread_cond_wait & pthread_cond_signal to synchronizedQueue.c
author | Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 02 Jun 2015 14:17:07 +0900 |
parents | 4302e2fb7019 |
children | 399ed10d1760 |
files | src/synchronizedQueue/synchronizedQueue.c src/synchronizedQueue/synchronizedQueueContext.c src/synchronizedQueue/synchronizedQueueContext.h src/synchronizedQueue/synchronizedQueueForSemContext.c |
diffstat | 4 files changed, 26 insertions(+), 24 deletions(-) [+] |
line wrap: on
line diff
--- 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) {
--- 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; }
--- 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;
--- 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;