# HG changeset patch # User Yasutaka Higa # Date 1462865946 -32400 # Node ID ac10727fee49ddc86c81202bbab0ad3a63796732 # Parent 51453d58fb8f34c1a9c8b88f694f88d721027f26 Parallel execution diff -r 51453d58fb8f -r ac10727fee49 schedular/main.c --- a/schedular/main.c Tue May 10 13:54:39 2016 +0900 +++ b/schedular/main.c Tue May 10 16:39:06 2016 +0900 @@ -1,9 +1,123 @@ -#include +#include +#include +#include + +enum ThreadType { + MainThread, + SchedularThread, + WorkerThread, +}; + +enum CodeSegment { + Schedular, + Worker, + CountUp, + CountStop, + Exit, +}; + +struct Context { + enum ThreadType threadType; + unsigned int threadId; + __code (**code) (struct Context*); -__code hello() { - printf("hello\n"); + union SubContext { + struct MainContext { + unsigned int threadNum; + pthread_t schedularThread; + struct Context *schedularContext; + } main; + struct WorkerContext { + unsigned int count; + } worker; + struct SchedularContext { + pthread_t *threads; + } schedular; + } *subContext; +}; + +__code meta(struct Context* context, enum CodeSegment next) { + goto (context->code[next])(context); +} + +__code countUp(struct Context* context) { + if (context->subContext->worker.count >= 5) goto meta(context, CountStop); + + printf("threadId %u, count %u\n", context->threadId, context->subContext->worker.count); + context->subContext->worker.count++; + + goto meta(context, CountUp); +} + +__code countStop(struct Context* context) { + printf("count stop: threadId %u\n", context->threadId); +} + +__code worker(struct Context* context) { + goto meta(context, CountUp); } +__code schedular(struct Context* context) { + for (unsigned int i = 0; i < 100; i++) { + printf("schedular %u\n", i); + } +} + +__code initFinish(struct Context* mainContext, enum CodeSegment next) { + pthread_join(mainContext->subContext->main.schedularThread, NULL); + goto (mainContext->code[next])(mainContext); +} + +__code initWorkers(struct Context* mainContext, enum CodeSegment next) { + struct Context* schedularContext = mainContext->subContext->main.schedularContext; + struct Context* workerContext; + + for (unsigned int i = 0; i < mainContext->subContext->main.threadNum; i++) { + workerContext = malloc(sizeof(struct Context)); + workerContext->code = mainContext->code; + workerContext->threadType = WorkerThread; + workerContext->threadId = i; + workerContext->subContext = malloc(sizeof(struct WorkerContext)); + workerContext->subContext->worker.count = 0; + + pthread_create(schedularContext->subContext->schedular.threads+i, NULL, (void*)worker, workerContext); + + } + goto initFinish(mainContext, next); +} + + +__code initSchedular(struct Context* mainContext, enum CodeSegment next) { + struct Context* schedularContext = (struct Context*)malloc(sizeof(struct Context)); + + schedularContext->code = mainContext->code; + schedularContext->threadType = SchedularThread; + schedularContext->subContext = malloc(sizeof(struct SchedularContext)); + schedularContext->subContext->schedular.threads = malloc(sizeof(pthread_t) * mainContext->subContext->main.threadNum); + mainContext->subContext->main.schedularContext = schedularContext; + + pthread_create(&(mainContext->subContext->main.schedularThread), NULL, (void*)schedular, schedularContext); + + goto initWorkers(mainContext, next); +} + +__code init(enum CodeSegment next) { + struct Context* context = (struct Context*)malloc(sizeof(struct Context)); + + context->code = malloc(sizeof(__code (**)(struct Context*)) * Exit); + context->code[Worker] = worker; + context->code[CountUp] = countUp; + context->code[CountStop] = countStop; + + context->threadType = MainThread; + context->threadId = 999; + context->subContext = malloc(sizeof(struct MainContext)); + context->subContext->main.threadNum = 1; + + goto initSchedular(context, next); +} + + int main(int argc, char const* argv[]) { - goto hello(); + goto init(Worker); }