Mercurial > hg > GearsTemplate
changeset 51:4302e2fb7019
merge
author | Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp> |
---|---|
date | Mon, 25 May 2015 01:53:02 +0900 |
parents | d191faf19961 (current diff) bb73a0ca8671 (diff) |
children | 83ee9c75115a |
files | |
diffstat | 4 files changed, 42 insertions(+), 23 deletions(-) [+] |
line wrap: on
line diff
--- a/src/synchronizedQueue/synchronizedQueue.c Mon May 25 01:52:45 2015 +0900 +++ b/src/synchronizedQueue/synchronizedQueue.c Mon May 25 01:53:02 2015 +0900 @@ -126,7 +126,7 @@ 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); + 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);
--- a/src/synchronizedQueue/synchronizedQueueContext.h Mon May 25 01:52:45 2015 +0900 +++ b/src/synchronizedQueue/synchronizedQueueContext.h Mon May 25 01:53:02 2015 +0900 @@ -54,5 +54,6 @@ enum Code next; enum Code after_put; enum Code after_get; + enum Code after_fail; } allocate; };
--- a/src/synchronizedQueue/synchronizedQueueForCas.c Mon May 25 01:52:45 2015 +0900 +++ b/src/synchronizedQueue/synchronizedQueueForCas.c Mon May 25 01:53:02 2015 +0900 @@ -53,7 +53,8 @@ } __code code4(struct Context* context) { - context->data[Allocate]->allocate.after_put = Code3; + context->data[Allocate]->allocate.after_put = Code3; + context->data[Allocate]->allocate.after_fail = Code3; context->data[context->dataNum] -> element.value = context->data[Counter]->count++; goto meta(context, Sender); } @@ -67,19 +68,27 @@ last_ds = context->data[Queue]->queue.last; new_ds = context->data[context->dataNum]; new_ds->element.next = 0; - if(__sync_bool_compare_and_swap(&context->data[Queue]->queue.last, last_ds, new_ds)) { - if(context->data[Queue]->queue.first) { - last_ds->element.next = new_ds; + int count = 0; + while(!__sync_bool_compare_and_swap(&context->data[Queue]->queue.last, last_ds, new_ds)) { + if(count < 1000) { + last_ds = context->data[Queue]->queue.last; + new_ds = context->data[context->dataNum]; + new_ds->element.next = 0; + count++; } else { - context->data[Queue]->queue.first = new_ds; + // error handle + goto meta(context, context->data[Allocate]->allocate.after_fail); } - printf("Put %d\n\n", context->data[Queue]->queue.last->element.value); - context->data[Queue]->queue.count++; - goto (context->code[next])(context); + } + + if(context->data[Queue]->queue.first) { + last_ds->element.next = new_ds; } else { - printf("Fail\n"); - goto meta(context, Sender); + context->data[Queue]->queue.first = new_ds; } + printf("Put %d\n\n", context->data[Queue]->queue.last->element.value); + context->data[Queue]->queue.count++; + goto (context->code[next])(context); } __code put(struct Context* context) { @@ -103,7 +112,8 @@ goto meta(context, ThreadExit); } context->data[Counter]->count++; - context->data[Allocate]->allocate.after_get = Code7; + context->data[Allocate]->allocate.after_get = Code7; + context->data[Allocate]->allocate.after_fail = Code7; goto meta(context, Receiver); } @@ -115,18 +125,26 @@ union Data *first_ds, *new_ds; first_ds = context->data[Queue]->queue.first; new_ds = first_ds? first_ds->element.next : 0; - if(__sync_bool_compare_and_swap(&context->data[Queue]->queue.first, first_ds, new_ds)) { - if (first_ds == new_ds) { - printf("queue is empty\n"); - goto meta(context, Receiver); + int count = 0; + while(!__sync_bool_compare_and_swap(&context->data[Queue]->queue.first, first_ds, new_ds)) { + if(count < 1000) { + first_ds = context->data[Queue]->queue.first; + new_ds = first_ds? first_ds->element.next : 0; + count++; } else { - printf("Get %d\n\n", first_ds->element.value); - context->data[Queue]->queue.count--; - goto (context->code[next])(context); + // error handle + goto meta(context, context->data[Allocate]->allocate.after_fail); } + } + + // success CAS + if (first_ds == new_ds) { + printf("queue is empty\n"); + goto meta(context, Get); } else { - printf("Fail\n"); - goto meta(context, Receiver); + printf(" Get %d\n\n", first_ds->element.value); + context->data[Queue]->queue.count--; + goto (context->code[next])(context); } }
--- a/src/synchronizedQueue/synchronizedQueueForSem.c Mon May 25 01:52:45 2015 +0900 +++ b/src/synchronizedQueue/synchronizedQueueForSem.c Mon May 25 01:53:02 2015 +0900 @@ -11,7 +11,7 @@ #define _CbC_environment __environment #endif -#define NUM 10 +#define NUM 100 extern __code initSynchronizedQueueContext(struct Context* context); @@ -137,7 +137,7 @@ context->data[Queue]->queue.queue_count->value--; pthread_mutex_unlock(&context->data[Queue]->queue.queue_count->mutex); - printf("Get %d\n\n", context->data[Queue]->queue.first->element.value); + 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; // sem_v