Mercurial > hg > Members > Moririn
view src/synchronizedQueue/synchronizedQueueForCas.c @ 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 | 4283b87ddbf4 |
children | a870c84acd0e |
line wrap: on
line source
#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, struct Allocate* allocate) { allocate->size = sizeof(long); allocate->next = Code2; goto meta(context, Allocator); } __code code1_stub(struct Context* context) { goto code1(context, &context->data[Allocate]->allocate); } __code code2(struct Context* context, long* count) { *count = 0; goto meta(context, Code3); } __code code2_stub(struct Context* context) { goto code2(context, &context->data[Counter]->count); } __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[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 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 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) { goto meta(context, Put); } __code sender_stub(struct Context* context) { goto sender(context); } __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; if(!__sync_bool_compare_and_swap(&queue->last, last_ds, new_ds)) { goto (context->code[ContinuePut])(context); } if(queue->first) { last_ds->next = new_ds; } else { 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, 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, &context->data[TimeOut]->timeOut); } __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 = Code8; goto meta(context, Allocator); } __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 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 code10(struct Context* context, long* timeOut) { *timeOut = 0; goto meta(context, Code11); } __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 = Code11; allocate->after_fail = Code11; goto meta(context, Receiver); } __code code11_stub(struct Context* context) { goto code11(context, &context->data[Counter]->count, &context->data[Allocate]->allocate); } __code receiver(struct Context* context) { goto meta(context, Get); } __code receiver_stub(struct Context* context) { goto receiver(context); } __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); } else { printf(" Get %d\n\n", first_ds->value); queue->count--; goto (context->code[next])(context); } } __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[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->heapStart); pthread_exit(0); } __code thread_exit_stub(struct Context* context) { goto thread_exit(context); } 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, Code7); 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); }