BarryServer : Git

All the code for all my projects
// BarryServer : Git / Nucleus / blob / master / include / nucleus / task.h

// Related

Nucleus

Barry Kernel threads + threads share address space 6217f0d (3 years, 1 month ago)
#ifndef _NUCLEUS_TASK_H
#define _NUCLEUS_TASK_H

#include <stdint.h>
#include <sys/types.h>
#include <nucleus/cpu.h>
#include <nucleus/memory.h>
#include <nucleus/object.h>
#include <nucleus/types.h>
#include <nucleus/vfs.h>

/* Task priorities */
enum Priority {
	NONE,
	LOWEST,
	LOW,
	NORMAL,
	HIGH,
	HIGHEST,
};
#define PRIORITY_COUNT 6

/* Task states */
enum State {
	RUNNING,
	READY,
	TERMINATED,
	WAITING,
};

/* Structure for a Task */
struct Task {
	Object obj;
	Scheduler *scheduler;
	enum Priority priority;
	enum State state;
	uintptr_t esi, edi, ebx;
	uintptr_t esp, ebp, eip;

	pid_t tid, tgid;
	uid_t uid, euid, suid;
	gid_t gid, egid, sgid;
	int status;
	uint32_t inSyscall;

	File *executable;
	Task *target;
	ObjectList *wait;

	/* Namespaces */
	FileSystem *fs;
	Files *files;
	VirtualMemory *vm;
	Signals *signals;
};

/* Structure for a Scheduler */
struct Scheduler {
	Object obj;
	Processor *cpu;
	Task *task;
	ObjectList *queue[PRIORITY_COUNT];
	uint32_t timeslice;
	size_t tasks;
};

#define current cpu->scheduler->task

#define KERNEL_STACK_SIZE (2 * PAGE_SIZE)

extern ObjectType taskType;
extern ObjectType schedulerType;
extern ObjectType signalsType;

/* Check if super-user */
static inline int
super_user(void)
{
	return (current->euid == 0);
}

/* Enter system call context */
static inline void
enter_syscall_context(void)
{
	current->inSyscall = 1;
}
/* Exit system call context */
static inline uint32_t
exit_syscall_context(void)
{
	uint32_t syscall = current->inSyscall;
	current->inSyscall = 0;
	return syscall;
}

void init_tasking(void);
void block_task(enum State reason, ObjectList *list);
void unblock_task(Task *task);
Task *find_task(pid_t tid);
void enqueue_task(Task *task);
void schedule(void);
Task *create_kthread(void (*func)(void), enum Priority p);
_Noreturn void terminate(void);

#endif