matos/core/thread.h

98 lines
3.5 KiB
C
Raw Normal View History

2021-10-30 14:18:21 +02:00
#pragma once
2021-10-30 15:30:19 +02:00
struct thread;
2021-10-30 14:18:21 +02:00
#include "cpu_context.h"
#include "mem.h"
2021-10-30 15:30:19 +02:00
#include "process.h"
#include "wait.h"
2021-10-30 14:18:21 +02:00
#define THREAD_NAME_MAX_LENGTH 32
#define THREAD_DEFAULT_STACK_SIZE PAGE_SIZE
typedef enum {
RUNNING,
READY,
SLEEPING,
WAITING,
EXITING
} thread_state;
2024-02-14 23:20:46 +01:00
typedef unsigned long int thread_id_t;
2021-10-30 14:18:21 +02:00
struct thread {
char name[THREAD_NAME_MAX_LENGTH];
2024-02-14 23:20:46 +01:00
thread_id_t tid;
2021-10-30 14:18:21 +02:00
struct cpu_state *cpuState;
thread_state state;
vaddr_t stackAddr;
size_t stackSize;
2021-10-30 15:30:19 +02:00
2021-10-30 14:18:21 +02:00
unsigned long jiffiesSleeping;
int sleepHaveTimeouted;
2021-10-30 15:30:19 +02:00
struct thread *next, *prev;
struct thread *timeNext, *timePrev;
// For User thread only
struct thread *nextInProcess, *prevInProcess;
struct process *process;
struct wait_queue *wqExit; // This will be signaled at thread exit (user only)
2021-11-02 21:24:12 +01:00
/**
* Address space currently "squatted" by the thread, or used to be
* active when the thread was interrupted/preempted. This is the MMU
* configuration expected before the cpu_state of the thread is
* restored on CPU.
* - For kernel threads: should normally be NULL, meaning that the
* thread will squat the current mm_context currently set in the
* MMU. Might be NON NULL when a kernel thread squats a given
* process to manipulate its address space.
* - For user threads: should normally be NULL. More precisely:
* - in user mode: the thread->process.mm_context is ALWAYS
* set on MMU. squatted_mm_context is ALWAYS NULL in this
* situation, meaning that the thread in user mode uses its
* process-space as expected
* - in kernel mode: NULL means that we keep on using the
* mm_context currently set on MMU, which might be the
* mm_context of another process. This is natural since a
* thread in kernel mode normally only uses data in kernel
* space. BTW, this limits the number of TLB flushes. However,
* there are exceptions where this squatted_mm_context will
* NOT be NULL. One is the copy_from/to_user API, which can
* force the effective mm_context so that the MMU will be
* (re)configured upon every context to the thread to match
* the squatted_mm_context. Another exception is when a parent
* thread creates the address space of a child process, in
* which case the parent thread might temporarilly decide to
* switch to the child's process space.
*
* This is the SOS/matos implementation of the Linux "Lazy TLB" and
* address-space loaning.
*/
struct mmu_context *squattedContext;
2021-10-30 14:18:21 +02:00
};
int threadSetup(vaddr_t mainStack, size_t mainStackSize);
void threadExit();
struct thread *threadCreate(const char *name, cpu_kstate_function_arg1_t func, void *args);
2021-11-03 23:54:36 +01:00
struct thread *threadCreateUser(const char *name, struct process *proc, uaddr_t startPc,
uint32_t arg1, uint32_t arg2, uaddr_t startSP);
2021-10-30 14:18:21 +02:00
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);
2024-02-14 18:43:47 +01:00
int threadUsleep(unsigned long usec);
2021-10-30 14:18:21 +02:00
int threadOnJieffiesTick();
struct thread *getCurrentThread();
int threadAddThread(struct thread *th);
2021-11-02 21:24:12 +01:00
int threadChangeCurrentContext(struct mmu_context *ctx);
2021-11-03 23:54:36 +01:00
int threadCount();
2024-02-14 23:20:46 +01:00
thread_id_t threadGetId(struct thread *th);