feat: initial locking etc.

This commit is contained in:
2021-03-01 21:07:53 +01:00
parent ebe006a8ba
commit 990b850c43
14 changed files with 427 additions and 14 deletions

View File

@@ -10,6 +10,7 @@
#include <mem/pmm.h>
#include <attributes.h>
#include <libk/libk.h>
#define stack_end(task) ((task)->stack + ((task)->stack_page_count * PAGE_SIZE))
@@ -17,7 +18,9 @@
#define TASK_STATE_RUNNABLE (1 << 0)
#define TASK_STATE_RUNNING (1 << 1)
#define TASK_STATE_WAIT_IRQ (1 << 2)
#define TASK_STATE_WAIT_SIGNAL (1 << 3)
#define TASK_STATE_STOPPED (1 << 6)
#define TASK_STATE_ERROR (1 << 7)
@@ -44,7 +47,7 @@ typedef struct {
char alignment[4];
} packed task_stack_start;
bool task_locked = false;
volatile uint32_t task_locked = 0;
task_t *idle_task = NULL;
task_t *first_task = NULL;
@@ -67,12 +70,45 @@ void cdecl noreturn task_entry_point(task_entrypoint entrypoint, void *entry_dat
while (true); // halt
}
void task_ensure_enabled() {
if (current_task == NULL) {
k_panics("Tasking should be enabled\n");
}
}
void task_lock_acquire() {
task_locked = true;
if (__sync_add_and_fetch(&task_locked, 1) == UINT32_MAX) {
k_panics("To many task locks");
};
}
void task_lock_free() {
task_locked = false;
if (__sync_sub_and_fetch(&task_locked, 1) == UINT32_MAX) {
k_panics("To many task free's");
}
}
uint32_t task_get_current_tid() {
return current_task->tid;
}
void task_signal(uint32_t tid) {
task_t *t = first_task;
while (t != NULL) {
if (t->tid == tid) {
if (t->state != TASK_STATE_WAIT_SIGNAL) {
// todo
}
t->state = TASK_STATE_RUNNABLE;
break;
}
t = t->next;
}
}
void task_suspend() {
current_task->state = TASK_STATE_WAIT_SIGNAL;
task_switch_next();
}
void noreturn task_idle(void *data) {
@@ -98,7 +134,7 @@ void task_wait_irq(uint16_t irq_bits) {
}
void task_switch_next_inner(task_t *next_task) {
if (task_locked) {
if (task_locked != 0) {
return; // don't switch while the task is locked
}
if (next_task == current_task) {
@@ -115,6 +151,9 @@ void task_switch_next_inner(task_t *next_task) {
}
void task_start_first() {
if (task_locked > 0) {
k_panics("Tasking locked before start\n");
}
task_switch_next_inner(first_task);
}
@@ -203,5 +242,13 @@ uint32_t task_spawn(task_entrypoint entrypoint, void *entry_data) {
return new_task->tid;
}
void task_end(uint32_t pid) {
void task_end(uint32_t tid) {
task_t *t = first_task;
while (t != NULL) {
if (t->tid == tid) {
t->state = TASK_STATE_STOPPED;
break;
}
t = t->next;
}
}