metal-cos/sys/include/thread.h

98 lines
2.6 KiB
C

#ifndef __SYS_THREAD_H__
#define __SYS_THREAD_H__
#include <sys/queue.h>
#include <sys/handle.h>
#include <sys/ktimer.h>
struct Thread;
typedef struct Thread Thread;
#include <machine/pmap.h>
#include <machine/thread.h>
#define SCHED_STATE_NULL 0
#define SCHED_STATE_RUNNABLE 1
#define SCHED_STATE_RUNNING 2
#define SCHED_STATE_WAITING 3
#define SCHED_STATE_ZOMBIE 4
typedef struct Thread {
ThreadArch arch;
AS *space;
uintptr_t kstack;
uintptr_t ustack;
uint64_t tid;
// Process
struct Process *proc;
// XXX: process list
// Scheduler
int schedState;
TAILQ_ENTRY(Thread) schedQueue;
TimerEvent *timerEvt; // Timer event for wakeups
uintptr_t exitValue;
// Statistics
uint64_t ctxSwitches;
uint64_t userTime;
uint64_t kernTime;
uint64_t waitTime;
uint64_t waitStart;
} Thread;
#define PROCESS_HANDLE_SLOTS 128
typedef struct Process {
uint64_t pid;
AS *space;
uintptr_t ustackNext; // Next user stack
TAILQ_ENTRY(Process) processList;
// Threads
uint64_t threads;
TAILQ_HEAD(ZombieQueueHead, Thread) zombieQueue;
// Handles
uint64_t nextFD;
TAILQ_HEAD(HandleQHead, Handle) handles[PROCESS_HANDLE_SLOTS];
} Process;
// Thread functions
void Thread_Init();
Thread *Thread_Current();
Process *Thread_CreateProcess();
Thread *Thread_Create(Process *proc);
Thread *Thread_KThreadCreate(void (*f)(void*), void *arg);
Thread *Thread_UThreadCreate(Thread *oldThr, uint64_t rip, uint64_t arg);
void Thread_SetRunnable(Thread *thr);
void Thread_SetWaiting(Thread *thr);
void Thread_SetZombie(Thread *thr);
void Thread_Destroy(Thread *thr);
uint64_t Thread_Wait(Thread *thr, uint64_t tid);
void Thread_Switch(Thread *oldthr, Thread *newthr);
void Thread_Scheduler();
void Thread_ProcDump(Process *proc);
void Thread_Dump(Thread *thr);
// Process functions
Process *Process_Create();
void Process_Destroy(Process *proc);
// Platform functions
void Thread_InitArch(Thread *thr);
void Thread_SetupKThread(Thread *thr, void (*f)(),
uintptr_t arg1, uintptr_t arg2, uintptr_t arg3);
void Thread_SetupUThread(Thread *thr, uint64_t rip, uint64_t arg);
void Thread_SwitchArch(Thread *oldthr, Thread *newthr);
// Handle Functions
void Handle_Init(Process *proc);
uint64_t Handle_Add(Process *proc, Handle *handle);
void Handle_Remove(Process *proc, Handle *handle);
Handle *Handle_Lookup(Process *proc, uint64_t fd);
// CopyIn/CopyOut Functions
int CopyIn(uintptr_t fromuser, void *tokernel, uintptr_t len);
int CopyOut(void *fromkernel, uintptr_t touser, uintptr_t len);
#endif /* __SYS_THREAD_H__ */