aboutsummaryrefslogtreecommitdiff
path: root/src/sys/schedule.q.c
blob: 88488da48ad5b4eb14bcea349997acb0b057cd05 (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
#if !(defined(LL) || defined(FLAT))
#include "../sys/schedule.h"
#include "../lib/q.h"
#include "../lib/mem.h"

static struct Q_base bq = {
	.next = 0,
	.last = 0,
};
static struct Scheduler  scheduler = {
	.tasks = &bq,
};

void add_fxn(void (*task)(void), unsigned char priority)
{
	struct Task* t = (struct Task*)malloc(sizeof(struct Task));
	t->priority = priority;
	t->task = task;
	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