changeset 1:aef83aed7a07

scheduler test success.
author kent <kent@cr.ie.u-ryukyu.ac.jp>
date Sun, 20 Dec 2009 20:46:53 +0900
parents 5b089096921f
children 803d6bf22e6d
files .hgignore List.c TaskManager.cbc TaskManager.h TaskScheduler.cbc test/scheduler/test_sched.cbc
diffstat 6 files changed, 99 insertions(+), 41 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/.hgignore	Sun Dec 20 20:46:53 2009 +0900
@@ -0,0 +1,10 @@
+syntax: glob
+
+*.o
+.*.swp
+.*.swo
+GTAGS
+GRTAGS
+GSYMS
+GPATH
+
--- a/List.c	Fri Dec 18 21:57:05 2009 +0900
+++ b/List.c	Sun Dec 20 20:46:53 2009 +0900
@@ -63,6 +63,19 @@
 
 	return t->data;
 }
+void *
+_listGetLastData(List *top)
+{
+	if (!top) return NULL;
+	return top->prev->data;
+}
+
+void *
+_listMoveLasttoFirst(List *top)
+{
+	if (!top) return NULL;
+	return top->prev;
+}
 
 void
 _listApply(List *top, ApplyFn fn, void *arg) {
--- a/TaskManager.cbc	Fri Dec 18 21:57:05 2009 +0900
+++ b/TaskManager.cbc	Sun Dec 20 20:46:53 2009 +0900
@@ -67,12 +67,12 @@
 /* belows is Interfaces for Users.  */
 /* it may be replace to code segment.  but how?  */
 
-Task *newTask(int task_id)
+Task *newTask(TaskTypeID typeid)
 {
 	static int id=0;	// 今は使ってない… 使う?
 	task = allocate(sizeof(Task));
 
-	task->taskclass = taskclasses[task_id];
+	task->tasktype = tasktypes[typeid];
 	task->id = id;
 	task->waiter = NULL;
 	task->waitee = NULL;
--- a/TaskManager.h	Fri Dec 18 21:57:05 2009 +0900
+++ b/TaskManager.h	Sun Dec 20 20:46:53 2009 +0900
@@ -4,31 +4,18 @@
 #include "List.h"
 
 typedef List TaskList;
-// inline functionのがいいか
-
-
-typedef __code (*Taskrun)(__code(*)(void*,void*,void*), void*, void*);
-typedef __code (*Scheduler)(__code(*)(void*,void*,void*), void*, void*);
-//typedef __code (*Scheduler)(Taskrun, void*, void*);
-//typedef __code (*Taskrun)(Scheduler, void*, void*);
-#define END (NULL)
-
-enum cpu {
-	ANY = 0,
-	MAIN,
-	SUB,
-};
+typedef uint32_t TaskTypeID;
 
 /* C++CeriumでのTaskObjectにあたる  */
-typedef struct _taskclass {
-	Taskrun run;
+typedef struct _tasktype {
+	//Taskrun run;
 	char *name;
 	enum cpu cputype;
-} TaskClass;
+} TaskType;
 
-/* C++CeriumでのHTaskにあたる  */
+/* C++CeriumでのHTaskにあたるのかな  */
 typedef struct _Task {
-	TaskClass taskclass;
+	TaskType tasktype;
 	unsigned int id;	// is it needed?
 
 	void *rbuff;
@@ -40,7 +27,9 @@
 	TaskList waitee;	// List of tasks keep me waiting :-(
 } Task;
 
-extern int max_taskclass;		/* defined by USERs.  */
-extern TaskClass taskclasses[];	/* defined by USERs.  */
+/* defined in USERs space.  */
+/* but generated by Cerium automatically.  */
+extern int max_tasktypes;
+extern TaskType taskTypes[];
 
 #endif /* !_TASKMANAGER_H */
--- a/TaskScheduler.cbc	Fri Dec 18 21:57:05 2009 +0900
+++ b/TaskScheduler.cbc	Sun Dec 20 20:46:53 2009 +0900
@@ -4,6 +4,11 @@
 #include "TaskScheduler.h"
 #include "List.h"
 
+enum {
+	NOWAIT=0,
+	WAIT=1,
+};
+__code checkNewCode();
 
 typedef List SchedTaskList;
 #define addSchedTask(a,b) (SchedTaskList*)_listAddFirst((List*)(a),(void*)(b))
@@ -57,10 +62,15 @@
 selectCode()
 {
 	SchedTask *task;
-	task = _listGetnthData(schedule->runnable, 0);
-	schedule->running = task;
+	if (schedule->runnable) {
+		task = _listGetLastData(schedule->runnable);
+		schedule->running = task;
 
-	goto task->nextcode((void*)schedEntry, task->rbuff, task->wbuff);
+		goto task->nextcode((void*)schedEntry, task->rbuff, task->wbuff);
+	} else {
+		//goto checkNewCode();
+		goto checkNewCode(WAIT);
+	}
 }
 
 __code
@@ -76,10 +86,14 @@
 		schedule->running = NULL;
 		goto exitCode(id);
 	} else {
+		SchedTaskList list;
+		/* save the next code segment for the task.  */
 		schedule->running->nextcode = nextcode;
 		schedule->running->rbuff = rbuff;
 		schedule->running->wbuff = wbuff;
-		goto checkNewCode();
+		/* move last task to first to be fair.  */
+		schedule->runnable = _listMoveLasttoFirst(schedule->runnable);
+		goto checkNewCode(NOWAIT);
 	}
 }
 
