thread.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
  3. * The President and Fellows of Harvard College.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the University nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. #ifndef _THREAD_H_
  30. #define _THREAD_H_
  31. /*
  32. * Definition of a thread.
  33. *
  34. * Note: curthread is defined by <current.h>.
  35. */
  36. #include <array.h>
  37. #include <spinlock.h>
  38. #include <threadlist.h>
  39. struct cpu;
  40. /* get machine-dependent defs */
  41. #include <machine/thread.h>
  42. /* Size of kernel stacks; must be power of 2 */
  43. #define STACK_SIZE 4096
  44. /* Mask for extracting the stack base address of a kernel stack pointer */
  45. #define STACK_MASK (~(vaddr_t)(STACK_SIZE-1))
  46. /* Macro to test if two addresses are on the same kernel stack */
  47. #define SAME_STACK(p1, p2) (((p1) & STACK_MASK) == ((p2) & STACK_MASK))
  48. /* States a thread can be in. */
  49. typedef enum
  50. {
  51. S_RUN, /* running */
  52. S_READY, /* ready to run */
  53. S_SLEEP, /* sleeping */
  54. S_ZOMBIE, /* zombie; exited but not yet deleted */
  55. } threadstate_t;
  56. /* Thread structure. */
  57. struct thread
  58. {
  59. /*
  60. * These go up front so they're easy to get to even if the
  61. * debugger is messed up.
  62. */
  63. char * t_name; /* Name of this thread */
  64. const char * t_wchan_name; /* Name of wait channel, if sleeping */
  65. threadstate_t t_state; /* State this thread is in */
  66. /*
  67. * Thread subsystem internal fields.
  68. */
  69. struct thread_machdep t_machdep; /* Any machine-dependent goo */
  70. struct threadlistnode t_listnode; /* Link for run/sleep/zombie lists */
  71. void * t_stack; /* Kernel-level stack */
  72. struct switchframe * t_context; /* Saved register context (on stack) */
  73. struct cpu * t_cpu; /* CPU thread runs on */
  74. struct proc * t_proc; /* Process thread belongs to */
  75. /*
  76. * Interrupt state fields.
  77. *
  78. * t_in_interrupt is true if current execution is in an
  79. * interrupt handler, which means the thread's normal context
  80. * of execution is stopped somewhere in the middle of doing
  81. * something else. This makes assorted operations unsafe.
  82. *
  83. * See notes in spinlock.c regarding t_curspl and t_iplhigh_count.
  84. *
  85. * Exercise for the student: why is this material per-thread
  86. * rather than per-cpu or global?
  87. */
  88. bool t_in_interrupt; /* Are we in an interrupt? */
  89. int t_curspl; /* Current spl*() state */
  90. int t_iplhigh_count; /* # of times IPL has been raised */
  91. /*
  92. * Public fields
  93. */
  94. /* add more here as needed */
  95. };
  96. /*
  97. * Array of threads.
  98. */
  99. #ifndef THREADINLINE
  100. #define THREADINLINE INLINE
  101. #endif
  102. DECLARRAY(thread);
  103. DEFARRAY(thread, THREADINLINE);
  104. /* Call once during system startup to allocate data structures. */
  105. void thread_bootstrap(void);
  106. /* Call late in system startup to get secondary CPUs running. */
  107. void thread_start_cpus(void);
  108. /* Call during panic to stop other threads in their tracks */
  109. void thread_panic(void);
  110. /* Call during system shutdown to offline other CPUs. */
  111. void thread_shutdown(void);
  112. /*
  113. * Make a new thread, which will start executing at "func". The thread
  114. * will belong to the process "proc", or to the current thread's
  115. * process if "proc" is null. The "data" arguments (one pointer, one
  116. * number) are passed to the function. The current thread is used as a
  117. * prototype for creating the new one. Returns an error code. The
  118. * thread structure for the new thread is not returned; it is not in
  119. * general safe to refer to it as the new thread may exit and
  120. * disappear at any time without notice.
  121. */
  122. int thread_fork(const char *name, struct proc *proc,
  123. void (*func)(void *, unsigned long),
  124. void *data1, unsigned long data2);
  125. /*
  126. * Cause the current thread to exit.
  127. * Interrupts need not be disabled.
  128. */
  129. void thread_exit(void);
  130. /*
  131. * Cause the current thread to yield to the next runnable thread, but
  132. * itself stay runnable.
  133. * Interrupts need not be disabled.
  134. */
  135. void thread_yield(void);
  136. /*
  137. * Reshuffle the run queue. Called from the timer interrupt.
  138. */
  139. void schedule(void);
  140. /*
  141. * Potentially migrate ready threads to other CPUs. Called from the
  142. * timer interrupt.
  143. */
  144. void thread_consider_migration(void);
  145. #endif /* _THREAD_H_ */