diff options
author | Christian Cunningham <cc@localhost> | 2021-12-30 20:50:44 -0800 |
---|---|---|
committer | Christian Cunningham <cc@localhost> | 2021-12-30 20:50:44 -0800 |
commit | e15d41a5619c41f3440369e625d6d96921718221 (patch) | |
tree | 1291a6f4cb964d7143ab63626bc03ebefaff484b /src/sys | |
parent | 104c6fdd6b217b06183df7007882afa9183688bb (diff) |
Added Data Structures
Added basic scheduling
TODO: Preserve registers for context switch
Diffstat (limited to 'src/sys')
-rw-r--r-- | src/sys/core.c | 3 | ||||
-rw-r--r-- | src/sys/schedule.c | 81 | ||||
-rw-r--r-- | src/sys/schedule.h | 29 |
3 files changed, 113 insertions, 0 deletions
diff --git a/src/sys/core.c b/src/sys/core.c index f75ec35..ebf50a0 100644 --- a/src/sys/core.c +++ b/src/sys/core.c @@ -2,10 +2,13 @@ #include "../drivers/uart.h" #include "../graphics/drawer.h" #include "../graphics/lfb.h" +#include "../lib/ll.h" #include "../lib/mem.h" +#include "../lib/q.h" #include "../lib/strings.h" #include "../sys/core.h" #include "../sys/power.h" +#include "../sys/schedule.h" #include "../sys/timer.h" #include "../util/mutex.h" #include "../util/time.h" diff --git a/src/sys/schedule.c b/src/sys/schedule.c new file mode 100644 index 0000000..f920b0e --- /dev/null +++ b/src/sys/schedule.c @@ -0,0 +1,81 @@ +#include "../sys/schedule.h" +#include "../lib/ll.h" +#include "../lib/q.h" + +#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 diff --git a/src/sys/schedule.h b/src/sys/schedule.h new file mode 100644 index 0000000..04dad6a --- /dev/null +++ b/src/sys/schedule.h @@ -0,0 +1,29 @@ +#ifndef SYS_SCHEDULE_H +#define SYS_SCHEDULE_H + +struct Task { + unsigned char priority; + void (*task)(void); +}; + +#ifdef FLAT +struct Scheduler { + struct Task** tasks; +}; +#elseif LL +#include "../lib/ll.h" +struct Scheduler { + struct LL* tasks; +}; +#else +#include "../lib/q.h" +struct Scheduler { + struct Q_base* tasks; +}; +#endif + +void add_task(struct Task*); +unsigned int get_task_length(void); +void execute_task(void); + +#endif |