--- a/test/scheduler/test_sched.cbc	Fri Dec 18 21:57:05 2009 +0900
+++ b/test/scheduler/test_sched.cbc	Sun Dec 20 20:46:53 2009 +0900
@@ -7,6 +7,7 @@
 #define __DEBUG(f, args...) \
 	fprintf(stderr, "in %s: "f, __FUNCTION__, ## args)
 
+__code task0(Scheduler scheduler, void *rbuff, void *wbuff);
 __code task1 (Scheduler scheduler, void *rbuff, void *wbuff);
 __code task2 (Scheduler scheduler, void *rbuff, void *wbuff);
 int main (int argc, char **argv);
@@ -35,7 +36,11 @@
 		}
 	}
 	__DEBUG("\n");
-	goto task1(scheduler, rbuff, wbuff);
+	__DEBUG("%p %p\n", rbuff, wbuff);
+	__DEBUG("%3.2lf %3.2lf %3.2lf\n", in[0],in[1],in[2]);
+	__DEBUG("%3.2lf %3.2lf %3.2lf\n", out[0],out[1],out[2]);
+	//goto task1(scheduler, wbuff, rbuff);
+	goto scheduler(task1, wbuff, rbuff);
 }
 __code
 task1(Scheduler scheduler, void *rbuff, void *wbuff)
@@ -45,8 +50,8 @@
 	double *out = wbuff;
 	double a[NUM][NUM] = {
 		{ 1.0, 0.0, 0.0 },
-		{ 0.0, 1.0, 0.0 },
-		{ 0.0, 0.0, 1.0 },
+		{ 0.0, 2.0, 0.0 },
+		{ 0.0, 0.0, 3.0 },
 	};
 	for (i=0; i<NUM; i++) {
 		out[i] = 0.0;
@@ -55,7 +60,11 @@
 		}
 	}
 	__DEBUG("\n");
-	goto task2(scheduler, rbuff, wbuff);
+	__DEBUG("%p %p\n", rbuff, wbuff);
+	__DEBUG("%3.2lf %3.2lf %3.2lf\n", in[0],in[1],in[2]);
+	__DEBUG("%3.2lf %3.2lf %3.2lf\n", out[0],out[1],out[2]);
+	//goto task2(scheduler, wbuff, rbuff);
+	goto scheduler(task2, wbuff, rbuff);
 }
 __code
 task2(Scheduler scheduler, void *rbuff, void *wbuff)
@@ -75,6 +84,9 @@
 		}
 	}
 	__DEBUG("\n");
+	__DEBUG("%p %p\n", rbuff, wbuff);
+	__DEBUG("%3.2lf %3.2lf %3.2lf\n", in[0],in[1],in[2]);
+	__DEBUG("%3.2lf %3.2lf %3.2lf\n", out[0],out[1],out[2]);
 	goto scheduler(END, rbuff, wbuff);
 }
 
@@ -88,26 +100,46 @@
 	return 0;
 }
 
-__code startcode(void *arg) {
-	double *rbuff, *wbuff;
-	rbuff = malloc(sizeof(double)*NUM);
-	wbuff = malloc(sizeof(double)*NUM);
+__code
+startcode(void *arg) {
+	double *readbuff, *writebuff;
+	readbuff = malloc(sizeof(double)*NUM);
+	writebuff = malloc(sizeof(double)*NUM);
 
-	goto addCode(0, task1, rbuff, wbuff);
+	readbuff[0] = 2.00;
+	readbuff[1] = 3.00;
+	readbuff[2] = 4.00;
+	__DEBUG("id=0 added.\n");
+	goto addCode(0, task0, readbuff, writebuff);
 }
 
 __code
-checkNewCode()
+checkNewCode(int wait)
 {
-	__DEBUG("\n");
+	static int t = 0;
+	double *readbuff, *writebuff;
+	readbuff = malloc(sizeof(double)*NUM);
+	writebuff = malloc(sizeof(double)*NUM);
+
+	if (wait) {
+		__DEBUG("all tasks finished\n");
+		exit(0);
+	}
+
+	if (t==0) {
+		t++;
+		__DEBUG("id=1 added.\n");
+		goto addCode(1, task0, readbuff, writebuff);
+	}
+	__DEBUG("no code added.\n");
 	goto selectCode();
 }
 
 __code
-exitCode()
+exitCode(int id)
 {
-	__DEBUG("exit\n");
-	exit(0);
+	__DEBUG("exit task%d\n", id);
+	goto selectCode();
 }
 
 void *