aboutsummaryrefslogtreecommitdiff
path: root/later/schedule.c
blob: 8cf2780277c72c967966b777f8f2861643caaa1e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include "../sys/schedule.h"
#include "../lib/ll.h"
#include "../lib/q.h"

#ifdef IGNORE
#ifdef FLAT
static struct Task* task_list[256];

static struct Scheduler scheduler = {
	.tasks = task_list,
};

static unsigned int ntask_i = 0;

void add_task(struct Task* t)
{
	scheduler.tasks[ntask_i] = t;
	ntask_i += 1;
	if (ntask_i > 256) {
		ntask_i = 0;
	}
}

unsigned int get_task_length(void)
{
	return ntask_i;
}

void execute_task(void)
{
	if (scheduler.tasks[ntask_i-1] != 0)
		scheduler.tasks[ntask_i-1]->task();
}
#elseif LL
static struct LL bl = {
	.prev = 0,
	.next = 0,
};
static struct Scheduler scheduler = {
	.tasks = &bl,
};
#else
static struct Q_base bq = {
	.next = 0,
	.last = 0,
};
static struct Scheduler  scheduler = {
	.tasks = &bq,
};

void add_task(struct Task* t)
{
	pushq(scheduler.tasks, t);
}

unsigned int get_task_length(void)
{
	unsigned int length = 0;
	if (scheduler.tasks->last == 0)
		return length;
	else if (scheduler.tasks->next == scheduler.tasks->last)
		return 1;
	else {
		struct Q* q = scheduler.tasks->next;
		length += 1;
		while (q->next != 0) {
			q = q->next;
			length += 1;
		}
		return length;
	}
}

void execute_task(void)
{
	if (scheduler.tasks->last != 0) {
		struct Task* tsk = (struct Task*)scheduler.tasks->next->data;
		(tsk->task)();
		popq(scheduler.tasks);
	}
}
#endif
#endif