Nucleus
Barry Kernel threads + threads share address space 6217f0d (3 years, 1 month ago)
diff --git a/task/kthread.c b/task/kthread.c
new file mode 100644
index 0000000..2c91f5a
--- /dev/null
+++ b/task/kthread.c
@@ -0,0 +1,34 @@
+/*
+ * This file implements kernel threads. Each kernel thread has a stack
+ * allocated in kernel memory and will run a specified function within the
+ * kernel. When that function completes, the thread will terminate. The
+ * thread has no resources allocated and should only access the kernel.
+ */
+
+#include <stddef.h>
+#include <stdint.h>
+#include <nucleus/lib.h>
+#include <nucleus/task.h>
+
+#define PUSH(s,v) ({ \
+ s -= sizeof(uintptr_t); \
+ *(uintptr_t *) s = (uintptr_t) v; \
+})
+
+/* Create a new kernel thread */
+Task *
+create_kthread(void (*func)(void), enum Priority p)
+{
+ /* Create new task */
+ Task *task = new(&taskType);
+ task->priority = p;
+ task->eip = (uintptr_t) func;
+
+ /* Setup stack to return to terminate() */
+ task->esp = (uintptr_t) task + KERNEL_STACK_SIZE;
+ PUSH(task->esp, terminate);
+
+ /* Schedule the task */
+ enqueue_task(task);
+ return task;
+}