CS318 - Pintos
Pintos source browser for JHU CS318 course
thread.h
Go to the documentation of this file.
1 #ifndef THREADS_THREAD_H
2 #define THREADS_THREAD_H
3 
4 #include <debug.h>
5 #include <list.h>
6 #include <stdint.h>
7 
8 /** States in a thread's life cycle. */
10  {
11  THREAD_RUNNING, /**< Running thread. */
12  THREAD_READY, /**< Not running but ready to run. */
13  THREAD_BLOCKED, /**< Waiting for an event to trigger. */
14  THREAD_DYING /**< About to be destroyed. */
15  };
16 
17 /** Thread identifier type.
18  You can redefine this to whatever type you like. */
19 typedef int tid_t;
20 #define TID_ERROR ((tid_t) -1) /**< Error value for tid_t. */
21 
22 /** Thread priorities. */
23 #define PRI_MIN 0 /**< Lowest priority. */
24 #define PRI_DEFAULT 31 /**< Default priority. */
25 #define PRI_MAX 63 /**< Highest priority. */
26 
27 /** A kernel thread or user process.
28 
29  Each thread structure is stored in its own 4 kB page. The
30  thread structure itself sits at the very bottom of the page
31  (at offset 0). The rest of the page is reserved for the
32  thread's kernel stack, which grows downward from the top of
33  the page (at offset 4 kB). Here's an illustration:
34 
35  4 kB +---------------------------------+
36  | kernel stack |
37  | | |
38  | | |
39  | V |
40  | grows downward |
41  | |
42  | |
43  | |
44  | |
45  | |
46  | |
47  | |
48  | |
49  +---------------------------------+
50  | magic |
51  | : |
52  | : |
53  | name |
54  | status |
55  0 kB +---------------------------------+
56 
57  The upshot of this is twofold:
58 
59  1. First, `struct thread' must not be allowed to grow too
60  big. If it does, then there will not be enough room for
61  the kernel stack. Our base `struct thread' is only a
62  few bytes in size. It probably should stay well under 1
63  kB.
64 
65  2. Second, kernel stacks must not be allowed to grow too
66  large. If a stack overflows, it will corrupt the thread
67  state. Thus, kernel functions should not allocate large
68  structures or arrays as non-static local variables. Use
69  dynamic allocation with malloc() or palloc_get_page()
70  instead.
71 
72  The first symptom of either of these problems will probably be
73  an assertion failure in thread_current(), which checks that
74  the `magic' member of the running thread's `struct thread' is
75  set to THREAD_MAGIC. Stack overflow will normally change this
76  value, triggering the assertion. */
77 /** The `elem' member has a dual purpose. It can be an element in
78  the run queue (thread.c), or it can be an element in a
79  semaphore wait list (synch.c). It can be used these two ways
80  only because they are mutually exclusive: only a thread in the
81  ready state is on the run queue, whereas only a thread in the
82  blocked state is on a semaphore wait list. */
83 struct thread
84  {
85  /* Owned by thread.c. */
86  tid_t tid; /**< Thread identifier. */
87  enum thread_status status; /**< Thread state. */
88  char name[16]; /**< Name (for debugging purposes). */
89  uint8_t *stack; /**< Saved stack pointer. */
90  int priority; /**< Priority. */
91  struct list_elem allelem; /**< List element for all threads list. */
92 
93  /* Shared between thread.c and synch.c. */
94  struct list_elem elem; /**< List element. */
95 
96 #ifdef USERPROG
97  /* Owned by userprog/process.c. */
98  uint32_t *pagedir; /**< Page directory. */
99 #endif
100 
101  /* Owned by thread.c. */
102  unsigned magic; /**< Detects stack overflow. */
103  };
104 
105 /** If false (default), use round-robin scheduler.
106  If true, use multi-level feedback queue scheduler.
107  Controlled by kernel command-line option "-o mlfqs". */
108 extern bool thread_mlfqs;
109 
110 void thread_init (void);
111 void thread_start (void);
112 
113 void thread_tick (void);
114 void thread_print_stats (void);
115 
116 typedef void thread_func (void *aux);
117 tid_t thread_create (const char *name, int priority, thread_func *, void *);
118 
119 void thread_block (void);
120 void thread_unblock (struct thread *);
121 
122 struct thread *thread_current (void);
123 tid_t thread_tid (void);
124 const char *thread_name (void);
125 
126 void thread_exit (void) NO_RETURN;
127 void thread_yield (void);
128 
129 /** Performs some operation on thread t, given auxiliary data AUX. */
130 typedef void thread_action_func (struct thread *t, void *aux);
131 void thread_foreach (thread_action_func *, void *);
132 
133 int thread_get_priority (void);
134 void thread_set_priority (int);
135 
136 int thread_get_nice (void);
137 void thread_set_nice (int);
138 int thread_get_recent_cpu (void);
139 int thread_get_load_avg (void);
140 
141 #endif /**< threads/thread.h */
name
char * name[]
Definition: insult.c:47
NO_RETURN
#define NO_RETURN
Definition: debug.h:8
uint8_t
unsigned char uint8_t
Definition: stdint.h:20
thread::stack
uint8_t * stack
Saved stack pointer.
Definition: thread.h:89
list_elem
Doubly linked list.
Definition: list.h:90
thread_start
void thread_start(void)
Starts preemptive thread scheduling by enabling interrupts.
Definition: thread.c:106
THREAD_READY
Not running but ready to run.
Definition: thread.h:12
thread_status
thread_status
States in a thread's life cycle.
Definition: thread.h:9
thread
A kernel thread or user process.
Definition: thread.h:83
THREAD_DYING
About to be destroyed.
Definition: thread.h:14
thread::tid
tid_t tid
Thread identifier.
Definition: thread.h:86
thread_yield
void thread_yield(void)
Yields the CPU.
Definition: thread.c:302
thread_get_load_avg
int thread_get_load_avg(void)
threads/thread.h
Definition: thread.c:365
thread_action_func
void thread_action_func(struct thread *t, void *aux)
Performs some operation on thread t, given auxiliary data AUX.
Definition: thread.h:130
thread::allelem
struct list_elem allelem
List element for all threads list.
Definition: thread.h:91
tid_t
int tid_t
Thread identifier type.
Definition: thread.h:19
uint32_t
unsigned int uint32_t
Definition: stdint.h:26
thread::status
enum thread_status status
Thread state.
Definition: thread.h:87
THREAD_RUNNING
Running thread.
Definition: thread.h:11
thread_init
void thread_init(void)
thread::elem
struct list_elem elem
List element.
Definition: thread.h:94
stdint.h
thread::magic
unsigned magic
Detects stack overflow.
Definition: thread.h:102
thread_mlfqs
bool thread_mlfqs
If false (default), use round-robin scheduler.
Definition: thread.c:60
thread_get_nice
int thread_get_nice(void)
Returns the current thread's nice value.
Definition: thread.c:357
thread_name
const char * thread_name(void)
Returns the name of the running thread.
Definition: thread.c:247
thread_exit
void thread_exit(void) NO_RETURN
Deschedules the current thread and destroys it.
Definition: thread.c:281
thread_current
struct thread * thread_current(void)
Returns the running thread.
Definition: thread.c:256
thread_block
void thread_block(void)
Puts the current thread to sleep.
Definition: thread.c:214
thread_print_stats
void thread_print_stats(void)
Prints thread statistics.
Definition: thread.c:144
THREAD_BLOCKED
Waiting for an event to trigger.
Definition: thread.h:13
thread::name
char name[16]
Name (for debugging purposes).
Definition: thread.h:88
thread_unblock
void thread_unblock(struct thread *)
Transitions a blocked thread T to the ready-to-run state.
Definition: thread.c:232
thread_foreach
void thread_foreach(thread_action_func *, void *)
Invoke function 'func' on all threads, passing along 'aux'.
Definition: thread.c:320
thread_get_priority
int thread_get_priority(void)
Returns the current thread's priority.
Definition: thread.c:343
thread::priority
int priority
Priority.
Definition: thread.h:90
thread_tid
tid_t thread_tid(void)
Returns the running thread's tid.
Definition: thread.c:273
thread_get_recent_cpu
int thread_get_recent_cpu(void)
Returns 100 times the current thread's recent_cpu value.
Definition: thread.c:373
thread_func
void thread_func(void *aux)
Definition: thread.h:116
list.h
thread_create
tid_t thread_create(const char *name, int priority, thread_func *, void *)
Creates a new kernel thread named NAME with the given initial PRIORITY, which executes FUNCTION passi...
Definition: thread.c:166
thread_set_priority
void thread_set_priority(int)
Sets the current thread's priority to NEW_PRIORITY.
Definition: thread.c:336
thread_tick
void thread_tick(void)
Called by the timer interrupt handler at each timer tick.
Definition: thread.c:123
debug.h
thread_set_nice
void thread_set_nice(int)