CS350 COS
COS
Loading...
Searching...
No Matches
thread.h
Go to the documentation of this file.
1
2#ifndef __SYS_THREAD_H__
3#define __SYS_THREAD_H__
4
5#include <sys/queue.h>
6#include <sys/handle.h>
7#include <sys/ktimer.h>
8#include <sys/waitchannel.h>
9
10struct Thread;
11typedef struct Thread Thread;
12struct Process;
13typedef struct Process Process;
14
15#include <sys/semaphore.h>
16#include <sys/mutex.h>
17#include <sys/cv.h>
18
19#include <machine/pmap.h>
20#include <machine/thread.h>
21
22typedef TAILQ_HEAD(ProcessQueue, Process) ProcessQueue;
23typedef TAILQ_HEAD(ThreadQueue, Thread) ThreadQueue;
24
25#define SCHED_STATE_NULL 0
26#define SCHED_STATE_RUNNABLE 1
27#define SCHED_STATE_RUNNING 2
28#define SCHED_STATE_WAITING 3
29#define SCHED_STATE_ZOMBIE 4
30
31typedef struct Thread {
38 // Process
39 struct Process *proc;
40 TAILQ_ENTRY(Thread) threadList;
41 // Scheduler
43 TAILQ_ENTRY(Thread) schedQueue;
44 KTimerEvent *timerEvt; // Timer event for wakeups
46 TAILQ_ENTRY(Thread) semaQueue; // Semaphore Queue
47 // Wait Channels
49 TAILQ_ENTRY(Thread) chanQueue;
50 // Statistics
56} Thread;
57
58#define PROCESS_HANDLE_SLOTS 128
59#define PROCESS_TITLE_LENGTH 128
60
61#define PROC_STATE_NULL 0
62#define PROC_STATE_READY 1
63#define PROC_STATE_ZOMBIE 2
64
65typedef struct Process {
71 uintptr_t ustackNext; // Next user stack
77 // Process
79 TAILQ_ENTRY(Process) siblingList;
80 ProcessQueue childrenList;
81 ProcessQueue zombieProc;
85 // Threads
87 ThreadQueue threadList;
89 ThreadQueue zombieQueue;
90 // Handles
93} Process;
94
95// General
96void Thread_Init();
97void Thread_InitAP();
98
99// Process functions
100Process *Process_Create(Process *parent, const char *title);
102void Process_Retain(Process *proc);
103void Process_Release(Process *proc);
105
106#define TID_ANY 0xFFFFFFFF
107
108// Thread functions
110Thread *Thread_KThreadCreate(void (*f)(void*), void *arg);
113void Thread_Retain(Thread *thr);
114void Thread_Release(Thread *thr);
116
117// Scheduler functions
119void Sched_SetRunnable(Thread *thr);
120void Sched_SetWaiting(Thread *thr);
121void Sched_SetZombie(Thread *thr);
122void Sched_Scheduler();
123
124// Debugging
125void Process_Dump(Process *proc);
126void Thread_Dump(Thread *thr);
127
128// Platform functions
129void Thread_InitArch(Thread *thr);
130void Thread_SetupKThread(Thread *thr, void (*f)(),
131 uintptr_t arg1, uintptr_t arg2, uintptr_t arg3);
132void Thread_SetupUThread(Thread *thr, uint64_t rip, uint64_t arg);
133void Thread_SwitchArch(Thread *oldthr, Thread *newthr);
134
135// Handle Functions
136void Handle_Init(Process *proc);
137void Handle_Destroy(Process *proc);
138uint64_t Handle_Add(Process *proc, Handle *handle);
139void Handle_Remove(Process *proc, Handle *handle);
141
142// Copy_In/Copy_Out Functions
143int Copy_In(uintptr_t fromuser, void *tokernel, uintptr_t len);
144int Copy_Out(void *fromkernel, uintptr_t touser, uintptr_t len);
145int Copy_StrIn(uintptr_t fromuser, void *tokernel, uintptr_t len);
146int Copy_StrOut(void *fromkernel, uintptr_t touser, uintptr_t len);
147
148#endif /* __SYS_THREAD_H__ */
149
Definition: cv.h:5
Thread * Thread_KThreadCreate(void(*f)(void *), void *arg)
Definition: thread.c:136
int Copy_StrIn(uintptr_t fromuser, void *tokernel, uintptr_t len)
Definition: copy.c:106
void Sched_SetWaiting(Thread *thr)
Definition: sched.c:104
void Sched_Scheduler()
Definition: sched.c:189
void Handle_Init(Process *proc)
Definition: handle.c:23
Thread * Thread_Create(Process *proc)
Definition: thread.c:96
Thread * Thread_UThreadCreate(Thread *oldThr, uint64_t rip, uint64_t arg)
Definition: thread.c:148
int Copy_StrOut(void *fromkernel, uintptr_t touser, uintptr_t len)
Definition: copy.c:142
Handle * Handle_Lookup(Process *proc, uint64_t fd)
Definition: handle.c:71
void Thread_SetupKThread(Thread *thr, void(*f)(), uintptr_t arg1, uintptr_t arg2, uintptr_t arg3)
Definition: thread.c:26
void Thread_Release(Thread *thr)
Definition: thread.c:265
int Copy_In(uintptr_t fromuser, void *tokernel, uintptr_t len)
Definition: copy.c:34
Process * Process_Create(Process *parent, const char *title)
Definition: process.c:50
void Process_Dump(Process *proc)
Definition: process.c:251
void Thread_Init()
Definition: thread.c:53
void Handle_Remove(Process *proc, Handle *handle)
Definition: handle.c:63
Process * Process_Lookup(uint64_t pid)
Definition: process.c:145
void Sched_SetZombie(Thread *thr)
Definition: sched.c:126
uint64_t Process_Wait(Process *proc, uint64_t pid)
Definition: process.c:206
void Thread_Dump(Thread *thr)
Definition: thread.c:328
void Sched_SetRunnable(Thread *thr)
Definition: sched.c:77
void Process_Retain(Process *proc)
Definition: process.c:171
void Thread_SwitchArch(Thread *oldthr, Thread *newthr)
Definition: thread.c:84
int Copy_Out(void *fromkernel, uintptr_t touser, uintptr_t len)
Definition: copy.c:70
uint64_t Handle_Add(Process *proc, Handle *handle)
Definition: handle.c:47
Thread * Sched_Current()
Definition: sched.c:56
void Handle_Destroy(Process *proc)
Definition: handle.c:33
uint64_t Thread_Wait(Thread *thr, uint64_t tid)
Definition: thread.c:279
void Thread_InitAP()
Definition: thread.c:79
void Thread_SetupUThread(Thread *thr, uint64_t rip, uint64_t arg)
Definition: thread.c:75
void Process_Release(Process *proc)
Definition: process.c:185
#define PROCESS_HANDLE_SLOTS
Definition: thread.h:58
Thread * Thread_Lookup(Process *proc, uint64_t tid)
Definition: thread.c:229
void Thread_InitArch(Thread *thr)
Definition: thread.c:20
#define PROCESS_TITLE_LENGTH
Definition: thread.h:59
void Thread_Retain(Thread *thr)
Definition: thread.c:253
uint64_t len
Definition: multiboot.h:2
Definition: pmap.h:52
#define TAILQ_HEAD(name, type)
Definition: queue.h:491
Definition: handle.h:17
Definition: mutex.h:8
Definition: thread.h:65
uint64_t nextFD
Definition: thread.h:91
HandleQueue handles[PROCESS_HANDLE_SLOTS]
Definition: thread.h:92
Spinlock lock
Definition: thread.h:68
TAILQ_ENTRY(Process) siblingList
uint64_t refCount
Definition: thread.h:73
ProcessQueue zombieProc
Definition: thread.h:81
ThreadQueue threadList
Definition: thread.h:87
uintptr_t entrypoint
Definition: thread.h:69
uintptr_t ustackNext
Definition: thread.h:71
uint64_t nextThreadID
Definition: thread.h:70
uint64_t exitCode
Definition: thread.h:76
char title[PROCESS_TITLE_LENGTH]
Definition: thread.h:74
ThreadQueue zombieQueue
Definition: thread.h:89
uint64_t threads
Definition: thread.h:86
Process * parent
Definition: thread.h:78
Semaphore zombieSemaphore
Definition: thread.h:88
CV zombieProcPCV
Definition: thread.h:84
Mutex zombieProcLock
Definition: thread.h:82
int procState
Definition: thread.h:75
CV zombieProcCV
Definition: thread.h:83
ProcessQueue childrenList
Definition: thread.h:80
uint64_t pid
Definition: thread.h:66
TAILQ_ENTRY(Process) processList
AS * space
Definition: thread.h:67
Definition: thread.h:31
uint64_t ctxSwitches
Definition: thread.h:51
uint64_t kernTime
Definition: thread.h:53
TAILQ_ENTRY(Thread) schedQueue
struct Process * proc
Definition: thread.h:39
uint64_t refCount
Definition: thread.h:37
uintptr_t ustack
Definition: thread.h:35
TAILQ_ENTRY(Thread) semaQueue
ThreadArch arch
Definition: thread.h:32
uintptr_t exitValue
Definition: thread.h:45
uint64_t waitTime
Definition: thread.h:54
WaitChannel * chan
Definition: thread.h:48
uint64_t userTime
Definition: thread.h:52
TAILQ_ENTRY(Thread) threadList
AS * space
Definition: thread.h:33
uint64_t waitStart
Definition: thread.h:55
KTimerEvent * timerEvt
Definition: thread.h:44
int schedState
Definition: thread.h:42
uint64_t tid
Definition: thread.h:36
uintptr_t kstack
Definition: thread.h:34
TAILQ_ENTRY(Thread) chanQueue
ProcessQueue processList
Definition: process.c:30
uint64_t uintptr_t
Definition: types.h:16
unsigned long uint64_t
Definition: types.h:13