0
|
1
|
|
2
|
|
3 typedef __code (*Scheduler)();
|
|
4 typedef __code (*Taskrun)(Scheduler, void*, void*);
|
|
5
|
|
6 typedef struct _Task {
|
|
7 Taskrun run;
|
|
8
|
|
9 TaskList waiter; // List of tasks waiting for me :-P
|
|
10 TaskList waitee; // List of tasks keep me waiting :-(
|
|
11 } Task;
|
|
12
|
|
13
|
|
14 typedef struct _TaskList {
|
|
15 Task *task;
|
|
16 struct _TaskList *next;
|
|
17 struct _TaskList *prev;
|
|
18 } TaskList;
|
|
19
|
|
20 /*
|
|
21 * use this function like below.
|
|
22 * list = listAddFirst(list, task);
|
|
23 */
|
|
24 TaskList *
|
|
25 listAddFirst(TaskList* top, Task *task)
|
|
26 {
|
|
27 if (!top) {
|
|
28 newlist = malloc(sizeof(struct _TaskList));
|
|
29 newlist->task = task;
|
|
30 newlist->next = newlist;
|
|
31 newlist->prev = newlist;
|
|
32 return newlist;
|
|
33 }
|
|
34 TaskList *last = top->prev;
|
|
35
|
|
36 newlist = malloc(sizeof(struct _TaskList));
|
|
37 newlist->task = task;
|
|
38 newlist->next = top;
|
|
39 newlist->prev = last;
|
|
40
|
|
41 top->prev = newlist;
|
|
42 last->next = newlist;
|
|
43 return newlist;
|
|
44 }
|
|
45
|
|
46 TaskList *
|
|
47 listAddLast(TaskList* top, Task *task)
|
|
48 {
|
|
49 /*
|
|
50 if (!top) {
|
|
51 newlist = malloc(sizeof(struct _TaskList));
|
|
52 newlist->task = task;
|
|
53 newlist->next = newlist;
|
|
54 newlist->prev = newlist;
|
|
55 return newlist;
|
|
56 }
|
|
57 TaskList *last = top->prev;
|
|
58
|
|
59 newlist = malloc(sizeof(struct _TaskList));
|
|
60 newlist->task = task;
|
|
61 newlist->next = top;
|
|
62 newlist->prev = last;
|
|
63
|
|
64 top->prev = newlist;
|
|
65 last->next = newlist;
|
|
66 return newlist;
|
|
67 */
|
|
68 }
|
|
69
|
|
70 TaskList *
|
|
71 listRemove(TaskList *top, Task *task)
|
|
72 {
|
|
73 TaskList *t;
|
|
74 if (top->task==task) {
|
|
75 if (top->next==top) {
|
|
76 free(top);
|
|
77 return NULL;
|
|
78 }
|
|
79 TaskList *newtop = top->next;
|
|
80 top->next->prev = top->prev;
|
|
81 top->prev->next = top->next;
|
|
82 free(top);
|
|
83 return newtop;
|
|
84 }
|
|
85 for (t=top->next; t!=top; t=t->next) {
|
|
86 if (t->task==task) {
|
|
87 t->next->prev = t->prev;
|
|
88 t->prev->next = t->next;
|
|
89 free(t);
|
|
90 return top;
|
|
91 }
|
|
92 }
|
|
93 return top;
|
|
94 }
|
|
95
|
|
96 typedef int (*ApplyFn)(Task*,void*);
|
|
97
|
|
98 listApply(TaskList *top, ApplyFn fn, void *arg) {
|
|
99 t = top;
|
|
100 do {
|
|
101 fn(t->task, arg);
|
|
102 t = t->next;
|
|
103 } while ( t!=top );
|
|
104 }
|
|
105
|
|
106
|
|
107
|