3 * Copyright (c) 2001,2003 David H. Hovemeyer <daveho@cs.umd.edu>
6 * This is free software. You are permitted to use,
7 * redistribute, and modify it as specified in the file "COPYING".
10 #ifndef GEEKOS_KTHREAD_H
11 #define GEEKOS_KTHREAD_H
13 #include <geekos/ktypes.h>
14 #include <geekos/list.h>
18 struct Interrupt_State;
22 * This is used for the run queue(s), and also for
23 * thread synchronization and communication.
25 DEFINE_LIST(Thread_Queue, Kernel_Thread);
28 * List which includes all threads.
30 DEFINE_LIST(All_Thread_List, Kernel_Thread);
33 * Kernel thread context data structure.
34 * NOTE: there is assembly code in lowlevel.asm that depends
35 * on the offsets of the fields in this struct, so if you change
36 * the layout, make sure everything gets updated.
38 struct Kernel_Thread {
39 ulong_t esp; /* offset 0 */
40 volatile ulong_t numTicks; /* offset 4 */
42 DEFINE_LINK(Thread_Queue, Kernel_Thread);
44 struct User_Context* userContext;
45 struct Kernel_Thread* owner;
48 /* These fields are used to implement the Join() function */
50 struct Thread_Queue joinQueue;
53 /* The kernel thread id; also used as process id */
56 /* Link fields for list of all threads in the system. */
57 DEFINE_LINK(All_Thread_List, Kernel_Thread);
59 /* Array of MAX_TLOCAL_KEYS pointers to thread-local data. */
60 #define MAX_TLOCAL_KEYS 128
61 const void* tlocalData[MAX_TLOCAL_KEYS];
66 * Define Thread_Queue and All_Thread_List access and manipulation functions.
68 IMPLEMENT_LIST(Thread_Queue, Kernel_Thread);
69 IMPLEMENT_LIST(All_Thread_List, Kernel_Thread);
71 static __inline__ void Enqueue_Thread(struct Thread_Queue *queue, struct Kernel_Thread *kthread) {
72 Add_To_Back_Of_Thread_Queue(queue, kthread);
75 static __inline__ void Remove_Thread(struct Thread_Queue *queue, struct Kernel_Thread *kthread) {
76 Remove_From_Thread_Queue(queue, kthread);
80 * Thread start functions should have this signature.
82 typedef void (*Thread_Start_Func)(ulong_t arg);
87 #define PRIORITY_IDLE 0
88 #define PRIORITY_USER 1
89 #define PRIORITY_LOW 2
90 #define PRIORITY_NORMAL 5
91 #define PRIORITY_HIGH 10
95 * Scheduler operations.
97 void Init_Scheduler(void);
98 struct Kernel_Thread* Start_Kernel_Thread(
99 Thread_Start_Func startFunc,
104 struct Kernel_Thread* Start_User_Thread(struct User_Context* userContext, bool detached);
105 void Make_Runnable(struct Kernel_Thread* kthread);
106 void Make_Runnable_Atomic(struct Kernel_Thread* kthread);
107 struct Kernel_Thread* Get_Current(void);
108 struct Kernel_Thread* Get_Next_Runnable(void);
111 void Exit(int exitCode) __attribute__ ((noreturn));
112 int Join(struct Kernel_Thread* kthread);
113 struct Kernel_Thread* Lookup_Thread(int pid);
116 * Thread context switch function, defined in lowlevel.asm
118 void Switch_To_Thread(struct Kernel_Thread*);
121 * Wait queue functions.
123 void Wait(struct Thread_Queue* waitQueue);
124 void Wake_Up(struct Thread_Queue* waitQueue);
125 void Wake_Up_One(struct Thread_Queue* waitQueue);
126 void Wake_Up_Thread(struct Thread_Queue* waitQueue, int pid);
129 * Pointer to currently executing thread.
131 extern struct Kernel_Thread* g_currentThread;
134 * Boolean flag indicating that we need to choose a new runnable thread.
136 extern int g_needReschedule;
139 * Boolean flag indicating that preemption should be disabled.
141 extern volatile int g_preemptionDisabled;
144 * Thread-local data information
146 #define MIN_DESTRUCTOR_ITERATIONS 4
148 typedef void (*tlocal_destructor_t)(void *);
149 typedef unsigned int tlocal_key_t;
151 extern int Tlocal_Create(tlocal_key_t *, tlocal_destructor_t);
152 extern void Tlocal_Put(tlocal_key_t, const void *);
153 extern void *Tlocal_Get(tlocal_key_t);
155 /* Print list of all threads, for debugging. */
156 extern void Dump_All_Thread_List(void);
159 #endif /* GEEKOS_KTHREAD_H */