view TaskScheduler.cbc @ 0:5b089096921f

first commit.
author kent <kent@cr.ie.u-ryukyu.ac.jp>
date Fri, 18 Dec 2009 21:57:05 +0900
parents
children aef83aed7a07
line wrap: on
line source

//#include "Task.h"
#include <stdint.h>
#include <stdlib.h>
#include "TaskScheduler.h"
#include "List.h"


typedef List SchedTaskList;
#define addSchedTask(a,b) (SchedTaskList*)_listAddFirst((List*)(a),(void*)(b))
#define removeTask(a,b) (SchedTaskList*)_listRemove((List*)(a),(void*)(b))
// inline functionのがいいか

typedef struct _segment {
	ID id;  // task identifier;
	Taskrun nextcode;
	void *rbuff;
	void *wbuff;
} SchedTask;

typedef struct _scheduler {
	/* it may be Singleton.  */

	SchedTask *running;
	SchedTaskList *runnable;

} TaskScheduler;

__code schedEntry(Taskrun nextcode, void *rbuff, void *wbuff);

static TaskScheduler *schedule;

extern void *allocate(size_t);
__code
initScheduler(__code (*ret)(void *), void *arg)
{
	schedule = allocate(sizeof(TaskScheduler));
	schedule->runnable = NULL;
	schedule->running = NULL;
	goto ret(arg);
}

__code
addCode(ID id, Taskrun code0, void *rbuff, void *wbuff)
{
	SchedTask *newcs;
	newcs = allocate(sizeof(SchedTask));
	newcs->nextcode = code0;
	newcs->rbuff = rbuff; //taskの遷移で引数が変化しないならいならい
	newcs->wbuff = wbuff;

	schedule->runnable = addSchedTask(schedule->runnable, newcs);

	goto selectCode();
}

__code
selectCode()
{
	SchedTask *task;
	task = _listGetnthData(schedule->runnable, 0);
	schedule->running = task;

	goto task->nextcode((void*)schedEntry, task->rbuff, task->wbuff);
}

__code
schedEntry(Taskrun nextcode, void *rbuff, void *wbuff)
{
	/* nextcode==NULLならTaskは終わったと判断  */
	/* scheduled  */
	if ( nextcode==NULL ) {
		ID id = schedule->running->id;
		schedule->runnable =
			removeTask(schedule->runnable, schedule->running);
		free(schedule->running);
		schedule->running = NULL;
		goto exitCode(id);
	} else {
		schedule->running->nextcode = nextcode;
		schedule->running->rbuff = rbuff;
		schedule->running->wbuff = wbuff;
		goto checkNewCode();
	}
}