Mercurial > hg > GearsTemplate
changeset 43:ec46ac3b0401
merge
author | Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 19 May 2015 05:07:22 +0900 |
parents | 44914699ee9b (current diff) 4a16cbaab802 (diff) |
children | a0a58875c93f |
files | |
diffstat | 3 files changed, 193 insertions(+), 3 deletions(-) [+] |
line wrap: on
line diff
--- a/src/synchronizedQueue/CMakeLists.txt Tue May 19 05:06:25 2015 +0900 +++ b/src/synchronizedQueue/CMakeLists.txt Tue May 19 05:07:22 2015 +0900 @@ -4,3 +4,8 @@ synchronizedQueue.c synchronizedQueueContext.c ) + +add_executable(synchronizedQueueForCas + synchronizedQueueForCas.c + synchronizedQueueContext.c +)
--- a/src/synchronizedQueue/synchronizedQueue.c Tue May 19 05:06:25 2015 +0900 +++ b/src/synchronizedQueue/synchronizedQueue.c Tue May 19 05:07:22 2015 +0900 @@ -142,17 +142,17 @@ free(context->code); free(context->data); free(context->heap_start); - pthread_exit(NULL); + pthread_exit(0); } void* thread_func(void* context) { goto start_code((struct Context*)context, Code1); - return NULL; + return 0; } void* thread_func2(void* context) { goto start_code((struct Context*)context, Code5); - return NULL; + return 0; } int main() {
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/synchronizedQueue/synchronizedQueueForCas.c Tue May 19 05:07:22 2015 +0900 @@ -0,0 +1,185 @@ +#include <stdlib.h> +#include <stdio.h> + +#include "synchronizedQueueContext.h" + +#include "allocate.h" +#include "origin_cs.h" + +#ifdef CLANG +#define _CbC_retrun __return +#define _CbC_environment __environment +#endif + +#define NUM 100 + +extern __code initSynchronizedQueueContext(struct Context* context); + +//__code code1(struct Context* context) { +// context->data[Allocate]->allocate.size = sizeof(struct Element); +// context->data[Allocate]->allocate.next = Code2; +// goto meta(context, Allocator); +//} + +__code meta(struct Context* context, enum Code next) { + goto (context->code[next])(context); +} + +//__code code2(struct Context* context) { +// context->data[Allocate]->allocate.after_put = Code3; +// context->data[context->dataNum] -> element.value = 1024; +// goto meta(context, Sender); +//} + +__code code1(struct Context* context) { + context->data[Allocate]->allocate.size = sizeof(long); + context->data[Allocate]->allocate.next = Code2; + goto meta(context, Allocator); +} + +__code code2(struct Context* context) { + context->data[Counter] -> count = 0; + goto meta(context, Code3); +} + +__code code3(struct Context* context) { + long loop = context->data[Counter]->count; + if(loop == NUM) { + goto meta(context, ThreadExit); + } + 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 = Code3; + context->data[context->dataNum] -> element.value = context->data[Counter]->count++; + goto meta(context, Sender); +} + +__code sender(struct Context* context) { + goto meta(context, Put); +} + +__code meta_put(struct Context* context, enum Code next) { + union Data *last_ds, *new_ds; + last_ds = context->data[Queue]->queue.last; + new_ds = context->data[context->dataNum]; + new_ds->element.next = 0; + + int old_count, new_count; + old_count = context->data[Queue]->queue.count; + new_count = old_count + 1; + 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; + } else { + 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); + } else { + printf("Fail\n"); + goto meta(context, Sender); + } +} + +__code put(struct Context* context) { + goto meta_put(context, context->data[Allocate]->allocate.after_put); +} + +__code code5(struct Context* context) { + context->data[Allocate]->allocate.size = sizeof(long); + context->data[Allocate]->allocate.next = Code6; + goto meta(context, Allocator); +} + +__code code6(struct Context* context) { + context->data[Counter] -> count = 0; + goto meta(context, Code7); +} + +__code code7(struct Context* context) { + long loop = context->data[Counter]->count; + if(loop == NUM) { + goto meta(context, ThreadExit); + } + context->data[Counter]->count++; + context->data[Allocate]->allocate.after_get = Code7; + goto meta(context, Receiver); +} + +__code receiver(struct Context* context) { + goto meta(context, Get); +} + +__code meta_get(struct Context* context, enum Code next) { + union Data *first_ds, *new_ds; + first_ds = context->data[Queue]->queue.first; + new_ds = first_ds? first_ds->element.next : 0; + int old_count, new_count; + old_count = context->data[Queue]->queue.count; + new_count = old_count == 0 ? 0 : old_count - 1; + 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); + } else { + printf("Get %d\n\n", first_ds->element.value); + context->data[Queue]->queue.count--; + goto (context->code[next])(context); + } + } else { + printf("Fail\n"); + goto meta(context, Receiver); + } +} + +__code get(struct Context* context) { + goto meta_get(context, context->data[Allocate]->allocate.after_get); +} + +__code thread_exit(struct Context* context) { + free(context->code); + free(context->data); + free(context->heap_start); + pthread_exit(0); +} + +void* thread_func(void* context) { + goto start_code((struct Context*)context, Code1); + return 0; +} + +void* thread_func2(void* context) { + goto start_code((struct Context*)context, Code5); + return 0; +} + +int main() { + struct Context* context1 = (struct Context*)malloc(sizeof(struct Context)); + initSynchronizedQueueContext(context1); + struct Context* context2 = (struct Context*)malloc(sizeof(struct Context)); + initSynchronizedQueueContext(context2); + struct Context* context3 = (struct Context*)malloc(sizeof(struct Context)); + initSynchronizedQueueContext(context3); + struct Context* context4 = (struct Context*)malloc(sizeof(struct Context)); + initSynchronizedQueueContext(context4); + context2->data[Queue] = context1->data[Queue]; + context3->data[Queue] = context1->data[Queue]; + context4->data[Queue] = context1->data[Queue]; + + // thread + pthread_t thread1, thread2, thread3, thread4; + pthread_create(&thread1, NULL, thread_func, (void *)context1); + pthread_create(&thread2, NULL, thread_func, (void *)context2); + pthread_create(&thread3, NULL, thread_func2, (void *)context3); + pthread_create(&thread4, NULL, thread_func2, (void *)context4); + pthread_join(thread1, NULL); + pthread_join(thread2, NULL); + pthread_join(thread3, NULL); + pthread_join(thread4, NULL); + printf("Count %d\n\n", context1->data[Queue]->queue.count); +}