Mercurial > hg > GearsTemplate
changeset 284:e6bc0a4c2c36
generate C_start_code and C_exit_code
author | mir3636 |
---|---|
date | Sun, 05 Feb 2017 19:08:22 +0900 |
parents | 2b41bd298fe8 |
children | bc17237bc8cf |
files | src/allocate/CMakeLists.txt src/allocate/allocate.c src/allocate/allocateContext.c src/allocate/allocateContext.h src/allocate/prototype.h src/list/CMakeLists.txt src/list/list.c src/list/listContext.c src/list/listContext.h src/list/prototype.h src/llrb/verifier/llrbContextWithVerifier.c src/llrb/verifier/llrbContextWithVerifier.h src/llrb/verifier/verify_put_cs.c src/parallel_execution/generate_context.pl src/parallel_execution/verifier/llrbContextWithVerifier.c src/parallel_execution/verifier/llrbContextWithVerifier.h src/parallel_execution/verifier/verify_put_cs.c src/synchronizedQueue/CMakeLists.txt src/synchronizedQueue/allocate.h src/synchronizedQueue/origin_cs.c src/synchronizedQueue/synchronizedQueue.c src/synchronizedQueue/synchronizedQueueContext.c src/synchronizedQueue/synchronizedQueueContext.h src/synchronizedQueue/synchronizedQueueForCas.c src/synchronizedQueue/synchronizedQueueForSem.c src/synchronizedQueue/synchronizedQueueForSemContext.c src/synchronizedQueue/synchronizedQueueForSemContext.h src/synchronizedQueue/synchronizedQueueIdeal.c |
diffstat | 28 files changed, 83 insertions(+), 1669 deletions(-) [+] |
line wrap: on
line diff
--- a/src/allocate/CMakeLists.txt Sun Feb 05 18:30:30 2017 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,6 +0,0 @@ -cmake_minimum_required(VERSION 2.8) - -add_executable(allocate - allocate.c - allocateContext.c -)
--- a/src/allocate/allocate.c Sun Feb 05 18:30:30 2017 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,65 +0,0 @@ -#include <stdio.h> -#include <stdlib.h> - -#include "allocateContext.h" - -#include "allocate.h" -#include "origin_cs.h" - -#ifdef CLANG -#define _CbC_retrun __return -#define _CbC_environment __environment -#endif - -#define NUM 100 -#define ALLOCATE_SIZE 1024 - -extern __code initAllocateContext(struct Context* context); -/* -__code code1(Allocate allocate) { - allocate.size = sizeof(long); - allocate.next = Code2; - goto Allocate(allocate); -} -*/ - -__code code1(struct Context* context) { - context->data[0]->allocate.size = sizeof(long); - context->data[0]->allocate.next = Code2; - goto meta(context, Allocate); -} - -__code meta(struct Context* context, enum Code next) { - goto (context->code[next])(context); -} - -/* -__code code2(Allocate allocate, Count count) { - count.count = 0; - goto code3(count); -} -*/ - -__code code2(struct Context* context) { - context->data[1]->count = 0; - goto meta(context, Code3); -} - -__code code3(struct Context* context) { - long loop = context->data[1]->count; - if (loop == NUM) { - goto meta(context, Exit); - } - printf("%ld\n", loop); - context->data[1]->count++; - goto meta(context, Code3); -} - -int main() { - struct Context* context = (struct Context*)malloc(sizeof(struct Context)); - context->code = malloc(sizeof(__code*)*ALLOCATE_SIZE); - context->data = malloc(sizeof(union Data**)*ALLOCATE_SIZE); - context->heap = malloc(sizeof(union Data*)*ALLOCATE_SIZE); - initAllocateContext(context); - goto start_code(context, Code1); -}
--- a/src/allocate/allocateContext.c Sun Feb 05 18:30:30 2017 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,20 +0,0 @@ -#include "allocateContext.h" -extern __code code1(struct Context*); -extern __code code2(struct Context*); -extern __code code3(struct Context*); -extern __code meta(struct Context*); -extern __code allocate(struct Context*); -extern __code exit_code(struct Context*); - -__code initAllocateContext(struct Context* context) { - context->codeSize = 3; - context->code[Code1] = code1; - context->code[Code2] = code2; - context->code[Code3] = code3; - context->code[Allocate] = allocate; - context->code[Exit] = exit_code; - - context->dataSize = 0; - context->data[context->dataSize] = context->heap; - context->heap += sizeof(struct Allocate); -}
--- a/src/allocate/allocateContext.h Sun Feb 05 18:30:30 2017 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,26 +0,0 @@ -/* Context definition for allocate example */ - -enum Code { - Code1, - Code2, - Code3, - Allocate, - Exit, -}; - -struct Context { - int codeSize; - __code (**code) (struct Context *); - void* heap; - int dataSize; - union Data **data; -}; - -union Data { - char data[8]; - long count; - struct Allocate { - long size; - enum Code next; - } allocate; -};
--- a/src/allocate/prototype.h Sun Feb 05 18:30:30 2017 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,23 +0,0 @@ -typedef struct DataSegment1 { - int i; // 4 byte - char c[10]; // 10 byte - // padding 2 byte -} data1; - -typedef struct DataSegment2 { - int x; // 4 byte - int y; // 4 byte - int z; // 4 byte -} data2; - -typedef struct metaDataSegment { - size_t size; // 8 byte - void* ds; // 8 byte -} mdata; - -__code code1(); -__code meta_code1(); -__code code2(); -__code meta_code2(); -__code code3(); -__code meta_code3();
--- a/src/list/CMakeLists.txt Sun Feb 05 18:30:30 2017 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,6 +0,0 @@ -cmake_minimum_required(VERSION 2.8) - -add_executable(list - list.c - listContext.c -)
--- a/src/list/list.c Sun Feb 05 18:30:30 2017 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,131 +0,0 @@ -#include <stdlib.h> -#include <stdio.h> - -#include "listContext.h" - -#include "allocate.h" -#include "origin_cs.h" - -extern __code initListContext(struct Context* context); - -__code code1(struct Context* context, struct Allocate* allocate) { - allocate->size = sizeof(struct Element); - allocate->next = Code2; - goto meta(context, Allocator); -} - -__code code1_stub(struct Context* context) { - goto code1(context, &context->data[Allocate]->allocate); -} - -__code meta(struct Context* context, enum Code next) { - goto (context->code[next])(context); -} - -__code code2(struct Context* context, struct Allocate* allocate, struct Element* element) { - allocate->after_append = Code3; - element ->value = 10; - goto meta(context, Append); -} - -__code code2_stub(struct Context* context) { - goto code2(context, &context->data[Allocate]->allocate, &context->data[context->dataNum]->element); -} - - -__code append(struct Context* context, struct Allocate* allocate, struct List* list, struct Element* element) { - if(list->head) { - list->tail->next = element; - } else { - list->head = element; - } - list->tail = element; - list->tail->next = 0; - goto meta(context, allocate->after_append); -} - - -__code append_stub(struct Context* context) { - goto append(context,&context->data[Allocate]->allocate, &context->data[List]->list, &context->data[context->dataNum]->element); -} - -__code code3(struct Context* context, struct Allocate *allocate) { - allocate->size = sizeof(struct Element); - 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, struct Allocate* allocate, struct Element* element) { - allocate->after_append = Code5; - element ->value = 100; - goto meta(context, Append); -} - -__code code4_stub(struct Context* context) { - goto code4(context, &context->data[Allocate]->allocate, &context->data[context->dataNum]->element); -} - -__code meta_traverse(struct Context* context, struct List* list, enum Code next) { - printf("current value in list is %d\n", list->current->value); - if (list->current->next) { - list->current = list->current->next; - goto meta_traverse(context, list, next); - } - goto (context->code[next])(context); -} - -__code traverse(struct Context* context, struct Allocate* allocate, struct List* list) { - list->current = list->head; - goto meta_traverse(context, list, allocate->after_traverse); -} - -__code traverse_stub(struct Context* context) { - goto traverse(context, &context->data[Allocate]->allocate, &context->data[List]->list); -} - -__code code5(struct Context* context, struct Allocate* allocate) { - allocate->after_traverse = Code6; - goto meta(context, Traverse); -} - -__code code5_stub(struct Context* context) { - goto code5(context, &context->data[Allocate]->allocate); -} - -__code code6(struct Context* context, struct Allocate* allocate) { - allocate->after_delete = Code7; - goto meta(context, Delete); -} - -__code code6_stub(struct Context* context) { - goto code6(context, &context->data[Allocate]->allocate); -} - -__code delete(struct Context* context, struct Allocate* allocate, struct List* list) { - list->head = (list->head->next) ? list->head->next : 0; - goto meta(context, allocate->after_delete); -} - -__code delete_stub(struct Context* context) { - goto delete(context, &context->data[Allocate]->allocate, &context->data[List]->list); -} - -__code code7(struct Context* context, struct Allocate* allocate) { - printf("after delete\n"); - allocate->after_traverse = Exit; - goto meta(context, Traverse); -} - -__code code7_stub(struct Context* context) { - goto code7(context, &context->data[Allocate]->allocate); -} - -int main() { - struct Context* context = (struct Context*)malloc(sizeof(struct Context)); - initListContext(context); - goto start_code(context, Code1); -}
--- a/src/list/listContext.c Sun Feb 05 18:30:30 2017 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,48 +0,0 @@ -#include <stdlib.h> - -#include "listContext.h" - -extern __code code1_stub(struct Context*); -extern __code code2_stub(struct Context*); -extern __code code3_stub(struct Context*); -extern __code code4_stub(struct Context*); -extern __code code5_stub(struct Context*); -extern __code code6_stub(struct Context*); -extern __code code7_stub(struct Context*); -extern __code meta(struct Context*); -extern __code allocate(struct Context*); -extern __code append_stub(struct Context*); -extern __code traverse_stub(struct Context*); -extern __code delete_stub(struct Context*); -extern __code exit_code(struct Context*); - -__code initListContext(struct Context* context) { - 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->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[Append] = append_stub; - context->code[Traverse] = traverse_stub; - context->code[Delete] = delete_stub; - context->code[Exit] = exit_code; - - context->heap = context->heap_start; - - context->data[Allocate] = context->heap; - context->heap += sizeof(struct Allocate); - - context->data[List] = context->heap; - context->heap += sizeof(struct List); - - context->dataNum = List; -}
--- a/src/list/listContext.h Sun Feb 05 18:30:30 2017 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,54 +0,0 @@ -/* Context definition for list example */ - -#define ALLOCATE_SIZE 100 - -enum Code { - Code1, - Code2, - Code3, - Code4, - Code5, - Code6, - Code7, - Allocator, - Append, - Traverse, - Delete, - Exit, -}; - -enum UniqueData { - Allocate, - List, -}; - -struct Context { - int codeNum; - __code (**code) (struct Context *); - void* heap_start; - void* heap; - long dataSize; - int dataNum; - union Data* head; - union Data** data; -}; - -union Data { - long count; - struct List { - struct Element* head; - struct Element* tail; - struct Element* current; - } list; - struct Element { - int value; - struct Element* next; - } element; - struct Allocate { - long size; - enum Code next; - enum Code after_append; - enum Code after_delete; - enum Code after_traverse; - } allocate; -};
--- a/src/list/prototype.h Sun Feb 05 18:30:30 2017 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,29 +0,0 @@ -typedef struct DataSegment1 { - int value; // 4 byte -} data1; - -typedef struct metaDataSegment { - size_t size; // 8 byte - void* ds; // 8 byte - void* next; // 8 byte -} mdata; - -__code code1(); -__code meta_code1(); -__code code2(); -__code meta_code2(); -__code code3(); -__code meta_code3(); -__code code4(); -__code meta_code4(); -__code code5(); -__code meta_code5(); -__code code6(); -__code meta_code6(); - -__code append(); -__code meta_append(); -__code traverse(); -__code meta_traverse(); -__code delete(); -__code meta_delete();
--- a/src/llrb/verifier/llrbContextWithVerifier.c Sun Feb 05 18:30:30 2017 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,46 +0,0 @@ -#include <stdio.h> -#include "llrbContextWithVerifier.h" - -unsigned int min_height(struct Node* node, unsigned int height) { - if ((node->left == NULL) && (node->right == NULL)) return height; - if (node->left == NULL) return min_height(node->right, height+1); - if (node->right == NULL) return min_height(node->left, height+1); - - unsigned int left_min = min_height(node->left, height+1); - unsigned int right_min = min_height(node->right, height+1); - - if (left_min < right_min) { - return left_min; - } else { - return right_min; - } -} - -unsigned int max_height(struct Node* node, unsigned int height) { - if ((node->left == NULL) && (node->right == NULL)) return height; - if (node->left == NULL) return max_height(node->right, height+1); - if (node->right == NULL) return max_height(node->left, height+1); - - unsigned int left_max = max_height(node->left, height+1); - unsigned int right_max = max_height(node->right, height+1); - - if (left_max > right_max) { - return left_max; - } else { - return right_max; - } -} - -void verify_tree_height(struct Node* root) { - if (root == NULL) return; - - unsigned int min_h = min_height(root, 1); - unsigned int max_h = max_height(root, 1); - - if (max_h >= 2*min_h) { - printf("llrb-condition violated.\n"); - printf("\tmin-height %u", min_h); - printf("\tmax-height %u", max_h); - exit(EXIT_FAILURE); - } -}
--- a/src/llrb/verifier/llrbContextWithVerifier.h Sun Feb 05 18:30:30 2017 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,5 +0,0 @@ -#include "llrbContext.h" - -unsigned int min_height(struct Node* node, unsigned int height); -unsigned int max_height(struct Node* node, unsigned int height); -void verify_tree_height(struct Node* root);
--- a/src/llrb/verifier/verify_put_cs.c Sun Feb 05 18:30:30 2017 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,29 +0,0 @@ -/* Verification of LLRB-Tree height in put operations. - * LLRB-Tree allows (max-height) <= 2*(min-height). - */ - -#include <stdlib.h> -#include <stdio.h> -#include <time.h> -#include "llrbContextWithVerifier.h" - -__code meta(struct Context* context, enum Code next) { - if (next == Put) { - verify_tree_height(context->data[Tree]->tree.root); - } - goto (context->code[next])(context); -} - -__code start_code(struct Context* context, enum Code next) { - unsigned int seed = (unsigned int)time(NULL); - - printf("--- srand(%u)\n", seed); - goto meta(context, next); -} - -__code exit_code(struct Context* context) { - free(context->code); - free(context->data); - free(context->heapStart); - goto exit(0); -}
--- a/src/parallel_execution/generate_context.pl Sun Feb 05 18:30:30 2017 +0900 +++ b/src/parallel_execution/generate_context.pl Sun Feb 05 19:08:22 2017 +0900 @@ -46,7 +46,7 @@ our($opt_o,$opt_d,$opt_h); getopts('o:d:h'); -my $name = $opt_o; +my $name = $opt_o?$opt_o:"gears"; if ($opt_d) { $ddir = $opt_d; @@ -112,6 +112,8 @@ } sub generateContext { + $codeGear{"start_code"} = "$ddir/$name-context.c"; + $codeGear{"exit_code"} = "$ddir/$name-context.c"; open my $fd,">","$ddir/extern.h" or die("can't open $ddir/extern.h $!"); for my $code ( sort keys %codeGear ) { print $fd "extern __code ${code}_stub(struct Context*);\n"; @@ -150,9 +152,6 @@ $code_init - context->code[C_start_code] = start_code_stub; - context->code[C_exit_code] = exit_code_stub; - #include "dataGearInit.c" // context->data[D_ActiveQueue] = createSynchronizedQueue(context);
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/parallel_execution/verifier/llrbContextWithVerifier.c Sun Feb 05 19:08:22 2017 +0900 @@ -0,0 +1,46 @@ +#include <stdio.h> +#include "llrbContextWithVerifier.h" + +unsigned int min_height(struct Node* node, unsigned int height) { + if ((node->left == NULL) && (node->right == NULL)) return height; + if (node->left == NULL) return min_height(node->right, height+1); + if (node->right == NULL) return min_height(node->left, height+1); + + unsigned int left_min = min_height(node->left, height+1); + unsigned int right_min = min_height(node->right, height+1); + + if (left_min < right_min) { + return left_min; + } else { + return right_min; + } +} + +unsigned int max_height(struct Node* node, unsigned int height) { + if ((node->left == NULL) && (node->right == NULL)) return height; + if (node->left == NULL) return max_height(node->right, height+1); + if (node->right == NULL) return max_height(node->left, height+1); + + unsigned int left_max = max_height(node->left, height+1); + unsigned int right_max = max_height(node->right, height+1); + + if (left_max > right_max) { + return left_max; + } else { + return right_max; + } +} + +void verify_tree_height(struct Node* root) { + if (root == NULL) return; + + unsigned int min_h = min_height(root, 1); + unsigned int max_h = max_height(root, 1); + + if (max_h >= 2*min_h) { + printf("llrb-condition violated.\n"); + printf("\tmin-height %u", min_h); + printf("\tmax-height %u", max_h); + exit(EXIT_FAILURE); + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/parallel_execution/verifier/llrbContextWithVerifier.h Sun Feb 05 19:08:22 2017 +0900 @@ -0,0 +1,5 @@ +#include "llrbContext.h" + +unsigned int min_height(struct Node* node, unsigned int height); +unsigned int max_height(struct Node* node, unsigned int height); +void verify_tree_height(struct Node* root);
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/parallel_execution/verifier/verify_put_cs.c Sun Feb 05 19:08:22 2017 +0900 @@ -0,0 +1,29 @@ +/* Verification of LLRB-Tree height in put operations. + * LLRB-Tree allows (max-height) <= 2*(min-height). + */ + +#include <stdlib.h> +#include <stdio.h> +#include <time.h> +#include "llrbContextWithVerifier.h" + +__code meta(struct Context* context, enum Code next) { + if (next == Put) { + verify_tree_height(context->data[Tree]->tree.root); + } + goto (context->code[next])(context); +} + +__code start_code(struct Context* context, enum Code next) { + unsigned int seed = (unsigned int)time(NULL); + + printf("--- srand(%u)\n", seed); + goto meta(context, next); +} + +__code exit_code(struct Context* context) { + free(context->code); + free(context->data); + free(context->heapStart); + goto exit(0); +}
--- a/src/synchronizedQueue/CMakeLists.txt Sun Feb 05 18:30:30 2017 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,17 +0,0 @@ -cmake_minimum_required(VERSION 2.8) - -add_executable(synchronizedQueueForCas - synchronizedQueueForCas.c - synchronizedQueueContext.c - origin_cs.c -) - -#add_executable(synchronizedQueue -# synchronizedQueue.c -# synchronizedQueueContext.c -#) - -#add_executable(synchronizedQueueForSem -# synchronizedQueueForSem.c -# synchronizedQueueForSemContext.c -#)
--- a/src/synchronizedQueue/allocate.h Sun Feb 05 18:30:30 2017 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,5 +0,0 @@ -void allocator(struct Context* context) { - context->data[++context->dataNum] = context->heap; - context->heap += context->data[0]->allocate.size; - return; -}
--- a/src/synchronizedQueue/origin_cs.c Sun Feb 05 18:30:30 2017 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,17 +0,0 @@ -#include <stdlib.h> -#include "synchronizedQueueContext.h" - -__code meta(struct Context* context, enum Code next) { - goto (context->code[next])(context); -} - -__code start_code(struct Context* context, enum Code next) { - goto meta(context, next); -} - -__code exit_code(struct Context* context) { - free(context->code); - free(context->data); - free(context->heapStart); - goto exit(0); -}
--- a/src/synchronizedQueue/synchronizedQueue.c Sun Feb 05 18:30:30 2017 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,215 +0,0 @@ -#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); -extern void allocator(struct Context* context); - -//__code code1(struct Context* context) { -// context->data[Allocate]->allocate.size = sizeof(struct Element); -// goto code2(context); -//} - -__code code1(struct Context* context, struct Allocate* allocate) { - allocate->size = sizeof(long); - allocator(context); - goto meta(context, Code2); -} - -__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, long* count, struct Allocate* allocate) { - long loop = *count; - if(loop == NUM) { - goto meta(context, ThreadExit); - } - allocate->size = sizeof(struct Element); - allocator(context); - goto meta(context, Code4); -} - -__code code3_stub(struct Context* context) { - goto code3(context, &context->data[Counter]->count, &context->data[Allocate]->allocate); -} - -__code code4(struct Context* context, long* count, struct Allocate* allocate, struct Element* element) { - allocate->after_put = Code3; - 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 meta_sender(struct Context* context, struct Queue* queue, enum Code next) { - // lock - pthread_mutex_lock(&queue->mutex); - goto (context->code[next])(context); -} - -__code sender(struct Context* context, struct Queue* queue) { - goto meta_sender(context, queue, Put); -} - -__code sender_stub(struct Context* context) { - goto sender(context, &context->data[Queue]->queue); -} - -__code meta_put(struct Context* context, struct Queue* queue, enum Code next) { - // signal - pthread_cond_signal(&queue->cond); - // unlock - pthread_mutex_unlock(&queue->mutex); - goto (context->code[next])(context); -} - -__code put(struct Context* context, struct Allocate* allocate, struct Queue* queue, struct Element* element) { - if(queue->first) { - queue->last->next = element; - } else { - queue->first = element; - } - queue->last = element; - element->next = 0; - queue->count++; - printf("Put %d\n\n", element->value); - goto meta_put(context, queue, 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); -} - -__code code5(struct Context* context, struct Allocate* allocate) { - allocate->size = sizeof(long); - allocator(context); - goto meta(context, Code6); -} - -__code code5_stub(struct Context* context) { - goto code5(context, &context->data[Allocate]->allocate); -} - -__code code6(struct Context* context, long* count) { - *count = 0; - goto meta(context, Code7); -} - -__code code6_stub(struct Context* context) { - goto code6(context, &context->data[Counter]->count); -} - -__code code7(struct Context* context, long* count, struct Allocate* allocate) { - long loop = *count; - if(loop == NUM) { - goto meta(context, ThreadExit); - } - (*count)++; - allocate->after_get = Code7; - goto meta(context, Receiver); -} - -__code code7_stub(struct Context* context) { - goto code7(context, &context->data[Counter]->count, &context->data[Allocate]->allocate); -} - -__code meta_receiver(struct Context* context, struct Queue* queue, enum Code next) { - // lock - pthread_mutex_lock(&queue->mutex); - goto (context->code[next])(context); -} - -__code receiver(struct Context* context, struct Queue* queue) { - goto meta_receiver(context, queue, Get); -} - -__code receiver_stub(struct Context* context) { - goto receiver(context, &context->data[Queue]->queue); -} - -__code meta_get(struct Context* context, struct Queue* queue, enum Code next) { - pthread_mutex_unlock(&queue->mutex); - goto (context->code[next])(context); -} - -__code get(struct Context* context, struct Allocate* allocate, struct Queue* queue, struct Element* element) { - // thread wait if queue is empty - while (queue->count == 0) { - pthread_cond_wait(&queue->cond, &queue->mutex); - } - printf(" Get %d\n\n", queue->first->value); - queue->first = (queue->first->next) ? queue->first->next : 0; - queue->count--; - goto meta_get(context, queue, 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); -} - -__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, 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]; - 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); -}
--- a/src/synchronizedQueue/synchronizedQueueContext.c Sun Feb 05 18:30:30 2017 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,65 +0,0 @@ -#include <stdlib.h> - -#include "synchronizedQueueContext.h" - -extern __code code1_stub(struct Context*); -extern __code code2_stub(struct Context*); -extern __code code3_stub(struct Context*); -extern __code code4_stub(struct Context*); -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 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*); - -__code initSynchronizedQueueContext(struct Context* context) { - context->dataSize = sizeof(union Data)*ALLOCATE_SIZE; - context->code = malloc(sizeof(__code*)*ALLOCATE_SIZE); - context->data = malloc(sizeof(union Data*)*ALLOCATE_SIZE); - context->heapStart = malloc(context->dataSize); - - context->codeNum = Exit; - 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->heapStart; - - context->data[Allocate] = context->heap; - context->heap += sizeof(struct Allocate); - - context->data[Queue] = context->heap; - context->heap += sizeof(struct Queue); - context->data[Queue]->queue.first = 0; - pthread_mutex_init(&context->data[Queue]->queue.mutex, NULL); - pthread_cond_init(&context->data[Queue]->queue.cond, NULL); - - context->dataNum = Queue; -}
--- a/src/synchronizedQueue/synchronizedQueueContext.h Sun Feb 05 18:30:30 2017 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,66 +0,0 @@ -/* Context definition for synchronized queue example */ - -#include <pthread.h> -#define ALLOCATE_SIZE 1000 - -enum Code { - Code1, - Code2, - Code3, - Code4, - Code5, - Code6, - Code7, - Code8, - Code9, - Code10, - Code11, - Sender, - Put, - ContinuePut, - Receiver, - Get, - ContinueGet, - ThreadExit, - Exit, -}; - -enum UniqueData { - Allocate, - Queue, - Counter, - TimeOut, -}; - -struct Context { - int codeNum; - __code (**code) (struct Context *); - void* heapStart; - void* heap; - long dataSize; - int dataNum; - union Data* head; - union Data** data; -}; - -union Data { - long count; - long timeOut; - struct Queue { - struct Element* first; - struct Element* last; - int count; - pthread_mutex_t mutex; - pthread_cond_t cond; - } queue; - struct Element { - int value; - struct Element* next; - } element; - struct Allocate { - long size; - enum Code after_put; - enum Code after_get; - enum Code after_fail; - } allocate; -};
--- a/src/synchronizedQueue/synchronizedQueueForCas.c Sun Feb 05 18:30:30 2017 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,294 +0,0 @@ -#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); -extern void allocator(struct Context* context); - - -//__code code1(struct Context* context) { -// context->data[Allocate]->allocate.size = sizeof(struct Element); -// goto code2(context); -//} - -__code code1(struct Context* context, struct Allocate* allocate) { - allocate->size = sizeof(long); - allocator(context); - goto meta(context, Code2); -} - -__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); - allocator(context); - goto meta(context, Code4); -} - -__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); - allocator(context); - goto meta(context, Code6); -} - -__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); - allocator(context); - goto meta(context, Code8); -} - -__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); - allocator(context); - goto meta(context, Code10); -} - -__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); -}
--- a/src/synchronizedQueue/synchronizedQueueForSem.c Sun Feb 05 18:30:30 2017 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,194 +0,0 @@ -#include <stdlib.h> -#include <stdio.h> - -#include "synchronizedQueueForSemContext.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 meta_sender(struct Context* context, enum Code next) { - goto (context->code[next])(context); -} - -__code sender(struct Context* context) { - goto meta_sender(context, Put); -} - -__code meta_put(struct Context* context, enum Code next) { - // sem_p - pthread_mutex_lock(&context->data[Queue]->queue.queue_remain->mutex); - while(context->data[Queue]->queue.queue_remain->value == 0) { - pthread_cond_wait(&context->data[Queue]->queue.queue_remain->cond, &context->data[Queue]->queue.queue_remain->mutex); - } - context->data[Queue]->queue.queue_remain->value--; - pthread_mutex_unlock(&context->data[Queue]->queue.queue_remain->mutex); - - // put - 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; - printf("Put %d\n\n", context->data[Queue]->queue.last->element.value); - - // sem_v - pthread_mutex_lock(&context->data[Queue]->queue.queue_count->mutex); - context->data[Queue]->queue.queue_count->value++; - pthread_cond_signal(&context->data[Queue]->queue.queue_count->cond); - pthread_mutex_unlock(&context->data[Queue]->queue.queue_count->mutex); - - goto (context->code[next])(context); -} - -__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 meta_receiver(struct Context* context, enum Code next) { - goto (context->code[next])(context); -} - -__code receiver(struct Context* context) { - goto meta_receiver(context, Get); -} - -__code meta_get(struct Context* context, enum Code next) { - // sem_p - pthread_mutex_lock(&context->data[Queue]->queue.queue_count->mutex); - while(context->data[Queue]->queue.queue_count->value == 0) { - pthread_cond_wait(&context->data[Queue]->queue.queue_count->cond, &context->data[Queue]->queue.queue_count->mutex); - } - 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); - context->data[Queue]->queue.first = (context->data[Queue]->queue.first->element.next) ? context->data[Queue]->queue.first->element.next : 0; - - // sem_v - pthread_mutex_lock(&context->data[Queue]->queue.queue_remain->mutex); - context->data[Queue]->queue.queue_remain->value++; - pthread_cond_signal(&context->data[Queue]->queue.queue_remain->cond); - pthread_mutex_unlock(&context->data[Queue]->queue.queue_remain->mutex); - - goto (context->code[next])(context); -} - -__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]; - 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); -}
--- a/src/synchronizedQueue/synchronizedQueueForSemContext.c Sun Feb 05 18:30:30 2017 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,63 +0,0 @@ -#include <stdlib.h> - -#include "synchronizedQueueForSemContext.h" - -extern __code code1(struct Context*); -extern __code code2(struct Context*); -extern __code code3(struct Context*); -extern __code code4(struct Context*); -extern __code code5(struct Context*); -extern __code code6(struct Context*); -extern __code code7(struct Context*); -extern __code meta(struct Context*); -extern __code allocate(struct Context*); -extern __code sender(struct Context*); -extern __code put(struct Context*); -extern __code receiver(struct Context*); -extern __code get(struct Context*); -extern __code exit_code(struct Context*); -extern __code thread_exit(struct Context*); - -__code initSynchronizedQueueContext(struct Context* context) { - 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->codeNum = Exit; - context->code[Code1] = code1; - context->code[Code2] = code2; - context->code[Code3] = code3; - context->code[Code4] = code4; - context->code[Code5] = code5; - context->code[Code6] = code6; - context->code[Code7] = code7; - context->code[Allocator] = allocate; - context->code[Sender] = sender; - context->code[Put] = put; - context->code[Receiver] = receiver; - context->code[Get] = get; - context->code[Exit] = exit_code; - context->code[ThreadExit] = thread_exit; - - context->heap = context->heap_start; - - context->data[Allocate] = context->heap; - context->heap += sizeof(struct Allocate); - - context->data[Queue] = context->heap; - context->heap += sizeof(struct Queue); - context->data[Queue]->queue.first = 0; - context->data[Queue]->queue.queue_remain = malloc(sizeof(struct Sem)); - pthread_mutex_init(&context->data[Queue]->queue.queue_remain->mutex, NULL); - pthread_cond_init(&context->data[Queue]->queue.queue_remain->cond, NULL); - context->data[Queue]->queue.queue_remain->value = 10; - - context->data[Queue]->queue.queue_count = malloc(sizeof(struct Sem)); - pthread_mutex_init(&context->data[Queue]->queue.queue_count->mutex, NULL); - pthread_cond_init(&context->data[Queue]->queue.queue_count->cond, NULL); - context->data[Queue]->queue.queue_count->value = 0; - - - context->dataNum = Queue; -}
--- a/src/synchronizedQueue/synchronizedQueueForSemContext.h Sun Feb 05 18:30:30 2017 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,63 +0,0 @@ -/* Context definition for synchronized queue for sem example */ - -#include <pthread.h> -#define ALLOCATE_SIZE 1000 - -enum Code { - Code1, - Code2, - Code3, - Code4, - Code5, - Code6, - Code7, - Allocator, - Sender, - Put, - Receiver, - Get, - Exit, - ThreadExit, -}; - -enum UniqueData { - Allocate, - Queue, - Counter, -}; - -struct Context { - int codeNum; - __code (**code) (struct Context *); - void* heap_start; - void* heap; - long dataSize; - int dataNum; - union Data* head; - union Data** data; -}; - - -union Data { - long count; - struct Queue { - union Data* first; - union Data* last; - struct Sem { - volatile int value; - pthread_mutex_t mutex; - pthread_cond_t cond; - - } *queue_remain, *queue_count; - } queue; - struct Element { - int value; - union Data* next; - } element; - struct Allocate { - long size; - enum Code next; - enum Code after_put; - enum Code after_get; - } allocate; -};
--- a/src/synchronizedQueue/synchronizedQueueIdeal.c Sun Feb 05 18:30:30 2017 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,178 +0,0 @@ -#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); -extern void allocator(struct Context* context); - -__code meta(struct Context* context, enum Code next) { - goto (context->code[next])(context); -} - -//__code code1(struct Context* context) { -// context->data[Allocate]->allocate.size = sizeof(struct Element); -// goto code2(context); -//} - -__code code1(struct Context* context, struct Allocate* allocate) { - allocate->size = sizeof(long); - allocator(); - goto code2(count) -} - -__code code2(struct Context* context, long* count) { - *count = 0; - goto code3(count, allocate); -} - -__code code3(struct Context* context, long* count, struct Allocate* allocate) { - long loop = *count; - if(loop == NUM) { - goto thread_exit(); - } - allocate->size = sizeof(struct Element); - allocator(context); - got code4(count, allocate, element); -} - -__code code4(struct Context* context, long* count, struct Allocate* allocate, struct Element* element) { - allocate->after_put = Code3; - element->value = (*count)++; - struct Queue* queue = &context->data[Queue]->queue; - goto sender(queue); -} - -__code meta_sender(struct Context* context, struct Queue* queue, enum Code next) { - // lock - pthread_mutex_lock(&queue->mutex); - goto (context->code[next])(context); -} - -__code sender(struct Context* context, struct Queue* queue) { - goto put(queue); -} - -__code meta_put(struct Context* context, struct Queue* queue, enum Code next) { - // signal - pthread_cond_signal(&queue->cond); - // unlock - pthread_mutex_unlock(&queue->mutex); - goto (context->code[next])(context); -} - -__code put(struct Context* context, struct Allocate* allocate, struct Queue* queue, struct Element* element) { - if(queue->first) { - queue->last->next = element; - } else { - queue->first = element; - } - queue->last = element; - element->next = 0; - queue->count++; - printf("Put %d\n\n", element->value); - goto meta_put(context, queue, allocate->after_put); -} - -__code code5(struct Context* context, struct Allocate* allocate) { - allocate->size = sizeof(long); - allocator(); - long* count = &context->data[Counter]->count - goto code6(count); -} - -__code code5_stub(struct Context* context) { - goto code5(context, &context->data[Allocate]->allocate); -} - -__code code6(struct Context* context, long* count) { - *count = 0; - struct Allocate* allocate = &context->data[Allocate]->allocate; - goto code7(count, allocate); -} - -__code code7(struct Context* context, long* count, struct Allocate* allocate) { - long loop = *count; - if(loop == NUM) { - goto threadExit(); - } - (*count)++; - allocate->after_get = Code7; - goto receiver(queue); -} - -__code meta_receiver(struct Context* context, struct Queue* queue, enum Code next) { - // lock - pthread_mutex_lock(&queue->mutex); - goto (context->code[next])(context); -} - -__code receiver(struct Context* context, struct Queue* queue) { - goto get(queue); -} - -__code meta_get(struct Context* context, struct Queue* queue, enum Code next) { - pthread_mutex_unlock(&queue->mutex); - goto (context->code[next])(context); -} - -__code get(struct Context* context, struct Allocate* allocate, struct Queue* queue, struct Element* element) { - // thread wait if queue is empty - while (queue->count == 0) { - pthread_cond_wait(&queue->cond, &queue->mutex); - } - printf(" Get %d\n\n", queue->first->value); - queue->first = (queue->first->next) ? queue->first->next : 0; - queue->count--; - goto meta_get(context, allocate->after_get); -} - -__code thread_exit(struct Context* context) { - free(context->code); - free(context->data); - free(context->heapStart); - 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]; - 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); -}