diff options
Diffstat (limited to 'pintos-progos/threads/thread.h')
| -rw-r--r-- | pintos-progos/threads/thread.h | 148 |
1 files changed, 0 insertions, 148 deletions
diff --git a/pintos-progos/threads/thread.h b/pintos-progos/threads/thread.h deleted file mode 100644 index 4ba5ef2..0000000 --- a/pintos-progos/threads/thread.h +++ /dev/null | |||
| @@ -1,148 +0,0 @@ | |||
| 1 | #ifndef THREADS_THREAD_H | ||
| 2 | #define THREADS_THREAD_H | ||
| 3 | |||
| 4 | #include <debug.h> | ||
| 5 | #include <list.h> | ||
| 6 | #include <stdint.h> | ||
| 7 | #include "threads/synch.h" | ||
| 8 | |||
| 9 | /* States in a thread's life cycle. */ | ||
| 10 | enum thread_status | ||
| 11 | { | ||
| 12 | THREAD_RUNNING, /* Running thread. */ | ||
| 13 | THREAD_READY, /* Not running but ready to run. */ | ||
| 14 | THREAD_BLOCKED, /* Waiting for an event to trigger. */ | ||
| 15 | THREAD_DYING /* About to be destroyed. */ | ||
| 16 | }; | ||
| 17 | |||
| 18 | /* Thread identifier type. | ||
| 19 | You can redefine this to whatever type you like. */ | ||
| 20 | typedef int tid_t; | ||
| 21 | #define TID_ERROR ((tid_t) -1) /* Error value for tid_t. */ | ||
| 22 | |||
| 23 | /* Thread priorities. */ | ||
| 24 | #define PRI_MIN 0 /* Lowest priority. */ | ||
| 25 | #define PRI_DEFAULT 31 /* Default priority. */ | ||
| 26 | #define PRI_MAX 63 /* Highest priority. */ | ||
| 27 | |||
| 28 | /* A kernel thread or user process. | ||
| 29 | |||
| 30 | Each thread structure is stored in its own 4 kB page. The | ||
| 31 | thread structure itself sits at the very bottom of the page | ||
| 32 | (at offset 0). The rest of the page is reserved for the | ||
| 33 | thread's kernel stack, which grows downward from the top of | ||
| 34 | the page (at offset 4 kB). Here's an illustration: | ||
| 35 | |||
| 36 | 4 kB +---------------------------------+ | ||
| 37 | | kernel stack | | ||
| 38 | | | | | ||
| 39 | | | | | ||
| 40 | | V | | ||
| 41 | | grows downward | | ||
| 42 | | | | ||
| 43 | | | | ||
| 44 | | | | ||
| 45 | | | | ||
| 46 | | | | ||
| 47 | | | | ||
| 48 | | | | ||
| 49 | | | | ||
| 50 | +---------------------------------+ | ||
| 51 | | magic | | ||
| 52 | | : | | ||
| 53 | | : | | ||
| 54 | | name | | ||
| 55 | | status | | ||
| 56 | 0 kB +---------------------------------+ | ||
| 57 | |||
| 58 | The upshot of this is twofold: | ||
| 59 | |||
| 60 | 1. First, `struct thread' must not be allowed to grow too | ||
| 61 | big. If it does, then there will not be enough room for | ||
| 62 | the kernel stack. Our base `struct thread' is only a | ||
| 63 | few bytes in size. It probably should stay well under 1 | ||
| 64 | kB. | ||
| 65 | |||
| 66 | 2. Second, kernel stacks must not be allowed to grow too | ||
| 67 | large. If a stack overflows, it will corrupt the thread | ||
| 68 | state. Thus, kernel functions should not allocate large | ||
| 69 | structures or arrays as non-static local variables. Use | ||
| 70 | dynamic allocation with malloc() or palloc_get_page() | ||
| 71 | instead. | ||
| 72 | |||
| 73 | The first symptom of either of these problems will probably be | ||
| 74 | an assertion failure in thread_current(), which checks that | ||
| 75 | the `magic' member of the running thread's `struct thread' is | ||
| 76 | set to THREAD_MAGIC. Stack overflow will normally change this | ||
| 77 | value, triggering the assertion. */ | ||
| 78 | /* The `elem' member has a dual purpose. It can be an element in | ||
| 79 | the run queue (thread.c), it can be an element in a semaphore | ||
| 80 | wait list (synch.c), or it can be an element in the timer/alarm sleep | ||
| 81 | list (timer.c). It can be used these three ways only because they | ||
| 82 | are mutually exclusive: only a thread in the ready state is on | ||
| 83 | the run queue, only a thread in the blocked state is on a semaphore | ||
| 84 | wait list, whereas only a thread waiting for an timer/alarm event is on | ||
| 85 | the timer/alarm sleep list. */ | ||
| 86 | struct thread | ||
| 87 | { | ||
| 88 | /* Owned by thread.c. */ | ||
| 89 | tid_t tid; /* Thread identifier. */ | ||
| 90 | enum thread_status status; /* Thread state. */ | ||
| 91 | char name[16]; /* Name (for debugging purposes). */ | ||
| 92 | uint8_t *stack; /* Saved stack pointer. */ | ||
| 93 | int priority; /* Priority. */ | ||
| 94 | struct list_elem allelem; /* List element for all threads list. */ | ||
| 95 | |||
| 96 | /* Shared between thread.c, synch.c and timer.c. */ | ||
| 97 | struct list_elem elem; /* List element. */ | ||
| 98 | |||
| 99 | #ifdef USERPROG | ||
| 100 | /* Owned by userprog/process.c */ | ||
| 101 | struct process* process; /* Process Structure */ | ||
| 102 | struct list children; /* Threads can hold processes, but not vice versa */ | ||
| 103 | uint32_t *pagedir; /* Page directory. */ | ||
| 104 | #endif | ||
| 105 | |||
| 106 | /* Owned by thread.c. */ | ||
| 107 | unsigned magic; /* Detects stack overflow. */ | ||
| 108 | |||
| 109 | int64_t wakeup_tick; /* absolute tick when to wake up the thread */ | ||
| 110 | }; | ||
| 111 | |||
| 112 | /* If false (default), use round-robin scheduler. | ||
| 113 | If true, use multi-level feedback queue scheduler. | ||
| 114 | Controlled by kernel command-line option "-o mlfqs". */ | ||
| 115 | extern bool thread_mlfqs; | ||
| 116 | |||
| 117 | void thread_init (void); | ||
| 118 | void thread_start (void); | ||
| 119 | |||
| 120 | void thread_tick (void); | ||
| 121 | void thread_print_stats (void); | ||
| 122 | |||
| 123 | typedef void thread_func (void *aux); | ||
| 124 | tid_t thread_create (const char *name, int priority, thread_func *, void *); | ||
| 125 | |||
| 126 | void thread_block (void); | ||
| 127 | void thread_unblock (struct thread *); | ||
| 128 | |||
| 129 | struct thread *thread_current (void); | ||
| 130 | tid_t thread_tid (void); | ||
| 131 | const char *thread_name (void); | ||
| 132 | |||
| 133 | void thread_exit (void) NO_RETURN; | ||
| 134 | void thread_yield (void); | ||
| 135 | |||
| 136 | /* Performs some operation on thread t, given auxiliary data AUX. */ | ||
| 137 | typedef void thread_action_func (struct thread *t, void *aux); | ||
| 138 | void thread_foreach (thread_action_func *, void *); | ||
| 139 | |||
| 140 | int thread_get_priority (void); | ||
| 141 | void thread_set_priority (int); | ||
| 142 | |||
| 143 | int thread_get_nice (void); | ||
| 144 | void thread_set_nice (int); | ||
| 145 | int thread_get_recent_cpu (void); | ||
| 146 | int thread_get_load_avg (void); | ||
| 147 | |||
| 148 | #endif /* threads/thread.h */ | ||
