view src/parallel_execution/TaskManager.c @ 178:5077cf9bf54e

add TaskManager.c
author mir3636
date Tue, 29 Nov 2016 18:09:28 +0900
parents
children 57a11c15ff4c
line wrap: on
line source

#include "context.h"
#include "stack.h"
#include "origin_cs.h"
#include <stdio.h>

union Data* createTaskManager(struct Context* context) {
    struct TaskManager* taskManager = &ALLOCATE(context, TaskManager)->taskManager;
    struct TaskManager* taskManagerImpl = &ALLOCATE(context, TaskManagerImpl)->taskManagerImpl;

    taskManager->taskManager = (union Data*)taskManager;
    taskManagerImpl->top = NULL;
    taskManager->spawn = C_spawnTaskManager;
    taskManager->shutdown  = C_shutdownTaskManager;
    return (union Data*)(taskManager);
}

void printStack1(union Data* data) {
    struct Node* node = &data->element.data->node;
    if (node == NULL) {
        printf("NULL");
    } else {
        printf("key = %d ,", node->key);
        printStack1((union Data*)data->element.next);
    }
}

void printStack(union Data* data) {
    printStack1(data);
    printf("\n");
}

__code spawnTaskManager(struct Context* context, struct Task* task, struct Element* element, struct Queue* queue, struct Queue* activeQueue, struct Queue* waitQueue) {
    if (task->idsCount == 0) {
        // enqueue activeQueue
        queue->queue = (union Data*)activeQueue;
    } else {
        // enqueue waitQueue
        queue->queue = (union Data*)waitQueue;
    }
    queue->data = element->data;
    queue->next = context->next;
    goto meta(context, queue->queue->put);
}

__code spawnTaskManager_stub(struct Context* context) {
    goto spawnTask(context,
            context->data[D_Element]->element.data,
            &context->data[D_Element]->element,
            &context->data[D_ActiveQueue]->Queue,
            &context->data[D_WaitQueue]->Queue);
}

__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;

    Time *t = &context->data[D_Time]->Time;
    t->next = C_code2;
    goto meta(context, EndTime);
}

__code taskManager_stub(struct Context* context) {
    goto taskManager(context, &context->data[D_LoopCounter]->loopCounter, &context->data[D_Worker]->worker);
}

__code clearTaskManager(struct Context* context, struct SingleLinkedStack* stack,enum Code next) {
    stack->top = NULL;
    goto meta(context, next);
}

__code clearTaskManager_stub(struct Context* context) {
    goto clearTaskManager(context, (struct SingleLinkedStack *)context->data[D_Stack]->stack.stack->stack.stack, context->data[D_Stack]->stack.next);
}

__code pushTaskManager(struct Context* context, struct SingleLinkedStack* stack, struct Element* element, union Data* data, enum Code next) {
    element->next = stack->top;
    element->data = data;
    stack->top = element;
    goto meta(context, next);
}

__code pushTaskManager_stub(struct Context* context) {
    struct Element* element = &ALLOCATE(context, Element)->element;
    goto pushTaskManager(context,
                               (struct TaskManager *)context->data[D_Stack]->stack.stack->stack.stack,
                               element,
                               context->data[D_Stack]->stack.data,
                               context->data[D_Stack]->stack.next);
}

__code popTaskManager(struct Context* context, struct SingleLinkedStack* stack, union Data** data, enum Code next) {
    if (stack->top) {
        *data = stack->top->data;
        stack->top = stack->top->next;
    } else {
        *data = NULL;
    }
    goto meta(context, next);
}

__code popTaskManager_stub(struct Context* context) {
    goto popTaskManager(context,
                               (struct TaskManager *)context->data[D_Stack]->stack.stack->stack.stack,
                               &context->data[D_Stack]->stack.data,
                               context->data[D_Stack]->stack.next);
}

__code pop2TaskManager(struct Context* context, struct SingleLinkedStack* stack, union Data** data, union Data** data1, enum Code next) {
    if (stack->top) {
        *data = stack->top->data;
        stack->top = stack->top->next;
    } else {
        *data = NULL;
    }
    if (stack->top) {
        *data1 = stack->top->data;
        stack->top = stack->top->next;
    } else {
        *data1 = NULL;
    }
    goto meta(context, next);
}

__code pop2TaskManager_stub(struct Context* context) {
    goto pop2TaskManager(context,
                               (struct TaskManager *)context->data[D_Stack]->stack.stack->stack.stack,
                               &context->data[D_Stack]->stack.data,
                               &context->data[D_Stack]->stack.data1,
                               context->data[D_Stack]->stack.next);
}

__code getTaskManager(struct Context* context, struct SingleLinkedStack* stack, union Data** data, enum Code next) {
    if (stack->top)
        *data = stack->top->data;
    else
        *data = NULL;
    goto meta(context, next);
}

__code getTaskManager_stub(struct Context* context) {
    goto getTaskManager(context,
                               (struct TaskManager *)context->data[D_Stack]->stack.stack->stack.stack,
                               &context->data[D_Stack]->stack.data,
                               context->data[D_Stack]->stack.next);
}

__code get2TaskManager(struct Context* context, struct SingleLinkedStack* stack, union Data** data, union Data** data1, enum Code next) {
    if (stack->top) {
        *data = stack->top->data;
        if (stack->top->next) {
            *data1 = stack->top->next->data;
        } else {
            *data1 = NULL;
        }
    } else {
        *data = NULL;
        *data1 = NULL;
    }
    goto meta(context, next);
}

__code get2TaskManager_stub(struct Context* context) {
    goto get2TaskManager(context,
                               (struct TaskManager *)context->data[D_Stack]->stack.stack->stack.stack,
                               &context->data[D_Stack]->stack.data,
                               &context->data[D_Stack]->stack.data1,
                               context->data[D_Stack]->stack.next);
}

__code isEmptyTaskManager(struct Context* context, struct SingleLinkedStack* stack, enum Code next,enum Code whenEmpty) {
    if (stack->top)
        goto meta(context, next);
    else
        goto meta(context, whenEmpty);
}

__code isEmptyTaskManager_stub(struct Context* context) {
    goto isEmptyTaskManager(context,
                               (struct TaskManager *)context->data[D_Stack]->stack.stack->stack.stack,
                               context->data[D_Stack]->stack.next,
                               context->data[D_Stack]->stack.whenEmpty);
}