comparison 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
comparison
equal deleted inserted replaced
83:c13575c3dbe9 86:e06e1a9e569e
1 #include <stdio.h>
2 #include <unistd.h>
3
4 #include "context.h"
5 #include "origin_cs.h"
6
7 extern __code initContext(struct Context* context);
8
9 int a;
10
11 __code code1(struct Context* context) {
12 if(__sync_bool_compare_and_swap(&a, 10, 0)) {
13 puts("success");
14 a = 10;
15 } else {
16 puts("failure");
17 goto meta(context, Code1);
18 }
19 }
20
21 __code code1_stub(struct Context* context) {
22 goto code1(context);
23 }
24
25 __code createWorker(struct Context* context, struct LoopCounter* loopCounter, struct Worker* worker) {
26 int i = loopCounter->i;
27
28 if (i < worker->num) {
29 struct Context* worker_context = &worker->contexts[i];
30 worker_context->next = Code1;
31 pthread_create(&worker_context->thread, NULL, (void*)&start_code, worker_context);
32 loopCounter->i++;
33
34 goto meta(context, CreateWorker);
35 }
36
37 loopCounter->i = 0;
38 goto meta(context, TaskManager);
39 }
40
41 __code createWorker_stub(struct Context* context) {
42 goto createWorker(context, &context->data[LoopCounter]->loopCounter, &context->data[Worker]->worker);
43 }
44
45 __code taskManager(struct Context* context, struct LoopCounter* loopCounter, struct Worker* worker) {
46 int i = loopCounter->i;
47
48 if (i < worker->num) {
49 pthread_join(worker->contexts[i].thread, NULL);
50 loopCounter->i++;
51
52 goto meta(context, TaskManager);
53 }
54
55 loopCounter->i = 0;
56 goto meta(context, Exit);
57 }
58
59 __code taskManager_stub(struct Context* context) {
60 goto taskManager(context, &context->data[LoopCounter]->loopCounter, &context->data[Worker]->worker);
61 }
62
63 int main(int argc, char** argv) {
64 a = 10;
65 int cpu_num = (int)atoi(argv[1]);
66
67 struct Context* main_context = (struct Context*)malloc(sizeof(struct Context));
68 initContext(main_context);
69 main_context->next = CreateWorker;
70
71 struct Context* worker_contexts = (struct Context*)malloc(sizeof(struct Context)*cpu_num);
72
73 struct Worker* worker = &main_context->data[Worker]->worker;
74 worker->num = cpu_num;
75 worker->contexts = worker_contexts;
76
77 for (int i = 0;i<cpu_num;i++)
78 initContext(&worker_contexts[i]);
79
80 goto start_code(main_context);
81 }