matos/core/thread.h

53 lines
1.2 KiB
C

#pragma once
struct thread;
#include "cpu_context.h"
#include "mem.h"
#include "process.h"
#define THREAD_NAME_MAX_LENGTH 32
#define THREAD_DEFAULT_STACK_SIZE PAGE_SIZE
typedef enum {
RUNNING,
READY,
SLEEPING,
WAITING,
EXITING
} thread_state;
struct thread {
char name[THREAD_NAME_MAX_LENGTH];
struct cpu_state *cpuState;
thread_state state;
vaddr_t stackAddr;
size_t stackSize;
unsigned long jiffiesSleeping;
int sleepHaveTimeouted;
struct thread *next, *prev;
struct thread *timeNext, *timePrev;
// For User thread only
struct thread *nextInProcess, *prevInProcess;
struct process *process;
};
int threadSetup(vaddr_t mainStack, size_t mainStackSize);
void threadExit();
struct thread *threadCreate(const char *name, cpu_kstate_function_arg1_t func, void *args);
void threadDelete(struct thread *thread);
struct thread *threadSelectNext();
struct cpu_state *threadSwitch(struct cpu_state *prevCpu);
int threadYield();
int threadWait(struct thread *current, struct thread *next, unsigned long msec);
int threadUnsched(struct thread *th);
int threadMsleep(unsigned long msec);
int threadOnJieffiesTick();
struct thread *getCurrentThread();
int threadAddThread(struct thread *th);