Newer
Older
tree-os / src / kernel / lib / task / task.c
@lukas lukas on 2 Apr 1 KB cleanup
#include <lib/task/task.h>
#include <lib/memory/alloc.h>
#include <lib/textMode/stdio.h>

Task *runningTask;

void createTask(Task *task, uint32_t mainFunction, uint32_t flags, uint32_t *pagedir) {
    task->registers.eax = 0;
    task->registers.ebx = 0;
    task->registers.ecx = 0;
    task->registers.edx = 0;
    task->registers.esi = 0;
    task->registers.edi = 0;
    task->registers.eflags = flags;
    task->registers.eip = mainFunction;
    task->registers.cr3 = (uint32_t) pagedir;
    task->registers.esp = 0xF000 + (uint32_t) malloc(0xF000);
    task->nextTask = 0;
}

void yield() {
    Task* last = runningTask;
    runningTask = runningTask->nextTask;
    if (runningTask == 0x00) {
        printf("No more tasks to run, halting . . .\n");
        asm("hlt");
    }
    last->nextTask = 0x00;
    switchTask(&(last->registers), &(runningTask->registers));
}

void schedule(Task* task) {
    Task* test = runningTask;
    while (test->nextTask != 0x0) {
        test = test->nextTask;
    }
    test->nextTask = task;
}

void setRunningTask(Task* task) {
    runningTask = task;
}

void yields() {
    schedule(runningTask);
    yield();
}