diff options
| author | Ada Christine <adachristine18@gmail.com> | 2022-01-17 12:23:44 +0000 |
|---|---|---|
| committer | Ada Christine <adachristine18@gmail.com> | 2022-01-17 12:23:44 +0000 |
| commit | 8c56039ca130dfef1f0bf204cc702eeb10801379 (patch) | |
| tree | 5f374b6f40f87fd356780c0d88e01651d1efd1a9 /kc/core/task.c | |
| parent | c5fcdec761211ef74ba84a8b41f34f073177cfab (diff) | |
task.h required for building
Diffstat (limited to 'kc/core/task.c')
| -rw-r--r-- | kc/core/task.c | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/kc/core/task.c b/kc/core/task.c new file mode 100644 index 0000000..8f78516 --- /dev/null +++ b/kc/core/task.c @@ -0,0 +1,76 @@ +#include "task.h" +#include "memory.h" +#include "panic.h" +#include "cpu.h" + +struct kc_thread *current_task = NULL; +struct kc_thread *first_ready_task = NULL; +struct kc_thread *last_ready_task = NULL; + +static int schedule_lock_cnt = 0; + +static void lock_scheduler(void); +static void unlock_scheduler(void); +static void set_thread_status(enum kc_thread_status status); +static void idle_task_thread(void); + +extern uint64_t *get_tss_rsp0(void); + +void task_set_thread(struct kc_thread *task) +{ + // wrapper around cpu_set_thread + uint64_t *rsp0 = get_tss_rsp0(); + cpu_set_thread(¤t_task->state, &task->state, rsp0); +} + +void task_schedule(void) +{ + if (first_ready_task) + { + struct kc_thread *task = first_ready_task; + first_ready_task = task->next; + task_set_thread(task); + } +} + +static void lock_scheduler(void) +{ + __asm__ volatile ("cli;"); + schedule_lock_cnt++; +} + +static void unlock_scheduler(void) +{ + if (1 == schedule_lock_cnt) + { + schedule_lock_cnt--; + __asm__ volatile("sti;"); + } +} + +static void unblock_thread(struct kc_thread *thread) +{ + lock_scheduler(); + if (!first_ready_task) + { + task_set_thread(thread); + } + else + { + first_ready_task->next = thread; + first_ready_task = thread; + } +} + +static void set_thread_status(enum kc_thread_status status) +{ + lock_scheduler(); + current_task->status = status; + task_schedule(); + unlock_scheduler(); +} + +static void idle_task_thread(void) +{ +} + |
