comparison schedular/main.c @ 4:ac10727fee49 default tip

Parallel execution
author Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
date Tue, 10 May 2016 16:39:06 +0900
parents 0410aa49eff5
children
comparison
equal deleted inserted replaced
3:51453d58fb8f 4:ac10727fee49
1 #include<stdio.h> 1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <pthread.h>
2 4
3 __code hello() { 5 enum ThreadType {
4 printf("hello\n"); 6 MainThread,
7 SchedularThread,
8 WorkerThread,
9 };
10
11 enum CodeSegment {
12 Schedular,
13 Worker,
14 CountUp,
15 CountStop,
16 Exit,
17 };
18
19 struct Context {
20 enum ThreadType threadType;
21 unsigned int threadId;
22 __code (**code) (struct Context*);
23
24 union SubContext {
25 struct MainContext {
26 unsigned int threadNum;
27 pthread_t schedularThread;
28 struct Context *schedularContext;
29 } main;
30 struct WorkerContext {
31 unsigned int count;
32 } worker;
33 struct SchedularContext {
34 pthread_t *threads;
35 } schedular;
36 } *subContext;
37 };
38
39 __code meta(struct Context* context, enum CodeSegment next) {
40 goto (context->code[next])(context);
5 } 41 }
6 42
43 __code countUp(struct Context* context) {
44 if (context->subContext->worker.count >= 5) goto meta(context, CountStop);
45
46 printf("threadId %u, count %u\n", context->threadId, context->subContext->worker.count);
47 context->subContext->worker.count++;
48
49 goto meta(context, CountUp);
50 }
51
52 __code countStop(struct Context* context) {
53 printf("count stop: threadId %u\n", context->threadId);
54 }
55
56 __code worker(struct Context* context) {
57 goto meta(context, CountUp);
58 }
59
60 __code schedular(struct Context* context) {
61 for (unsigned int i = 0; i < 100; i++) {
62 printf("schedular %u\n", i);
63 }
64 }
65
66 __code initFinish(struct Context* mainContext, enum CodeSegment next) {
67 pthread_join(mainContext->subContext->main.schedularThread, NULL);
68 goto (mainContext->code[next])(mainContext);
69 }
70
71 __code initWorkers(struct Context* mainContext, enum CodeSegment next) {
72 struct Context* schedularContext = mainContext->subContext->main.schedularContext;
73 struct Context* workerContext;
74
75 for (unsigned int i = 0; i < mainContext->subContext->main.threadNum; i++) {
76 workerContext = malloc(sizeof(struct Context));
77 workerContext->code = mainContext->code;
78 workerContext->threadType = WorkerThread;
79 workerContext->threadId = i;
80 workerContext->subContext = malloc(sizeof(struct WorkerContext));
81 workerContext->subContext->worker.count = 0;
82
83 pthread_create(schedularContext->subContext->schedular.threads+i, NULL, (void*)worker, workerContext);
84
85 }
86 goto initFinish(mainContext, next);
87 }
88
89
90 __code initSchedular(struct Context* mainContext, enum CodeSegment next) {
91 struct Context* schedularContext = (struct Context*)malloc(sizeof(struct Context));
92
93 schedularContext->code = mainContext->code;
94 schedularContext->threadType = SchedularThread;
95 schedularContext->subContext = malloc(sizeof(struct SchedularContext));
96 schedularContext->subContext->schedular.threads = malloc(sizeof(pthread_t) * mainContext->subContext->main.threadNum);
97 mainContext->subContext->main.schedularContext = schedularContext;
98
99 pthread_create(&(mainContext->subContext->main.schedularThread), NULL, (void*)schedular, schedularContext);
100
101 goto initWorkers(mainContext, next);
102 }
103
104 __code init(enum CodeSegment next) {
105 struct Context* context = (struct Context*)malloc(sizeof(struct Context));
106
107 context->code = malloc(sizeof(__code (**)(struct Context*)) * Exit);
108 context->code[Worker] = worker;
109 context->code[CountUp] = countUp;
110 context->code[CountStop] = countStop;
111
112 context->threadType = MainThread;
113 context->threadId = 999;
114 context->subContext = malloc(sizeof(struct MainContext));
115 context->subContext->main.threadNum = 1;
116
117 goto initSchedular(context, next);
118 }
119
120
7 int main(int argc, char const* argv[]) { 121 int main(int argc, char const* argv[]) {
8 goto hello(); 122 goto init(Worker);
9 } 123 }