Add thread priority support.
[pintos-anon] / src / threads / thread.h
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 #ifdef USERPROG
9 #include "userprog/addrspace.h"
10 #endif
11
12 /* States in a thread's life cycle. */
13 enum thread_status
14   {
15     THREAD_RUNNING,     /* Running thread. */
16     THREAD_READY,       /* Not running but ready to run. */
17     THREAD_BLOCKED,     /* Waiting for an event to trigger. */
18     THREAD_DYING        /* About to be destroyed. */
19   };
20
21 /* Thread identifier type.
22    You can redefine this to whatever type you like. */
23 typedef int tid_t;
24 #define TID_ERROR ((tid_t) -1)          /* Error value for tid_t. */
25
26 /* Thread priorities. */
27 #define PRI_MIN 0                       /* Lowest priority. */
28 #define PRI_DEFAULT 29                  /* Default priority. */
29 #define PRI_MAX 59                      /* Highest priority. */
30
31 /* A kernel thread or user process.
32
33    Each thread structure is stored in its own 4 kB page.  The
34    thread structure itself sits at the very bottom of the page
35    (at offset 0).  The rest of the page is reserved for the
36    thread's kernel stack, which grows downward from the top of
37    the page (at offset 4 kB).  Here's an illustration:
38
39         4 kB +---------------------------------+
40              |          kernel stack           |
41              |                |                |
42              |                |                |
43              |                V                |
44              |         grows downward          |
45              |                                 |
46              |                                 |
47              |                                 |
48              |                                 |
49              |                                 |
50              |                                 |
51              |                                 |
52              |                                 |
53              +---------------------------------+
54              |             magic               |
55              |               :                 |
56              |               :                 |
57              |              name               |
58              |             status              |
59         0 kB +---------------------------------+
60
61    The upshot of this is twofold:
62
63       1. First, `struct thread' must not be allowed to grow too
64          big.  If it does, then there will not be enough room for
65          the kernel stack.  Our base `struct thread' is only a
66          few bytes in size.  It probably should stay well under 1
67          kB.
68
69       2. Second, kernel stacks must not be allowed to grow too
70          large.  If a stack overflows, it will corrupt the thread
71          state.  Thus, kernel functions should not allocate large
72          structures or arrays as non-static local variables.  Use
73          dynamic allocation with malloc() or palloc_get()
74          instead.
75
76    The first symptom of either of these problems will probably be
77    an assertion failure in thread_current(), which checks that
78    the `magic' member of the running thread's `struct thread' is
79    set to THREAD_MAGIC.  Stack overflow will normally change this
80    value, triggering the assertion. */
81 /* The `elem' member has a dual purpose.  It can be an element in
82    the run queue (thread.c), or it can be an element in a
83    semaphore wait list (synch.c).  It can be used these two ways
84    only because they are mutually exclusive: only a thread in the
85    ready state is on the run queue, whereas only a thread in the
86    blocked state is on a semaphore wait list. */
87 struct thread
88   {
89     /* These members are owned by thread.c. */
90     tid_t tid;                          /* Thread identifier. */
91     enum thread_status status;          /* Thread state. */
92     char name[16];                      /* Name (for debugging purposes). */
93     uint8_t *stack;                     /* Saved stack pointer. */
94     int priority;                       /* Priority. */
95
96     /* Shared between thread.c and synch.c. */
97     list_elem elem;                     /* List element. */
98
99 #ifdef USERPROG
100     /* These members are owned by userprog/addrspace.c. */
101     uint32_t *pagedir;                  /* Page directory. */
102 #endif
103
104     /* Marker to detect stack overflow. */
105     unsigned magic;                     /* Always set to THREAD_MAGIC. */
106   };
107
108 void thread_init (void);
109 void thread_start (void);
110
111 typedef void thread_func (void *aux);
112 tid_t thread_create (const char *name, int priority, thread_func *, void *);
113 #ifdef USERPROG
114 tid_t thread_execute (const char *filename);
115 #endif
116
117 void thread_unblock (struct thread *);
118
119 struct thread *thread_current (void);
120 tid_t thread_tid (void);
121 const char *thread_name (void);
122 void thread_exit (void) NO_RETURN;
123 void thread_yield (void);
124 void thread_block (void);
125
126 /* This function will be implemented in problem 1-2. */
127 void thread_join (tid_t);
128
129 /* These functions will be implemented in problem 1-3. */
130 void thread_set_priority (int);
131 int thread_get_priority (void);
132
133 #endif /* threads/thread.h */