Palacios Public Git Repository

To checkout Palacios execute

  git clone http://v3vee.org/palacios/palacios.web/palacios.git
This will give you the master branch. You probably want the devel branch or one of the release branches. To switch to the devel branch, simply execute
  cd palacios
  git checkout --track -b devel origin/devel
The other branches are similar.


(no commit message)
[palacios.git] / palacios / include / geekos / kthread.h
1 /*
2  * Kernel threads
3  * Copyright (c) 2001,2003 David H. Hovemeyer <daveho@cs.umd.edu>
4  * $Revision: 1.1.1.1 $
5  * 
6  * This is free software.  You are permitted to use,
7  * redistribute, and modify it as specified in the file "COPYING".
8  */
9
10 #ifndef GEEKOS_KTHREAD_H
11 #define GEEKOS_KTHREAD_H
12
13 #include <geekos/ktypes.h>
14 #include <geekos/list.h>
15
16 struct Kernel_Thread;
17 struct User_Context;
18 struct Interrupt_State;
19
20 /*
21  * Queue of threads.
22  * This is used for the run queue(s), and also for
23  * thread synchronization and communication.
24  */
25 DEFINE_LIST(Thread_Queue, Kernel_Thread);
26
27 /*
28  * List which includes all threads.
29  */
30 DEFINE_LIST(All_Thread_List, Kernel_Thread);
31
32 /*
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.
37  */
38 struct Kernel_Thread {
39     ulong_t esp;                         /* offset 0 */
40     volatile ulong_t numTicks;           /* offset 4 */
41     int priority;
42     DEFINE_LINK(Thread_Queue, Kernel_Thread);
43     void* stackPage;
44     struct User_Context* userContext;
45     struct Kernel_Thread* owner;
46     int refCount;
47
48     /* These fields are used to implement the Join() function */
49     bool alive;
50     struct Thread_Queue joinQueue;
51     int exitCode;
52
53     /* The kernel thread id; also used as process id */
54     int pid;
55
56     /* Link fields for list of all threads in the system. */
57     DEFINE_LINK(All_Thread_List, Kernel_Thread);
58
59     /* Array of MAX_TLOCAL_KEYS pointers to thread-local data. */
60 #define MAX_TLOCAL_KEYS 128
61     const void* tlocalData[MAX_TLOCAL_KEYS];
62
63 };
64
65 /*
66  * Define Thread_Queue and All_Thread_List access and manipulation functions.
67  */
68 IMPLEMENT_LIST(Thread_Queue, Kernel_Thread);
69 IMPLEMENT_LIST(All_Thread_List, Kernel_Thread);
70
71 static __inline__ void Enqueue_Thread(struct Thread_Queue *queue, struct Kernel_Thread *kthread) {
72     Add_To_Back_Of_Thread_Queue(queue, kthread);
73 }
74
75 static __inline__ void Remove_Thread(struct Thread_Queue *queue, struct Kernel_Thread *kthread) {
76     Remove_From_Thread_Queue(queue, kthread);
77 }
78
79 /*
80  * Thread start functions should have this signature.
81  */
82 typedef void (*Thread_Start_Func)(ulong_t arg);
83
84 /*
85  * Thread priorities
86  */
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
92
93
94 /*
95  * Scheduler operations.
96  */
97 void Init_Scheduler(void);
98 struct Kernel_Thread* Start_Kernel_Thread(
99     Thread_Start_Func startFunc,
100     ulong_t arg,
101     int priority,
102     bool detached
103 );
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);
109 void Schedule(void);
110 void Yield(void);
111 void Exit(int exitCode) __attribute__ ((noreturn));
112 int Join(struct Kernel_Thread* kthread);
113 struct Kernel_Thread* Lookup_Thread(int pid);
114
115 /*
116  * Thread context switch function, defined in lowlevel.asm
117  */
118 void Switch_To_Thread(struct Kernel_Thread*);
119
120 /*
121  * Wait queue functions.
122  */
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
127 /*
128  * Pointer to currently executing thread.
129  */
130 extern struct Kernel_Thread* g_currentThread;
131
132 /*
133  * Boolean flag indicating that we need to choose a new runnable thread.
134  */
135 extern int g_needReschedule;
136
137 /*
138  * Boolean flag indicating that preemption should be disabled.
139  */
140 extern volatile int g_preemptionDisabled;
141
142 /*
143  * Thread-local data information
144  */
145 #define MIN_DESTRUCTOR_ITERATIONS 4
146
147 typedef void (*tlocal_destructor_t)(void *);
148 typedef unsigned int tlocal_key_t;
149
150 extern int Tlocal_Create(tlocal_key_t *, tlocal_destructor_t);
151 extern void Tlocal_Put(tlocal_key_t, const void *);
152 extern void *Tlocal_Get(tlocal_key_t);
153
154 /* Print list of all threads, for debugging. */
155 extern void Dump_All_Thread_List(void);
156
157
158 #endif  /* GEEKOS_KTHREAD_H */