view TaskManager/kernel/ppe/TaskInfo.cc @ 6:39ce245235d4

*** empty log message ***
author gongo
date Wed, 06 Feb 2008 11:41:08 +0900
parents 75f184d16fa5
children 7aa4c006e4be
line wrap: on
line source

#include <stdio.h>
#include <stdlib.h>
#include "TaskInfo.h"

TaskInfo::TaskInfo(int num)
{
    machineNum = num;
}

/**
 * initialize pool of task_pack_list
 * @return if (success) ? 0 : -1
 */
int
TaskInfo::init_taskList(int num)
{
    if (!taskListPool) {
	return extend_pool_taskList(num);
    }
    return 0;
}

/**
 * @return free list from pool of task_pack_list
 */
TaskListPtr
TaskInfo::get_free_taskList(void)
{
    TaskListPtr q;

    if (!freeTaskList) {
	extend_pool_taskList(10);
    }
    q = freeTaskList;
    freeTaskList = freeTaskList->next;

    q->length = 0;
    q->next = 0;

    return q;
}

/**
 * initialize pool of task_queue
 * @return if (success) ? 0 : -1
 */
int
TaskInfo::init_taskQueue(int num)
{
    if (!taskQueuePool) {
	return extend_pool_taskQueue(num);
    }
    return 0;
}

int
TaskInfo::extend_pool_taskQueue(int num)
{
    TaskQueuePtr q = NULL;

    q = (TaskQueuePtr)malloc(sizeof(TaskQueue)*(num+1));

    if (q == NULL) {
	return -1;
    }
    q->next = taskQueuePool;
    taskQueuePool = q;

    /* Connect all free queue in the pool */
    for (q = taskQueuePool + 1; --num > 0; q++) {
	q->next = q + 1;
    }
    q->next = freeTaskQueue;
    freeTaskQueue = taskQueuePool + 1;

    return 0;
}

TaskQueuePtr
TaskInfo::get_free_taskQueue(HTaskPtr task)
{
    TaskQueuePtr q;
 
    if (!freeTaskQueue) {
	extend_pool_taskQueue(100);
    }
    q = freeTaskQueue;
    freeTaskQueue = freeTaskQueue->next;

    q->task = task;
    q->next = NULL;

    return q;
}


int
TaskInfo::init_task(int num)
{
    if (!taskPool) {
	return extend_pool_task(num);
    }
    return 0;
}

int
TaskInfo::extend_pool_task(int num)
{
    HTaskPtr q = NULL;

    q = (HTaskPtr)malloc(sizeof(HTask)*(num+1));

    if (q == NULL) {
	return -1;
    }
    q->next = taskPool;
    taskPool = q;

    /* Connect all free queue in the pool */
    for (q = taskPool + 1; --num > 0; q++) {
	q->next = q + 1;
    }
    q->next = freeTask;
    freeTask = taskPool + 1;

    return 0;
}

HTaskPtr
TaskInfo::get_free_task(int cmd, int size,
			unsigned long long in_addr,
			unsigned long long out_addr)
{
    HTaskPtr q;
 
    if (!freeTask) {
	extend_pool_task(100);
    }
    q = freeTask;
    freeTask = freeTask->next;

    q->command  = cmd;
    q->in_addr  = in_addr;
    q->out_addr = out_addr;
    q->in_size  = size;
    q->wait_me  = NULL;
    q->wait_i   = NULL;
    q->post_func = NULL;

    return q;
}

void
TaskInfo::free_taskList(TaskListPtr q)
{
    q->next = freeTaskList;
    freeTaskList = q;
}

void
TaskInfo::free_taskQueue(TaskQueuePtr q)
{
    q->next = freeTaskQueue;
    freeTaskQueue = q;
}

void
TaskInfo::free_task(HTaskPtr q)
{
    q->next = freeTask;
    freeTask = q;
}

/**
 * [task] is added to active_task
 */
void
TaskInfo::append_activeTask(HTaskPtr task)
{
    TaskQueuePtr q;

    q = get_free_taskQueue(task);
    activeTaskQueue = append_taskQueue(activeTaskQueue, q);
}

/**
 * [task] is added to wait_task
 */
void
TaskInfo::append_waitTask(HTaskPtr task)
{
    TaskQueuePtr q;

    q = get_free_taskQueue(task);
    waitTaskQueue = append_taskQueue(waitTaskQueue, q);
}

void
TaskInfo::check_task_finish(HTaskPtr task)
{
    notify_wait_taskQueue(task, task->wait_me);
    task->post_func();

    free_task(task);
}

void
TaskInfo::notify_wait_taskQueue(HTaskPtr depend, TaskQueuePtr list)
{
    TaskQueuePtr p, d;
    HTaskPtr task;
    
    p = list; // wait task list

    while (p) {
	task = p->task;
	task->wait_i = remove_taskQueue_eq_task(task->wait_i, depend);
	if (task->wait_i == NULL) {
	    d = p;
	    p = p->next;
	    append_activeTask(task);
	    waitTaskQueue = remove_taskQueue(waitTaskQueue, d);
	} else {
	    p = p->next;
	}
    }
}

TaskQueuePtr
TaskInfo::remove_taskQueue_eq_task(TaskQueuePtr list, HTaskPtr task)
{
    TaskQueuePtr p = list;
    TaskQueuePtr p1;

    if (!p) return p;

    if (p->task == task) {
	list = list->next;
	free_taskQueue(p);
    } else {
	p1 = p->next;
	while (p1 && p1->task && p1->task != task) {
	    p1 = p1->next;
	    p = p->next;
	}
	if (p1) {
	    p->next = p1->next;
	    free_taskQueue(p1);
	}
    }

    return list;    
}

TaskQueuePtr
TaskInfo::remove_taskQueue(TaskQueuePtr list, TaskQueuePtr q)
{
    TaskQueuePtr p = list;
    TaskQueuePtr p1;

    if (!p) return p;

    if (p == q) {
	list = list->next;
	free_taskQueue(p);
    } else {
	p1 = p->next;
	while (p1 && p1 != q) {
	    p1 = p1->next;
	    p = p->next;
	}
	if (p1) {
	    p->next = p1->next;
	    free_taskQueue(p1);
	}
    }

    return list;    
}

TaskListPtr
append_taskList(TaskListPtr list, TaskListPtr q)
{
    TaskListPtr p = list;

    if (!p) {
	return q;
    } else {
	while (p->next) p = p->next;
	p->next = q;
	return list;
    }
}

TaskQueuePtr
append_taskQueue(TaskQueuePtr list, TaskQueuePtr q)
{
    TaskQueuePtr p = list;

    if (!p) {
	return q;
    } else {
	while(p->next) p = p->next;
	p->next = q;
	return list;
    }
}