1 #ifndef HEADER_THREAD_H
2 #define HEADER_THREAD_H 1
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 /* A kernel thread or user process.
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:
29 4 kB +---------------------------------+
43 +---------------------------------+
49 0 kB +---------------------------------+
51 The upshot of this is twofold:
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
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()
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. */
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. */
80 /* These members are owned by the addrspace_*() functions. */
81 uint32_t *pagedir; /* Page directory. */
84 /* Marker to detect stack overflow. */
85 unsigned magic; /* Always set to THREAD_MAGIC. */
88 void thread_init (void);
89 void thread_start (void);
91 typedef void thread_func (void *aux);
92 struct thread *thread_create (const char *name, thread_func *, void *);
94 bool thread_execute (const char *filename);
97 void thread_unblock (struct thread *);
98 const char *thread_name (struct thread *);
100 struct thread *thread_current (void);
101 void thread_exit (void) NO_RETURN;
102 void thread_yield (void);
103 void thread_block (void);
105 #endif /* thread.h */