Mercurial > hg > Gears > GearsAgda
changeset 38:ce9fde200f3e
Add code segment sender & receiver to synchronizedQueue
author | Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp> |
---|---|
date | Sat, 16 May 2015 02:45:31 +0900 |
parents | a1fc4c670150 |
children | 754c90e96e3d |
files | src/synchronizedQueue/synchronizedQueue.c src/synchronizedQueue/synchronizedQueueContext.c src/synchronizedQueue/synchronizedQueueContext.h |
diffstat | 3 files changed, 50 insertions(+), 48 deletions(-) [+] |
line wrap: on
line diff
--- a/src/synchronizedQueue/synchronizedQueue.c Fri May 15 19:19:31 2015 +0900 +++ b/src/synchronizedQueue/synchronizedQueue.c Sat May 16 02:45:31 2015 +0900 @@ -18,30 +18,41 @@ goto (context->code[next])(context); } -__code meta_code2(struct Context* context, enum Code next) { - context->data[Queue]->queue.head = context->data[context->dataNum]; - context->data[Queue]->queue.tail = context->data[Queue]->queue.head; - context->data[context->dataNum]->element.next = 0; +__code code2(struct Context* context) { + context->data[Allocate]->allocate.after_put = Code3; + context->data[context->dataNum] -> element.value = 1024; + goto meta(context, Sender); +} + +__code meta_sender(struct Context* context, enum Code next) { goto (context->code[next])(context); } -__code code2(struct Context* context) { - context->data[context->dataNum] -> element.value = 1024; - context->data[Allocate]->allocate.size = sizeof(struct Element); - context->data[Allocate]->allocate.next = Code3; - goto meta_code2(context, Allocator); +__code sender(struct Context* context) { + goto meta(context, Put); } __code code3(struct Context* context) { - context->data[Allocate]->allocate.after_put = Code4; + context->data[Allocate]->allocate.size = sizeof(struct Element); + context->data[Allocate]->allocate.next = Code4; + goto meta(context, Allocator); +} + +__code code4(struct Context* context) { + context->data[Allocate]->allocate.after_put = Code5; context->data[context->dataNum] -> element.value = 10; - goto meta(context, Put); + goto meta(context, Sender); } __code meta_put(struct Context* context, enum Code next) { - context->data[Queue]->queue.tail->element.next = context->data[context->dataNum]; - context->data[Queue]->queue.tail = context->data[Queue]->queue.tail->element.next; - context->data[Queue]->queue.tail->element.next = 0; + if(context->data[Queue]->queue.first) { + context->data[Queue]->queue.last->element.next = context->data[context->dataNum]; + context->data[Queue]->queue.last = context->data[Queue]->queue.last->element.next; + } else { + context->data[Queue]->queue.first = context->data[context->dataNum]; + context->data[Queue]->queue.last = context->data[Queue]->queue.first; + } + context->data[Queue]->queue.last->element.next = 0; goto (context->code[next])(context); } @@ -50,37 +61,25 @@ } -__code meta_traverse(struct Context* context, enum Code next) { - printf("current value in queue is %d\n", context->data[Queue]->queue.current->element.value); - if (context->data[Queue]->queue.current->element.next) { - context->data[Queue]->queue.current = context->data[Queue]->queue.current->element.next; - goto meta_traverse(context, next); - } - goto (context->code[next])(context); +__code code5(struct Context* context) { + context->data[Allocate]->allocate.after_get = Code6; + goto meta(context, Receiver); } -__code traverse(struct Context* context) { - context->data[Queue]->queue.current = context->data[Queue]->queue.head; - goto meta_traverse(context, context->data[Allocate]->allocate.after_traverse); +__code meta_receiver(struct Context* context, enum Code next) { } -__code code4(struct Context* context) { - context->data[Allocate]->allocate.after_traverse = Code5; - goto meta(context, Traverse); -} - - -__code code5(struct Context* context) { - context->data[Allocate]->allocate.after_get = Code6; +__code receiver(struct Context* context, enum Code next) { goto meta(context, Get); } __code meta_get(struct Context* context, enum Code next) { - if (context->data[Queue]->queue.head == context->data[Queue]->queue.tail) { + if (context->data[Queue]->queue.first == context->data[Queue]->queue.last) { printf("queue is empty\n"); goto (context->code[Exit])(context); } - context->data[Queue]->queue.head = (context->data[Queue]->queue.head->element.next) ? context->data[Queue]->queue.head->element.next : 0; + printf("Get %d\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; goto (context->code[next])(context); } @@ -89,14 +88,15 @@ } __code code6(struct Context* context) { - printf("after delete\n"); - context->data[Allocate]->allocate.after_traverse = Exit; - goto meta(context, Traverse); + goto meta(context, Exit); } int main() { - struct Context* context = (struct Context*)malloc(sizeof(struct Context)); - initSynchronizedQueueContext(context); - goto start_code(context, Code1); + struct Context* context1 = (struct Context*)malloc(sizeof(struct Context)); + initSynchronizedQueueContext(context1); + struct Context* context2 = (struct Context*)malloc(sizeof(struct Context)); + initSynchronizedQueueContext(context2); + context2->data[Queue] = context1->data[Queue]; + goto start_code(context1, Code1); }
--- a/src/synchronizedQueue/synchronizedQueueContext.c Fri May 15 19:19:31 2015 +0900 +++ b/src/synchronizedQueue/synchronizedQueueContext.c Sat May 16 02:45:31 2015 +0900 @@ -10,8 +10,9 @@ extern __code code6(struct Context*); extern __code meta(struct Context*); extern __code allocate(struct Context*); +extern __code sender(struct Context*); extern __code put(struct Context*); -extern __code traverse(struct Context*); +extern __code receiver(struct Context*); extern __code get(struct Context*); extern __code exit_code(struct Context*); @@ -29,8 +30,9 @@ context->code[Code5] = code5; context->code[Code6] = code6; context->code[Allocator] = allocate; + context->code[Sender] = sender; context->code[Put] = put; - context->code[Traverse] = traverse; + context->code[Receiver] = receiver; context->code[Get] = get; context->code[Exit] = exit_code; @@ -41,6 +43,7 @@ context->data[Queue] = context->heap; context->heap += sizeof(struct Queue); + context->data[Queue]->queue.first = 0; context->dataNum = Queue; }
--- a/src/synchronizedQueue/synchronizedQueueContext.h Fri May 15 19:19:31 2015 +0900 +++ b/src/synchronizedQueue/synchronizedQueueContext.h Sat May 16 02:45:31 2015 +0900 @@ -10,8 +10,9 @@ Code5, Code6, Allocator, + Sender, Put, - Traverse, + Receiver, Get, Exit, }; @@ -35,10 +36,9 @@ union Data { long count; struct Queue { - union Data* head; - union Data* tail; - union Data* current; - int dataSize; + union Data* first; + union Data* last; + int count; } queue; struct Element { int value; @@ -49,6 +49,5 @@ enum Code next; enum Code after_put; enum Code after_get; - enum Code after_traverse; } allocate; };