Mercurial > hg > Members > innparusu > Gears
changeset 63:2a40d697bf4e
Delete while loop for cas
author | Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp> |
---|---|
date | Mon, 13 Jul 2015 23:20:31 +0900 |
parents | fb2e67dfa5ef |
children | 89d760486188 |
files | doc/Cerium_on_Gears.mm src/synchronizedQueue/CMakeLists.txt src/synchronizedQueue/synchronizedQueue.c src/synchronizedQueue/synchronizedQueueContext.c src/synchronizedQueue/synchronizedQueueContext.h src/synchronizedQueue/synchronizedQueueForCas.c |
diffstat | 6 files changed, 175 insertions(+), 92 deletions(-) [+] |
line wrap: on
line diff
--- a/doc/Cerium_on_Gears.mm Fri Jun 26 18:45:02 2015 +0900 +++ b/doc/Cerium_on_Gears.mm Mon Jul 13 23:20:31 2015 +0900 @@ -61,5 +61,9 @@ <node CREATED="1435310562447" ID="ID_1893745957" MODIFIED="1435310566406" TEXT="sort"/> <node CREATED="1435310566919" ID="ID_370953203" MODIFIED="1435310568718" TEXT="word count"/> </node> +<node CREATED="1435655123173" ID="ID_476222881" MODIFIED="1435655128583" POSITION="right" TEXT="active task list"/> +<node CREATED="1435655131078" ID="ID_1089114952" MODIFIED="1435655136126" POSITION="left" TEXT="wait task list"> +<node CREATED="1435655137399" ID="ID_459495369" MODIFIED="1435655144883" TEXT="wait fo"/> +</node> </node> </map>
--- a/src/synchronizedQueue/CMakeLists.txt Fri Jun 26 18:45:02 2015 +0900 +++ b/src/synchronizedQueue/CMakeLists.txt Mon Jul 13 23:20:31 2015 +0900 @@ -1,15 +1,15 @@ cmake_minimum_required(VERSION 2.8) -add_executable(synchronizedQueue - synchronizedQueue.c - synchronizedQueueContext.c -) - add_executable(synchronizedQueueForCas synchronizedQueueForCas.c synchronizedQueueContext.c ) +#add_executable(synchronizedQueue +# synchronizedQueue.c +# synchronizedQueueContext.c +#) + add_executable(synchronizedQueueForSem synchronizedQueueForSem.c synchronizedQueueForSemContext.c
--- a/src/synchronizedQueue/synchronizedQueue.c Fri Jun 26 18:45:02 2015 +0900 +++ b/src/synchronizedQueue/synchronizedQueue.c Mon Jul 13 23:20:31 2015 +0900 @@ -182,7 +182,7 @@ __code thread_exit(struct Context* context) { free(context->code); free(context->data); - free(context->heap_start); + free(context->heapStart); pthread_exit(0); }
--- a/src/synchronizedQueue/synchronizedQueueContext.c Fri Jun 26 18:45:02 2015 +0900 +++ b/src/synchronizedQueue/synchronizedQueueContext.c Mon Jul 13 23:20:31 2015 +0900 @@ -9,12 +9,18 @@ extern __code code5_stub(struct Context*); extern __code code6_stub(struct Context*); extern __code code7_stub(struct Context*); +extern __code code8_stub(struct Context*); +extern __code code9_stub(struct Context*); +extern __code code10_stub(struct Context*); +extern __code code11_stub(struct Context*); extern __code meta(struct Context*); extern __code allocate(struct Context*); extern __code sender_stub(struct Context*); extern __code put_stub(struct Context*); +extern __code continue_put_stub(struct Context*); extern __code receiver_stub(struct Context*); extern __code get_stub(struct Context*); +extern __code continue_get_stub(struct Context*); extern __code exit_code(struct Context*); extern __code thread_exit_stub(struct Context*); @@ -22,25 +28,31 @@ context->dataSize = sizeof(union Data)*ALLOCATE_SIZE; context->code = malloc(sizeof(__code*)*ALLOCATE_SIZE); context->data = malloc(sizeof(union Data*)*ALLOCATE_SIZE); - context->heap_start = malloc(context->dataSize); + context->heapStart = malloc(context->dataSize); - context->codeNum = Exit; - context->code[Allocator] = allocate; - context->code[Code1] = code1_stub; - context->code[Code2] = code2_stub; - context->code[Code3] = code3_stub; - context->code[Code4] = code4_stub; - context->code[Code5] = code5_stub; - context->code[Code6] = code6_stub; - context->code[Code7] = code7_stub; - context->code[Sender] = sender_stub; - context->code[Put] = put_stub; - context->code[Receiver] = receiver_stub; - context->code[Get] = get_stub; - context->code[Exit] = exit_code; - context->code[ThreadExit] = thread_exit_stub; + context->codeNum = Exit; + context->code[Allocator] = allocate; + context->code[Code1] = code1_stub; + context->code[Code2] = code2_stub; + context->code[Code3] = code3_stub; + context->code[Code4] = code4_stub; + context->code[Code5] = code5_stub; + context->code[Code6] = code6_stub; + context->code[Code7] = code7_stub; + context->code[Code8] = code8_stub; + context->code[Code9] = code9_stub; + context->code[Code10] = code10_stub; + context->code[Code11] = code11_stub; + context->code[Sender] = sender_stub; + context->code[Put] = put_stub; + context->code[ContinuePut] = continue_put_stub; + context->code[Receiver] = receiver_stub; + context->code[Get] = get_stub; + context->code[ContinueGet] = continue_get_stub; + context->code[ThreadExit] = thread_exit_stub; + context->code[Exit] = exit_code; - context->heap = context->heap_start; + context->heap = context->heapStart; context->data[Allocate] = context->heap; context->heap += sizeof(struct Allocate);
--- a/src/synchronizedQueue/synchronizedQueueContext.h Fri Jun 26 18:45:02 2015 +0900 +++ b/src/synchronizedQueue/synchronizedQueueContext.h Mon Jul 13 23:20:31 2015 +0900 @@ -11,25 +11,32 @@ Code5, Code6, Code7, + Code8, + Code9, + Code10, + Code11, Allocator, Sender, Put, + ContinuePut, Receiver, Get, + ContinueGet, + ThreadExit, Exit, - ThreadExit, }; enum UniqueData { Allocate, Queue, Counter, + TimeOut, }; struct Context { int codeNum; __code (**code) (struct Context *); - void* heap_start; + void* heapStart; void* heap; long dataSize; int dataNum; @@ -39,6 +46,7 @@ union Data { long count; + long timeOut; struct Queue { struct Element* first; struct Element* last;
--- a/src/synchronizedQueue/synchronizedQueueForCas.c Fri Jun 26 18:45:02 2015 +0900 +++ b/src/synchronizedQueue/synchronizedQueueForCas.c Mon Jul 13 23:20:31 2015 +0900 @@ -3,7 +3,7 @@ #include "synchronizedQueueContext.h" -#include "allocate.h" +#include "./allocate.h" #include "origin_cs.h" #ifdef CLANG @@ -50,30 +50,48 @@ goto code2(context, &context->data[Counter]->count); } - -__code code3(struct Context* context, long* count, struct Allocate* allocate) { - long loop = *count; - if(loop == NUM) { - goto meta(context, ThreadExit); - } - allocate->size = sizeof(struct Element); +__code code3(struct Context* context, struct Allocate* allocate) { + allocate->size = sizeof(long); allocate->next = Code4; goto meta(context, Allocator); } __code code3_stub(struct Context* context) { - goto code3(context, &context->data[Counter]->count, &context->data[Allocate]->allocate); + goto code3(context, &context->data[Allocate]->allocate); +} + +__code code4(struct Context* context, long* timeOut) { + *timeOut = 0; + goto meta(context, Code5); +} + +__code code4_stub(struct Context* context) { + goto code4(context, &context->data[TimeOut]->timeOut); } -__code code4(struct Context* context, long* count, struct Allocate* allocate, struct Element* element) { - allocate->after_put = Code3; - allocate->after_fail = Code3; - element->value = (*count)++; +__code code5(struct Context* context, long* count, struct Allocate* allocate) { + long loop = *count; + if(loop == NUM) { + goto meta(context, ThreadExit); + } + allocate->size = sizeof(struct Element); + allocate->next = Code6; + goto meta(context, Allocator); +} + +__code code5_stub(struct Context* context) { + goto code5(context, &context->data[Counter]->count, &context->data[Allocate]->allocate); +} + +__code code6(struct Context* context, long* count, struct Allocate* allocate, struct Element* element) { + allocate->after_put = Code5; + allocate->after_fail = Code5; + element->value = (*count)++; goto meta(context, Sender); } -__code code4_stub(struct Context* context) { - goto code4(context, &context->data[Counter]->count, &context->data[Allocate]->allocate, &context->data[context->dataNum]->element); +__code code6_stub(struct Context* context) { + goto code6(context, &context->data[Counter]->count, &context->data[Allocate]->allocate, &context->data[context->dataNum]->element); } __code sender(struct Context* context) { @@ -84,22 +102,12 @@ goto sender(context); } -__code meta_put(struct Context* context, struct Allocate* allocate, struct Queue* queue, struct Element* element, enum Code next) { - struct Element *last_ds, *new_ds; - last_ds = queue->last; - new_ds = element; +__code meta_put(struct Context* context, struct Allocate* allocate, struct Queue* queue, struct Element* element, long* timeOut, enum Code next) { + struct Element* last_ds = queue->last; + struct Element* new_ds = element; new_ds->next = 0; - int count = 0; - while(!__sync_bool_compare_and_swap(&queue->last, last_ds, new_ds)) { - if(count < 1000) { - last_ds = queue->last; - new_ds = element; - new_ds->next = 0; - count++; - } else { - // error handle - goto meta(context, context->data[Allocate]->allocate.after_fail); - } + if(!__sync_bool_compare_and_swap(&queue->last, last_ds, new_ds)) { + goto (context->code[ContinuePut])(context); } if(queue->first) { @@ -108,50 +116,90 @@ queue->first = new_ds; } printf("Put %d\n\n", queue->last->value); + *timeOut = 0; queue->count++; goto (context->code[next])(context); } -__code put(struct Context* context, struct Allocate* allocate, struct Queue* queue, struct Element* element) { - goto meta_put(context, allocate, queue, element, allocate->after_put); +__code put(struct Context* context, struct Allocate* allocate, struct Queue* queue, struct Element* element, long* timeOut) { + goto meta_put(context, allocate, queue, element, timeOut, allocate->after_put); } __code put_stub(struct Context* context) { - goto put(context, &context->data[Allocate]->allocate, &context->data[Queue]->queue, &context->data[context->dataNum]->element); + goto put(context, &context->data[Allocate]->allocate, &context->data[Queue]->queue, &context->data[context->dataNum]->element, &context->data[TimeOut]->timeOut); } -__code code5(struct Context* context, struct Allocate* allocate) { +__code meta_continue_put(struct Context* context, struct Allocate* allocate, long *timeOut) { + if(*timeOut < 1000) { + (*timeOut)++; + goto (context->code[Put])(context); + } else { + *timeOut = 0; + // goto error handle + goto meta(context, allocate->after_fail); + } +} + +__code continue_put(struct Context* context, struct Allocate* allocate, long *timeOut) { + goto meta_continue_put(context, allocate, timeOut); +} + +__code continue_put_stub(struct Context* context) { + goto continue_put(context, &context->data[Allocate]->allocate, &context->data[TimeOut]->timeOut); +} + + +__code code7(struct Context* context, struct Allocate* allocate) { allocate->size = sizeof(long); - allocate->next = Code6; + allocate->next = Code8; goto meta(context, Allocator); } -__code code5_stub(struct Context* context) { - goto code5(context, &context->data[Allocate]->allocate); +__code code7_stub(struct Context* context) { + goto code7(context, &context->data[Allocate]->allocate); +} + +__code code8(struct Context* context, long* count) { + *count = 0; + goto meta(context, Code9); +} + +__code code8_stub(struct Context* context) { + goto code8(context, &context->data[Counter]->count); } -__code code6(struct Context* context, long* count) { - *count = 0; - goto meta(context, Code7); +__code code9(struct Context* context, struct Allocate* allocate) { + allocate->size = sizeof(long); + allocate->next = Code10; + goto meta(context, Allocator); +} + +__code code9_stub(struct Context* context) { + goto code9(context, &context->data[Allocate]->allocate); } -__code code6_stub(struct Context* context) { - goto code6(context, &context->data[Counter]->count); +__code code10(struct Context* context, long* timeOut) { + *timeOut = 0; + goto meta(context, Code11); } -__code code7(struct Context* context, long* count, struct Allocate* allocate) { +__code code10_stub(struct Context* context) { + goto code10(context, &context->data[TimeOut]->timeOut); +} + +__code code11(struct Context* context, long* count, struct Allocate* allocate) { long loop = *count; if(loop == NUM) { goto meta(context, ThreadExit); } (*count)++; - allocate->after_get = Code7; - allocate->after_fail = Code7; + allocate->after_get = Code11; + allocate->after_fail = Code11; goto meta(context, Receiver); } -__code code7_stub(struct Context* context) { - goto code7(context, &context->data[Counter]->count, &context->data[Allocate]->allocate); +__code code11_stub(struct Context* context) { + goto code11(context, &context->data[Counter]->count, &context->data[Allocate]->allocate); } __code receiver(struct Context* context) { @@ -162,23 +210,15 @@ goto receiver(context); } -__code meta_get(struct Context* context, struct Allocate* allocate, struct Queue* queue, struct Element* element, enum Code next) { - struct Element *first_ds, *new_ds; - first_ds = queue->first; - new_ds = first_ds? first_ds->next : 0; - int count = 0; - while(!__sync_bool_compare_and_swap(&queue->first, first_ds, new_ds)) { - if(count < 1000) { - first_ds = queue->first; - new_ds = first_ds? first_ds->next : 0; - count++; - } else { - // error handle - goto meta(context, context->data[Allocate]->allocate.after_fail); - } +__code meta_get(struct Context* context, struct Allocate* allocate, struct Queue* queue, long* timeOut, enum Code next) { + struct Element *first_ds = queue->first; + struct Element *new_ds = first_ds? first_ds->next : 0; + if(!__sync_bool_compare_and_swap(&queue->first, first_ds, new_ds)) { + goto (context->code[ContinueGet])(context); } - + // success CAS + *timeOut = 0; if (first_ds == new_ds) { printf("queue is empty\n"); goto meta(context, Get); @@ -189,18 +229,37 @@ } } -__code get(struct Context* context, struct Allocate* allocate, struct Queue* queue, struct Element* element) { - goto meta_get(context, allocate, queue, element, allocate->after_get); +__code get(struct Context* context, struct Allocate* allocate, struct Queue* queue, long* timeOut) { + goto meta_get(context, allocate, queue, timeOut, allocate->after_get); } __code get_stub(struct Context* context) { - goto get(context, &context->data[Allocate]->allocate, &context->data[Queue]->queue, &context->data[context->dataNum]->element); + goto get(context, &context->data[Allocate]->allocate, &context->data[Queue]->queue, &context->data[TimeOut]->timeOut); +} + +__code meta_continue_get(struct Context* context, struct Allocate* allocate, long *timeOut) { + if(*timeOut < 1000) { + (*timeOut)++; + goto (context->code[Get])(context); + } else { + *timeOut = 0; + // goto error handle + goto meta(context, allocate->after_fail); + } +} + +__code continue_get(struct Context* context, struct Allocate* allocate, long *timeOut) { + goto meta_continue_get(context, allocate, timeOut); +} + +__code continue_get_stub(struct Context* context) { + goto continue_get(context, &context->data[Allocate]->allocate, &context->data[TimeOut]->timeOut); } __code thread_exit(struct Context* context) { free(context->code); free(context->data); - free(context->heap_start); + free(context->heapStart); pthread_exit(0); } @@ -214,7 +273,7 @@ } void* thread_func2(void* context) { - goto start_code((struct Context*)context, Code5); + goto start_code((struct Context*)context, Code7); return 0; }