Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.27 KB | None | 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. #include "threads/fixed-point.h"
  9.  
  10. /* States in a thread's life cycle. */
  11. enum thread_status
  12. {
  13. THREAD_RUNNING, /* Running thread. */
  14. THREAD_READY, /* Not running but ready to run. */
  15. THREAD_BLOCKED, /* Waiting for an event to trigger. */
  16. THREAD_DYING /* About to be destroyed. */
  17. };
  18.  
  19. /* Thread identifier type.
  20. You can redefine this to whatever type you like. */
  21. typedef int tid_t;
  22. #define TID_ERROR ((tid_t) -1) /* Error value for tid_t. */
  23.  
  24. /* Thread priorities. */
  25. #define PRI_MIN 0 /* Lowest priority. */
  26. #define PRI_DEFAULT 31 /* Default priority. */
  27. #define PRI_MAX 63 /* Highest priority. */
  28.  
  29. /* A kernel thread or user process.
  30.  
  31. Each thread structure is stored in its own 4 kB page. The
  32. thread structure itself sits at the very bottom of the page
  33. (at offset 0). The rest of the page is reserved for the
  34. thread's kernel stack, which grows downward from the top of
  35. the page (at offset 4 kB). Here's an illustration:
  36.  
  37. 4 kB +---------------------------------+
  38. | kernel stack |
  39. | | |
  40. | | |
  41. | V |
  42. | grows downward |
  43. | |
  44. | |
  45. | |
  46. | |
  47. | |
  48. | |
  49. | |
  50. | |
  51. +---------------------------------+
  52. | magic |
  53. | : |
  54. | : |
  55. | name |
  56. | status |
  57. 0 kB +---------------------------------+
  58.  
  59. The upshot of this is twofold:
  60.  
  61. 1. First, `struct thread' must not be allowed to grow too
  62. big. If it does, then there will not be enough room for
  63. the kernel stack. Our base `struct thread' is only a
  64. few bytes in size. It probably should stay well under 1
  65. kB.
  66.  
  67. 2. Second, kernel stacks must not be allowed to grow too
  68. large. If a stack overflows, it will corrupt the thread
  69. state. Thus, kernel functions should not allocate large
  70. structures or arrays as non-static local variables. Use
  71. dynamic allocation with malloc() or palloc_get_page()
  72. instead.
  73.  
  74. The first symptom of either of these problems will probably be
  75. an assertion failure in thread_current(), which checks that
  76. the `magic' member of the running thread's `struct thread' is
  77. set to THREAD_MAGIC. Stack overflow will normally change this
  78. value, triggering the assertion. */
  79. /* The `elem' member has a dual purpose. It can be an element in
  80. the run queue (thread.c), or it can be an element in a
  81. semaphore wait list (synch.c). It can be used these two ways
  82. only because they are mutually exclusive: only a thread in the
  83. ready state is on the run queue, whereas only a thread in the
  84. blocked state is on a semaphore wait list. */
  85. struct thread
  86. {
  87. /* Owned by thread.c. */
  88. tid_t tid; /* Thread identifier. */
  89. enum thread_status status; /* Thread state. */
  90. char name[16]; /* Name (for debugging purposes). */
  91. uint8_t *stack; /* Saved stack pointer. */
  92. int priority; /* Priority. */
  93. int own_priority; /* priority that was assigned to*/
  94. int nice; /* Niceness of the thread */
  95. fixed_point_t recent_cpu; /* CPU time recently used by thread */
  96. struct list_elem allelem; /* List element for all threads list. */
  97.  
  98. /* Shared between thread.c and synch.c. */
  99. struct list_elem elem; /* List element. */
  100.  
  101. #ifdef USERPROG
  102. /* Owned by userprog/process.c. */
  103. uint32_t *pagedir; /* Page directory. */
  104. #endif
  105.  
  106. /* Owned by thread.c. */
  107. unsigned magic; /* Detects stack overflow. */
  108. uint64_t end_tick; /* Tick when the thread should wake up*/
  109. struct list held_locks; /* List of locks this thread holds*/
  110. struct semaphore* locked_on; /* Lock upon which the thread is waiting*/
  111. struct lock * locked_lock;
  112.  
  113. };
  114.  
  115. /* If false (default), use round-robin scheduler.
  116. If true, use multi-level feedback queue scheduler.
  117. Controlled by kernel command-line option "-o mlfqs". */
  118. extern bool thread_mlfqs;
  119.  
  120. void thread_init (void);
  121. void thread_start (void);
  122.  
  123. void thread_tick (void);
  124. void thread_print_stats (void);
  125.  
  126. typedef void thread_func (void *aux);
  127. tid_t thread_create (const char *name, int priority, thread_func *, void *);
  128.  
  129. void thread_block (void);
  130. void thread_unblock (struct thread *);
  131.  
  132. struct thread *thread_current (void);
  133. tid_t thread_tid (void);
  134. const char *thread_name (void);
  135.  
  136. void thread_exit (void) NO_RETURN;
  137. void thread_yield (void);
  138.  
  139. /* Performs some operation on thread t, given auxiliary data AUX. */
  140. typedef void thread_action_func (struct thread *t, void *aux);
  141. void thread_foreach (thread_action_func *, void *);
  142.  
  143. int thread_get_priority (void);
  144. void thread_set_priority (int);
  145.  
  146. int thread_get_nice (void);
  147. void thread_set_nice (int);
  148. int thread_get_recent_cpu (void);
  149. int thread_get_load_avg (void);
  150. void thread_update_load_avg(void);
  151. void thread_update_recent_cpu(void);
  152. void thread_update_priorities(void);
  153. void recent_cpu_formula(struct thread* t, void* aux);
  154. void priority_formula(struct thread* t, void* aux);
  155. bool has_higher_priority_than(struct list_elem* a, struct list_elem* b, void* aux);
  156. bool has_lower_priority_than(struct list_elem* a, struct list_elem* b, void* aux);
  157.  
  158. #endif /* threads/thread.h */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement