Advertisement
Guest User

Untitled

a guest
Jul 28th, 2017
441
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.47 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.  
  8. /* States in a thread's life cycle. */
  9. enum thread_status
  10. {
  11. THREAD_RUNNING, /* Running thread. */
  12. THREAD_READY, /* Not running but ready to run. */
  13. THREAD_BLOCKED, /* Waiting for an event to trigger. */
  14. THREAD_DYING /* About to be destroyed. */
  15. };
  16.  
  17. /* Thread identifier type.
  18. You can redefine this to whatever type you like. */
  19. typedef int tid_t;
  20. #define TID_ERROR ((tid_t) -1) /* Error value for tid_t. */
  21.  
  22. /* Thread priorities. */
  23. #define PRI_MIN 0 /* Lowest priority. */
  24. #define PRI_DEFAULT 31 /* Default priority. */
  25. #define PRI_MAX 63 /* Highest priority. */
  26.  
  27. #ifdef USERPROG
  28. #include "userprog/process.h"
  29. #endif
  30.  
  31. /* A kernel thread or user process.
  32.  
  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:
  38.  
  39. 4 kB +---------------------------------+
  40. | kernel stack |
  41. | | |
  42. | | |
  43. | V |
  44. | grows downward |
  45. | |
  46. | |
  47. | |
  48. | |
  49. | |
  50. | |
  51. | |
  52. | |
  53. +---------------------------------+
  54. | magic |
  55. | : |
  56. | : |
  57. | name |
  58. | status |
  59. 0 kB +---------------------------------+
  60.  
  61. The upshot of this is twofold:
  62.  
  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
  67. kB.
  68.  
  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_page()
  74. instead.
  75.  
  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. */
  87. struct thread
  88. {
  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. */
  95. struct list_elem allelem; /* List element for all threads list. */
  96.  
  97. /* Shared between thread.c and synch.c. */
  98. struct list_elem elem; /* List element. */
  99.  
  100. #ifdef USERPROG
  101. /* Owned by userprog/process.c. */
  102. uint32_t *pagedir; /* Page directory. */
  103. struct process* process; /* This thread's associated process */
  104.  
  105. #endif
  106.  
  107. /* Owned by thread.c. */
  108. unsigned magic; /* Detects stack overflow. */
  109. };
  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 */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement