]> git.buserror.net Git - polintos/scott/priv.git/blob - kernel/include/kern/thread.h
update
[polintos/scott/priv.git] / kernel / include / kern / thread.h
1 #ifndef _KERN_THREAD_H
2 #define _KERN_THREAD_H
3
4 #include <kern/types.h>
5 #include <kern/time.h>
6 #include <kern/orb.h>
7 #include <kern/sched.h>
8 #include <kern/lock.h>
9
10 #include <arch/thread.h>
11
12 #include <util/list.h>
13
14 namespace Mem {
15         class ProcAddrSpace;
16 }
17
18 namespace Threads {
19         class Thread {
20                 // This must be first.
21                 Arch::ArchThread arch;
22                 
23                 // If the thread is currently running, this contains the time of
24                 // the context switch when the thread most recently started
25                 // running.
26
27                 Time::Time last_time;
28                 
29                 // This contains the time in ns that the thread has remaining in
30                 // its current timeslice.
31                 
32                 s32 time_left;
33
34                 // The current timeslice length in ns.  For timeshared tasks, this
35                 // is simply the priority scaled linearly.
36                 
37                 u32 time_slice;
38                 
39                 // Real-time and timeshared priorities
40                 //
41                 // Real-time priorities go from 0 to 255, with 0 indicating a
42                 // timeshared task.
43                 //
44                 // Static timeshared priorities go from 1 to 16; they determine the
45                 // timeslice of a task (not including interactivity bonuses).  The
46                 // default is 8.  The dynamic timeshared priority is the priority
47                 // associated with time_left as of the last global timeslice
48                 // replenishment or wake-up, and ranges from 1 to 31.  If the
49                 // static priority is x, the dynamic priority is within the range
50                 // [x,2x-1].
51                 
52                 int rt_prio, ts_static_prio, ts_prio, last_replenish;
53                 
54                 enum Policy {
55                         TimeShared,
56                         FIFO,
57                         RoundRobin
58                 } policy;
59
60                 ThreadBlocker *blocked_on;
61                 Util::List runqueue_node;
62                 
63                 void ts_add();
64                 void ts_del();
65                 void ts_deplete();
66
67                 void add();
68                 void del();
69                 void replenish();
70                 void charge(Time::Time &now);
71                 void prio_adjust();
72                 void wake_nolock();
73                 
74         public:
75                 Util::List threadlist_node;
76                 Mem::ProcAddrSpace *aspace, *active_aspace;
77                 
78                 enum {
79                         name_len = 32
80                 };
81                 
82                 char name[name_len];
83         
84                 enum {
85                         internal_orbstack_frames = 8
86                 };
87                 
88                 ORB::CallStackHeader orbstack;
89                 ORB::CallFrame orbstackframes[internal_orbstack_frames];
90
91                 // The header and frame of the method invocation at the top of
92                 // the stack.  FIXME: add a lock aronud these if other threads
93                 // can read these to do a traceback/traceforward.
94
95                 ORB::CallStackHeader *orbstack_top_hdr;
96                 int orbstack_top; // Index into orbstack_top_hdr->frames[]
97                 
98                 ~Thread();
99                 void exit();
100                 void block(ThreadBlocker *blocker);
101                 void wake();
102                 
103                 friend class Sched;
104                 friend class WaitQueue;
105
106                 friend void Arch::switch_thread(Thread *dest, Thread *src);
107                 
108                 // FIXME: temp hack; use a Process later
109                 
110                 void set_aspace(Mem::ProcAddrSpace *aspace);
111         };
112         
113         enum {
114                 thread_size = sizeof(Thread) + 7 / 8
115         };
116 }
117
118 #include <arch/current.h>
119 #define curthread (::Arch::get_current_thread())
120
121 namespace Lock {
122         inline bool Lock::held_by_curthread()
123         {
124                 return lockval == reinterpret_cast<ulong>(curthread);
125         }
126 }
127
128 #endif