thread.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. * Copyright (c) 2008-2009 Travis Geiselbrecht
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining
  5. * a copy of this software and associated documentation files
  6. * (the "Software"), to deal in the Software without restriction,
  7. * including without limitation the rights to use, copy, modify, merge,
  8. * publish, distribute, sublicense, and/or sell copies of the Software,
  9. * and to permit persons to whom the Software is furnished to do so,
  10. * subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be
  13. * included in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  18. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  19. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  20. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  21. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #ifndef __KERNEL_THREAD_H
  24. #define __KERNEL_THREAD_H
  25. #include <sys/types.h>
  26. #include <list.h>
  27. #include <compiler.h>
  28. #include <arch/ops.h>
  29. #include <arch/thread.h>
  30. enum thread_state {
  31. THREAD_SUSPENDED = 0,
  32. THREAD_READY,
  33. THREAD_RUNNING,
  34. THREAD_BLOCKED,
  35. THREAD_SLEEPING,
  36. THREAD_DEATH,
  37. };
  38. typedef int (*thread_start_routine)(void *arg);
  39. /* thread local storage */
  40. enum thread_tls_list {
  41. MAX_TLS_ENTRY
  42. };
  43. #define THREAD_MAGIC 'thrd'
  44. typedef struct thread {
  45. int magic;
  46. struct list_node thread_list_node;
  47. /* active bits */
  48. struct list_node queue_node;
  49. int priority;
  50. enum thread_state state;
  51. int saved_critical_section_count;
  52. int remaining_quantum;
  53. /* if blocked, a pointer to the wait queue */
  54. struct wait_queue *blocking_wait_queue;
  55. status_t wait_queue_block_ret;
  56. /* architecture stuff */
  57. struct arch_thread arch;
  58. /* stack stuff */
  59. void *stack;
  60. size_t stack_size;
  61. /* entry point */
  62. thread_start_routine entry;
  63. void *arg;
  64. /* return code */
  65. int retcode;
  66. /* thread local storage */
  67. uint32_t tls[MAX_TLS_ENTRY];
  68. char name[32];
  69. } thread_t;
  70. /* thread priority */
  71. #define NUM_PRIORITIES 32
  72. #define LOWEST_PRIORITY 0
  73. #define HIGHEST_PRIORITY (NUM_PRIORITIES - 1)
  74. #define DPC_PRIORITY HIGHEST_PRIORITY //(NUM_PRIORITIES - 2)
  75. #define IDLE_PRIORITY LOWEST_PRIORITY
  76. #define LOW_PRIORITY (NUM_PRIORITIES / 4)
  77. #define DEFAULT_PRIORITY (NUM_PRIORITIES / 2)
  78. #define HIGH_PRIORITY ((NUM_PRIORITIES / 4) * 3)
  79. /* stack size */
  80. #define DEFAULT_STACK_SIZE 8192
  81. /* functions */
  82. void thread_init_early(void);
  83. void thread_init(void);
  84. void thread_become_idle(void) __NO_RETURN;
  85. void thread_set_name(const char *name);
  86. void thread_set_priority(int priority);
  87. thread_t *thread_create(const char *name, thread_start_routine entry, void *arg, int priority, size_t stack_size);
  88. status_t thread_resume(thread_t *);
  89. void thread_exit(int retcode) __NO_RETURN;
  90. void thread_sleep(time_t delay);
  91. void dump_thread(thread_t *t);
  92. void dump_all_threads(void);
  93. void dump_target_threads_stack(void);
  94. /* scheduler routines */
  95. void thread_yield(void); /* give up the cpu voluntarily */
  96. void thread_preempt(void); /* get preempted (inserted into head of run queue) */
  97. void thread_block(void); /* block on something and reschedule */
  98. /* called on every timer tick for the scheduler to do quantum expiration */
  99. enum handler_return thread_timer_tick(void);
  100. /* the current thread */
  101. extern thread_t *current_thread;
  102. /* the idle thread */
  103. extern thread_t *idle_thread;
  104. /* critical sections */
  105. extern int critical_section_count;
  106. static inline __ALWAYS_INLINE void enter_critical_section(void)
  107. {
  108. critical_section_count++;
  109. if (critical_section_count == 1)
  110. arch_disable_ints();
  111. }
  112. static inline __ALWAYS_INLINE void exit_critical_section(void)
  113. {
  114. critical_section_count--;
  115. if (critical_section_count == 0)
  116. arch_enable_ints();
  117. }
  118. static inline __ALWAYS_INLINE bool in_critical_section(void)
  119. {
  120. return critical_section_count > 0;
  121. }
  122. /* only used by interrupt glue */
  123. static inline void inc_critical_section(void) { critical_section_count++; }
  124. static inline void dec_critical_section(void) { critical_section_count--; }
  125. /* thread local storage */
  126. static inline __ALWAYS_INLINE uint32_t tls_get(uint entry)
  127. {
  128. return current_thread->tls[entry];
  129. }
  130. static inline __ALWAYS_INLINE uint32_t tls_set(uint entry, uint32_t val)
  131. {
  132. uint32_t oldval = current_thread->tls[entry];
  133. current_thread->tls[entry] = val;
  134. return oldval;
  135. }
  136. /* wait queue stuff */
  137. #define WAIT_QUEUE_MAGIC 'wait'
  138. typedef struct wait_queue {
  139. int magic;
  140. struct list_node list;
  141. int count;
  142. } wait_queue_t;
  143. /* wait queue primitive */
  144. /* NOTE: must be inside critical section when using these */
  145. void wait_queue_init(wait_queue_t *);
  146. /*
  147. * release all the threads on this wait queue with a return code of ERR_OBJECT_DESTROYED.
  148. * the caller must assure that no other threads are operating on the wait queue during or
  149. * after the call.
  150. */
  151. void wait_queue_destroy(wait_queue_t *, bool reschedule);
  152. /*
  153. * block on a wait queue.
  154. * return status is whatever the caller of wait_queue_wake_*() specifies.
  155. * a timeout other than INFINITE_TIME will set abort after the specified time
  156. * and return ERR_TIMED_OUT. a timeout of 0 will immediately return.
  157. */
  158. status_t wait_queue_block(wait_queue_t *, time_t timeout);
  159. /*
  160. * release one or more threads from the wait queue.
  161. * reschedule = should the system reschedule if any is released.
  162. * wait_queue_error = what wait_queue_block() should return for the blocking thread.
  163. */
  164. int wait_queue_wake_one(wait_queue_t *, bool reschedule, status_t wait_queue_error);
  165. int wait_queue_wake_all(wait_queue_t *, bool reschedule, status_t wait_queue_error);
  166. /*
  167. * remove the thread from whatever wait queue it's in.
  168. * return an error if the thread is not currently blocked (or is the current thread)
  169. */
  170. status_t thread_unblock_from_wait_queue(thread_t *t, bool reschedule, status_t wait_queue_error);
  171. /* thread level statistics */
  172. #if DEBUGLEVEL > 1
  173. #define THREAD_STATS 0
  174. #else
  175. #define THREAD_STATS 0
  176. #endif
  177. #if THREAD_STATS
  178. struct thread_stats {
  179. bigtime_t idle_time;
  180. bigtime_t last_idle_timestamp;
  181. int reschedules;
  182. int context_switches;
  183. int preempts;
  184. int yields;
  185. int interrupts; /* platform code increment this */
  186. int timer_ints; /* timer code increment this */
  187. int timers; /* timer code increment this */
  188. };
  189. extern struct thread_stats thread_stats;
  190. #endif
  191. #endif