1 #ifndef THREADS_THREAD_H
2 #define THREADS_THREAD_H
9 #include "userprog/addrspace.h"
12 /* States in a thread's life cycle. */
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. */
21 /* Thread identifier type.
22 You can redefine this to whatever type you like. */
24 #define TID_ERROR ((tid_t) -1) /* Error value for tid_t. */
26 /* Thread priorities. */
27 #define PRI_MIN 0 /* Lowest priority. */
28 #define PRI_DEFAULT 29 /* Default priority. */
29 #define PRI_MAX 59 /* Highest priority. */
31 /* A kernel thread or user process.
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:
39 4 kB +---------------------------------+
53 +---------------------------------+
59 0 kB +---------------------------------+
61 The upshot of this is twofold:
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
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()
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. */
89 /* 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. */
96 /* Shared between thread.c and synch.c. */
97 list_elem elem; /* List element. */
100 /* Owned by userprog/addrspace.c. */
101 uint32_t *pagedir; /* Page directory. */
104 /* Owned by thread.c */
105 unsigned magic; /* Detects stack overflow. */
108 void thread_init (void);
109 void thread_start (void);
111 typedef void thread_func (void *aux);
112 tid_t thread_create (const char *name, int priority, thread_func *, void *);
114 tid_t thread_execute (const char *filename);
117 void thread_unblock (struct thread *);
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);
126 /* This function will be implemented in problem 1-2. */
127 void thread_join (tid_t);
129 /* These functions will be implemented in problem 1-3. */
130 void thread_set_priority (int);
131 int thread_get_priority (void);
133 #endif /* threads/thread.h */