Add comments.
[pintos-anon] / src / threads / thread.h
1 #ifndef HEADER_THREAD_H
2 #define HEADER_THREAD_H 1
3
4 #include <stdint.h>
5 #include "debug.h"
6 #include "list.h"
7
8 #ifdef USERPROG
9 #include "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 /* A kernel thread or user process.
22
23    Each thread structure is stored in its own 4 kB page.  The
24    thread structure itself sits at the very bottom of the page
25    (at offset 0).  The rest of the page is reserved for the
26    thread's kernel stack, which grows downward from the top of
27    the page (at offset 4 kB).  Here's an illustration:
28
29         4 kB +---------------------------------+
30              |          kernel stack           |
31              |                |                |
32              |                |                |
33              |                V                |
34              |         grows downward          |
35              |                                 |
36              |                                 |
37              |                                 |
38              |                                 |
39              |                                 |
40              |                                 |
41              |                                 |
42              |                                 |
43              +---------------------------------+
44              |             magic               |
45              |               :                 |
46              |               :                 |
47              |              name               |
48              |             status              |
49         0 kB +---------------------------------+
50
51    The upshot of this is twofold:
52
53       1. First, `struct thread' must not be allowed to grow too
54          big.  If it does, then there will not be enough room for
55          the kernel stack.  Our base `struct thread' is only a
56          few bytes in size.  It probably should stay well under 1
57          kB.
58
59       2. Second, kernel stacks must not be allowed to grow too
60          large.  If a stack overflows, it will corrupt the thread
61          state.  Thus, kernel functions should not allocate large
62          structures or arrays as non-static local variables.  Use
63          dynamic allocation with malloc() or palloc_get()
64          instead.
65
66    The first symptom of either of these problems will probably be
67    an assertion failure in thread_current(), which checks that
68    the `magic' member of the running thread's `struct thread' is
69    set to THREAD_MAGIC.  Stack overflow will normally change this
70    value, triggering the assertion. */
71 struct thread
72   {
73     /* These members are owned by the thread_*() functions. */
74     enum thread_status status;          /* Thread state. */
75     char name[16];                      /* Name (for debugging purposes). */
76     uint8_t *stack;                     /* Saved stack pointer. */
77     list_elem rq_elem;                  /* Run queue list element. */
78
79 #ifdef USERPROG
80     /* These members are owned by the addrspace_*() functions. */
81     uint32_t *pagedir;                  /* Page directory. */
82 #endif
83
84     /* Marker to detect stack overflow. */
85     unsigned magic;                     /* Always set to THREAD_MAGIC. */
86   };
87
88 void thread_init (void);
89 void thread_start (void);
90
91 typedef void thread_func (void *aux);
92 struct thread *thread_create (const char *name, thread_func *, void *);
93 #ifdef USERPROG
94 bool thread_execute (const char *filename);
95 #endif
96
97 void thread_unblock (struct thread *);
98 const char *thread_name (struct thread *);
99
100 struct thread *thread_current (void);
101 void thread_exit (void) NO_RETURN;
102 void thread_yield (void);
103 void thread_block (void);
104
105 #endif /* thread.h */