Mercurial > hg > GearsTemplate
diff src/tmp/main.c @ 86:e06e1a9e569e parallel_execution
create worker
author | Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp> |
---|---|
date | Mon, 18 Jan 2016 17:50:52 +0900 |
parents | |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/tmp/main.c Mon Jan 18 17:50:52 2016 +0900 @@ -0,0 +1,81 @@ +#include <stdio.h> +#include <unistd.h> + +#include "context.h" +#include "origin_cs.h" + +extern __code initContext(struct Context* context); + +int a; + +__code code1(struct Context* context) { + if(__sync_bool_compare_and_swap(&a, 10, 0)) { + puts("success"); + a = 10; + } else { + puts("failure"); + goto meta(context, Code1); + } +} + +__code code1_stub(struct Context* context) { + goto code1(context); +} + +__code createWorker(struct Context* context, struct LoopCounter* loopCounter, struct Worker* worker) { + int i = loopCounter->i; + + if (i < worker->num) { + struct Context* worker_context = &worker->contexts[i]; + worker_context->next = Code1; + pthread_create(&worker_context->thread, NULL, (void*)&start_code, worker_context); + loopCounter->i++; + + goto meta(context, CreateWorker); + } + + loopCounter->i = 0; + goto meta(context, TaskManager); +} + +__code createWorker_stub(struct Context* context) { + goto createWorker(context, &context->data[LoopCounter]->loopCounter, &context->data[Worker]->worker); +} + +__code taskManager(struct Context* context, struct LoopCounter* loopCounter, struct Worker* worker) { + int i = loopCounter->i; + + if (i < worker->num) { + pthread_join(worker->contexts[i].thread, NULL); + loopCounter->i++; + + goto meta(context, TaskManager); + } + + loopCounter->i = 0; + goto meta(context, Exit); +} + +__code taskManager_stub(struct Context* context) { + goto taskManager(context, &context->data[LoopCounter]->loopCounter, &context->data[Worker]->worker); +} + +int main(int argc, char** argv) { + a = 10; + int cpu_num = (int)atoi(argv[1]); + + struct Context* main_context = (struct Context*)malloc(sizeof(struct Context)); + initContext(main_context); + main_context->next = CreateWorker; + + struct Context* worker_contexts = (struct Context*)malloc(sizeof(struct Context)*cpu_num); + + struct Worker* worker = &main_context->data[Worker]->worker; + worker->num = cpu_num; + worker->contexts = worker_contexts; + + for (int i = 0;i<cpu_num;i++) + initContext(&worker_contexts[i]); + + goto start_code(main_context); +}