Mercurial > hg > Members > kent > CbCTaskManager
diff Task.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 | 803d6bf22e6d |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Task.cbc Fri Dec 18 21:57:05 2009 +0900 @@ -0,0 +1,107 @@ + + +typedef __code (*Scheduler)(); +typedef __code (*Taskrun)(Scheduler, void*, void*); + +typedef struct _Task { + Taskrun run; + + TaskList waiter; // List of tasks waiting for me :-P + TaskList waitee; // List of tasks keep me waiting :-( +} Task; + + +typedef struct _TaskList { + Task *task; + struct _TaskList *next; + struct _TaskList *prev; +} TaskList; + +/* + * use this function like below. + * list = listAddFirst(list, task); + */ +TaskList * +listAddFirst(TaskList* top, Task *task) +{ + if (!top) { + newlist = malloc(sizeof(struct _TaskList)); + newlist->task = task; + newlist->next = newlist; + newlist->prev = newlist; + return newlist; + } + TaskList *last = top->prev; + + newlist = malloc(sizeof(struct _TaskList)); + newlist->task = task; + newlist->next = top; + newlist->prev = last; + + top->prev = newlist; + last->next = newlist; + return newlist; +} + +TaskList * +listAddLast(TaskList* top, Task *task) +{ + /* + if (!top) { + newlist = malloc(sizeof(struct _TaskList)); + newlist->task = task; + newlist->next = newlist; + newlist->prev = newlist; + return newlist; + } + TaskList *last = top->prev; + + newlist = malloc(sizeof(struct _TaskList)); + newlist->task = task; + newlist->next = top; + newlist->prev = last; + + top->prev = newlist; + last->next = newlist; + return newlist; + */ +} + +TaskList * +listRemove(TaskList *top, Task *task) +{ + TaskList *t; + if (top->task==task) { + if (top->next==top) { + free(top); + return NULL; + } + TaskList *newtop = top->next; + top->next->prev = top->prev; + top->prev->next = top->next; + free(top); + return newtop; + } + for (t=top->next; t!=top; t=t->next) { + if (t->task==task) { + t->next->prev = t->prev; + t->prev->next = t->next; + free(t); + return top; + } + } + return top; +} + +typedef int (*ApplyFn)(Task*,void*); + +listApply(TaskList *top, ApplyFn fn, void *arg) { + t = top; + do { + fn(t->task, arg); + t = t->next; + } while ( t!=top ); +} + + +