BarryServer : Git

All the code for all my projects
// BarryServer : Git / Nucleus / blob / master / task / kthread.c

// Related

Nucleus

Barry Kernel threads + threads share address space 6217f0d (3 years, 1 month ago)
/*
 * 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;
}