Mercurial > hg > CbC > old > CbC_scripts
changeset 26:b9f88346cecd support auto stub generation
generate stub
author | Kaito Tokumori <e105711@ie.u-ryukyu.ac.jp> |
---|---|
date | Mon, 02 Nov 2015 04:02:13 +0900 |
parents | 9a1cce532f29 |
children | a3798ce85b6b |
files | meta_connector/example/allocate.h meta_connector/example/origin_cs.h meta_connector/example/synchronizedQueue.c meta_connector/example/synchronizedQueueConnected.c meta_connector/example/synchronizedQueueContext.c meta_connector/example/synchronizedQueueContext.h meta_connector/example/synchronizedQueueIdeal.c meta_connector/meta_connector.py meta_connector/synchronizedQueueIdeal.c |
diffstat | 9 files changed, 806 insertions(+), 183 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/meta_connector/example/allocate.h Mon Nov 02 04:02:13 2015 +0900 @@ -0,0 +1,5 @@ +void allocator(struct Context* context) { + context->data[++context->dataNum] = context->heap; + context->heap += context->data[0]->allocate.size; + return; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/meta_connector/example/origin_cs.h Mon Nov 02 04:02:13 2015 +0900 @@ -0,0 +1,14 @@ +__code start_code(); +__code exit_code(); +extern __code meta(); + +__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); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/meta_connector/example/synchronizedQueue.c Mon Nov 02 04:02:13 2015 +0900 @@ -0,0 +1,222 @@ +#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_stub(struct Context* context) { + double ccc; + goto code1(context, &context->data[Allocate]->allocate); +} + +__code code1(struct Context* context, struct Allocate* allocate) { + allocate->size = sizeof(long); + allocator(context); + goto meta(context, Code2); +} + + +__code code2(struct Context* context, long* count) { + *count = 0; + goto meta(context, Code3); +} + +__code code2_stub(struct Context* context) { + goto code2(context, &context->data[Count]->count); +} + +__code code3(struct Context* context, long* count, struct Allocate* allocate) { + long loop = *count; + if(loop == NUM) { + goto meta(context, Thread_exit); + } + allocate->size = sizeof(struct Element); + allocator(context); + goto meta(context, Code4); +} + +__code code3_stub(struct Context* context) { + goto code3(context, &context->data[Count]->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[Count]->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[Count]->count); +} + +__code code7(struct Context* context, long* count, struct Allocate* allocate) { + long loop = *count; + if(loop == NUM) { + goto meta(context, Thread_exit); + } + (*count)++; + allocate->after_get = Code7; + goto meta(context, Receiver); +} + +__code code7_stub(struct Context* context) { + goto code7(context, &context->data[Count]->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); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/meta_connector/example/synchronizedQueueConnected.c Mon Nov 02 04:02:13 2015 +0900 @@ -0,0 +1,251 @@ +#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); +} + +/*-- generated by script */ +// __code code1(struct Allocate* allocate) { +__code code1(struct Context* context, struct Allocate* allocate) { + allocate->size = sizeof(long); + allocator(context); +/*-- connected by script */ +// goto code2(); +goto meta(context, Code2); +} + +__code code1_stub(struct Context* context) { + goto code1(context, &context->data[Allocate]->allocate); +} + +/*-- generated by script */ +// __code code2(long* count) { +__code code2(struct Context* context, long* count) { + *count = 0; +/*-- connected by script */ +// goto code3(); +goto meta(context, Code3); +} + +__code code2_stub(struct Context* context) { + goto code2(context, &context->data[Count]->count); +} +/*-- generated by script */ +// __code code3(long* count, struct Allocate* allocate) { +__code code3(struct Context* context, long* count, struct Allocate* allocate) { + long loop = *count; + if(loop == NUM) { +/*-- connected by script */ +// goto thread_exit(); +goto meta(context, Thread_exit); + } + allocate->size = sizeof(struct Element); + allocator(context); +/*-- connected by script */ +// goto code4(); +goto meta(context, Code4); +} + +/*-- generated by script */ +__code code3_stub(struct Context* context) { + goto code3(context, &context->data[Count]->count, &context->data[Allocate]->allocate); +} +// __code code4(long* count, struct Allocate* allocate, struct Element* element) { +__code code4(struct Context* context, long* count, struct Allocate* allocate, struct Element* element) { + allocate->after_put = Code3; + element->value = (*count)++; +/*-- connected by script */ +// goto sender(); +goto meta(context, Sender); +} + +__code code4_stub(struct Context* context) { + goto code4(context, &context->data[Count]->count, &context->data[Allocate]->allocate, &context->data[context->dataNum]->element); +} + +/*-- generated by script */ + +__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); +} + +/*-- generated by script */ +// __code code5(struct Allocate* allocate) { +__code code5(struct Context* context, struct Allocate* allocate) { + allocate->size = sizeof(long); + allocator(context); +/*-- connected by script */ +// goto code6(); +goto meta(context, Code6); +} + +__code code5_stub(struct Context* context) { + goto code5(context, &context->data[Allocate]->allocate); +} + +/*-- generated by script */ +// __code code6(long* count) { +__code code6(struct Context* context, long* count) { + *count = 0; +/*-- connected by script */ +// goto code7(); +goto meta(context, Code7); +} + +__code code6_stub(struct Context* context) { + goto code6(context, &context->data[Count]->count); +} + +/*-- generated by script */ +// __code code7(long* count, struct Allocate* allocate) { +__code code7(struct Context* context, long* count, struct Allocate* allocate) { + long loop = *count; + if(loop == NUM) { + goto meta(context, Thread_exit); + } + (*count)++; + allocate->after_get = Code7; +/*-- connected by script */ +// goto receiver(); +goto meta(context, Receiver); +} + +__code code7_stub(struct Context* context) { + goto code7(context, &context->data[Count]->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); +} + +/*-- generated by script */ +// __code receiver(struct Queue* queue) { +__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, enum Code next) { + pthread_mutex_unlock(&context->data[Queue]->queue.mutex); + goto (context->code[next])(context); +} + +/*-- generated by script */ +// __code get(struct Allocate* allocate, struct Queue* queue, struct Element* element) { +__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 get_stub(struct Context* context) { + goto get(context, &context->data[Allocate]->allocate, &context->data[Queue]->queue, &context->data[context->dataNum]->element); +} + +/*-- generated by script */ +// __code thread_exit() { +__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); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/meta_connector/example/synchronizedQueueContext.c Mon Nov 02 04:02:13 2015 +0900 @@ -0,0 +1,65 @@ +#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[Thread_exit] = 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; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/meta_connector/example/synchronizedQueueContext.h Mon Nov 02 04:02:13 2015 +0900 @@ -0,0 +1,66 @@ +/* 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, + Thread_exit, + Exit, +}; + +enum UniqueData { + Allocate, + Queue, + Count, + 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; +};
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/meta_connector/example/synchronizedQueueIdeal.c Mon Nov 02 04:02:13 2015 +0900 @@ -0,0 +1,182 @@ +#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 Allocate* allocate) { + allocate->size = sizeof(long); + allocator(context); + goto code2(); +} + +__code code2(long* count) { + *count = 0; + goto code3(); +} + +__code code3(long* count, struct Allocate* allocate) { + long loop = *count; + if(loop == NUM) { + goto thread_exit(); + } + allocate->size = sizeof(struct Element); + allocator(context); + goto code4(); +} + +__code code4(long* count, struct Allocate* allocate, struct Element* element) { + allocate->after_put = Code3; + element->value = (*count)++; + goto sender(); +} + +__code code4_stub(struct Context* context) { + goto code4(context, &context->data[Count]->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 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_stub(struct Context* context) { + goto put(context, &context->data[Allocate]->allocate, &context->data[Queue]->queue, &context->data[context->dataNum]->element); +} + +__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 Allocate* allocate) { + allocate->size = sizeof(long); + allocator(context); + goto code6(); +} + +__code code5_stub(struct Context* context) { + goto code5(context, &context->data[Allocate]->allocate); +} + +__code code6(long* count) { + *count = 0; + goto code7(); +} + +__code code7(long* count, struct Allocate* allocate) { + long loop = *count; + if(loop == NUM) { + goto meta(context, Thread_exit); + } + (*count)++; + allocate->after_get = Code7; + goto receiver(); +} + +__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 Queue* queue) { + goto meta_receiver(context, queue, Get); +} + +__code meta_get(struct Context* context, enum Code next) { + pthread_mutex_unlock(&context->data[Queue]->queue.mutex); + goto (context->code[next])(context); +} + +__code get(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 get_stub(struct Context* context) { + goto get(context, &context->data[Allocate]->allocate, &context->data[Queue]->queue, &context->data[context->dataNum]->element); +} + +__code thread_exit() { + 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); +}
--- a/meta_connector/meta_connector.py Sun Nov 01 18:00:20 2015 +0900 +++ b/meta_connector/meta_connector.py Mon Nov 02 04:02:13 2015 +0900 @@ -98,7 +98,7 @@ file.write(');\n}\n') def connect_meta(file, lines, l, caller_name, callee_name): - meta_name = 'meta_'+caller_name if cs_search(lines, 'meta_'+callee_name) else 'meta' + meta_name = 'meta_'+caller_name if cs_search(lines, 'meta_'+caller_name) else 'meta' args = get_args_from_line(l) file.write("/*-- connected by script */\n") file.write('// '+l)
--- a/meta_connector/synchronizedQueueIdeal.c Sun Nov 01 18:00:20 2015 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,182 +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(context); - long* count = &context->data[Counter]->count - goto code2(count) -} - -__code code2(struct Context* context, long* count) { - *count = 0; - struct Allocate* allocate = &context->data[Allocate]->allocate; - 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); - struct Element* element = &context->data[context->dataNum]->element) - 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 meta_sender(context, queue, Put); -} - -__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(context); - 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 meta(context, ThreadExit); - } - (*count)++; - allocate->after_get = Code7; - struct Queue* queue = &context->data[Queue]->queue; - 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 meta_receiver(context, queue, Get); -} - -__code meta_get(struct Context* context, enum Code next) { - pthread_mutex_unlock(&context->data[Queue]->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); -}