view TaskManager/kernel/ppe/HTaskInfo.cc @ 247:0098b5ff0d11

change example
author aaa
date Mon, 01 Jun 2009 19:33:09 +0900
parents 5c194c71eca8
children 58fd16298954
line wrap: on
line source

#include <stdio.h>
#include <stdlib.h>
#include "TaskManagerImpl.h"
#include "HTaskInfo.h"

HTaskInfo::HTaskInfo(void)
    :htaskPool(NULL), freeHTask(NULL) {}

HTaskInfo::~HTaskInfo(void) { destroy(); }

int
HTaskInfo::init(int num)
{
    if (htaskPool == NULL) {
	return extend_pool(num);
    }
    return 0;
}

/**
 * Fix me
 *   extend できる限界を設定するべき?
 */
int
HTaskInfo::extend_pool(int num)
{
    HTaskPtr q = NULL;

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

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

    /* Connect all free queue in the pool */
    for (q = htaskPool + 1; --num > 0; q++) {
	q->next = q + 1;
	q->inData = (ListDataPtr)malloc(sizeof(ListData));
	q->outData = (ListDataPtr)malloc(sizeof(ListData));
    }
    q->next = freeHTask;
    q->inData = (ListDataPtr)malloc(sizeof(ListData));
    q->outData = (ListDataPtr)malloc(sizeof(ListData));

    freeHTask = htaskPool + 1;

    return 0;
}

/**
 * Task をプールから取って来て返す
 *
 * @param [cmd] タスクコマンド
 */
HTaskPtr
HTaskInfo::create(int cmd)
{
    HTaskPtr q;
    
    if (freeHTask == NULL) {
	extend_pool(100);
    }

    q = freeHTask;
    freeHTask = freeHTask->next;

    q->command  = cmd;
    q->inData->clear();
    q->outData->clear();
    q->self = (unsigned int)q;
    q->param_size = 0;

    q->wait_me  = NULL;
    q->wait_i   = NULL;
    q->post_func = NULL;
    q->mimpl     = NULL;
    q->cpu_type  = CPU_PPE;

    return q;
}

void
HTaskInfo::free(HTaskPtr q)
{
    q->next = freeHTask;
    freeHTask = q;
}

void
HTaskInfo::destroy(void)
{
    HTaskPtr q, tmp; 

#if 1
    q = htaskPool;
    while (q) {
	tmp = q->next;
	free(q);
	q = tmp;
    }
#else
    for (q = htaskPool; q; q = q->next) {
	free(q);
    }
#endif

    freeHTask = htaskPool = NULL;
}