Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 1 | /* |
Travis Geiselbrecht | 9045c06 | 2009-06-28 11:20:09 -0700 | [diff] [blame] | 2 | * Copyright (c) 2008-2009 Travis Geiselbrecht |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 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 | |
| 26 | #include <sys/types.h> |
| 27 | #include <list.h> |
| 28 | #include <compiler.h> |
| 29 | #include <arch/ops.h> |
| 30 | #include <arch/thread.h> |
| 31 | |
| 32 | enum thread_state { |
| 33 | THREAD_SUSPENDED = 0, |
| 34 | THREAD_READY, |
| 35 | THREAD_RUNNING, |
| 36 | THREAD_BLOCKED, |
| 37 | THREAD_SLEEPING, |
| 38 | THREAD_DEATH, |
| 39 | }; |
| 40 | |
| 41 | typedef int (*thread_start_routine)(void *arg); |
| 42 | |
travis geiselbrecht | c60a2e6 | 2008-12-22 23:46:26 +0000 | [diff] [blame] | 43 | /* thread local storage */ |
| 44 | enum thread_tls_list { |
| 45 | MAX_TLS_ENTRY |
| 46 | }; |
| 47 | |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 48 | #define THREAD_MAGIC 'thrd' |
| 49 | |
| 50 | typedef struct thread { |
| 51 | int magic; |
| 52 | struct list_node thread_list_node; |
| 53 | |
| 54 | /* active bits */ |
| 55 | struct list_node queue_node; |
| 56 | int priority; |
| 57 | enum thread_state state; |
| 58 | int saved_critical_section_count; |
| 59 | int remaining_quantum; |
Travis Geiselbrecht | 887061f | 2008-09-05 01:47:07 -0700 | [diff] [blame] | 60 | |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 61 | /* if blocked, a pointer to the wait queue */ |
| 62 | struct wait_queue *blocking_wait_queue; |
| 63 | status_t wait_queue_block_ret; |
| 64 | |
| 65 | /* architecture stuff */ |
| 66 | struct arch_thread arch; |
| 67 | |
| 68 | /* stack stuff */ |
| 69 | void *stack; |
| 70 | size_t stack_size; |
| 71 | |
| 72 | /* entry point */ |
| 73 | thread_start_routine entry; |
| 74 | void *arg; |
| 75 | |
| 76 | /* return code */ |
| 77 | int retcode; |
| 78 | |
travis geiselbrecht | c60a2e6 | 2008-12-22 23:46:26 +0000 | [diff] [blame] | 79 | /* thread local storage */ |
| 80 | uint32_t tls[MAX_TLS_ENTRY]; |
| 81 | |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 82 | char name[32]; |
| 83 | } thread_t; |
| 84 | |
| 85 | /* thread priority */ |
| 86 | #define NUM_PRIORITIES 32 |
| 87 | #define LOWEST_PRIORITY 0 |
| 88 | #define HIGHEST_PRIORITY (NUM_PRIORITIES - 1) |
| 89 | #define DPC_PRIORITY (NUM_PRIORITIES - 2) |
| 90 | #define IDLE_PRIORITY LOWEST_PRIORITY |
| 91 | #define LOW_PRIORITY (NUM_PRIORITIES / 4) |
| 92 | #define DEFAULT_PRIORITY (NUM_PRIORITIES / 2) |
| 93 | #define HIGH_PRIORITY ((NUM_PRIORITIES / 4) * 3) |
| 94 | |
Mayank Grover | d6e4da2 | 2017-12-19 14:58:51 +0530 | [diff] [blame] | 95 | /* stack size - 12kb per thread (12 * 1024) */ |
| 96 | #define DEFAULT_STACK_SIZE 12288 |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 97 | |
| 98 | /* functions */ |
travis geiselbrecht | 858b1ad | 2008-12-23 21:55:41 +0000 | [diff] [blame] | 99 | void thread_init_early(void); |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 100 | void thread_init(void); |
| 101 | void thread_become_idle(void) __NO_RETURN; |
| 102 | void thread_set_name(const char *name); |
| 103 | void thread_set_priority(int priority); |
| 104 | thread_t *thread_create(const char *name, thread_start_routine entry, void *arg, int priority, size_t stack_size); |
| 105 | status_t thread_resume(thread_t *); |
| 106 | void thread_exit(int retcode) __NO_RETURN; |
| 107 | void thread_sleep(time_t delay); |
| 108 | |
| 109 | void dump_thread(thread_t *t); |
| 110 | void dump_all_threads(void); |
| 111 | |
| 112 | /* scheduler routines */ |
| 113 | void thread_yield(void); /* give up the cpu voluntarily */ |
| 114 | void thread_preempt(void); /* get preempted (inserted into head of run queue) */ |
| 115 | void thread_block(void); /* block on something and reschedule */ |
| 116 | |
| 117 | /* called on every timer tick for the scheduler to do quantum expiration */ |
| 118 | enum handler_return thread_timer_tick(void); |
| 119 | |
| 120 | /* the current thread */ |
| 121 | extern thread_t *current_thread; |
| 122 | |
| 123 | /* the idle thread */ |
| 124 | extern thread_t *idle_thread; |
| 125 | |
| 126 | /* critical sections */ |
| 127 | extern int critical_section_count; |
| 128 | |
Travis Geiselbrecht | 887061f | 2008-09-05 01:47:07 -0700 | [diff] [blame] | 129 | static inline __ALWAYS_INLINE void enter_critical_section(void) |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 130 | { |
| 131 | critical_section_count++; |
| 132 | if (critical_section_count == 1) |
| 133 | arch_disable_ints(); |
| 134 | } |
| 135 | |
Travis Geiselbrecht | 887061f | 2008-09-05 01:47:07 -0700 | [diff] [blame] | 136 | static inline __ALWAYS_INLINE void exit_critical_section(void) |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 137 | { |
| 138 | critical_section_count--; |
| 139 | if (critical_section_count == 0) |
| 140 | arch_enable_ints(); |
| 141 | } |
| 142 | |
Travis Geiselbrecht | 887061f | 2008-09-05 01:47:07 -0700 | [diff] [blame] | 143 | static inline __ALWAYS_INLINE bool in_critical_section(void) |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 144 | { |
| 145 | return critical_section_count > 0; |
| 146 | } |
| 147 | |
| 148 | /* only used by interrupt glue */ |
| 149 | static inline void inc_critical_section(void) { critical_section_count++; } |
| 150 | static inline void dec_critical_section(void) { critical_section_count--; } |
| 151 | |
travis geiselbrecht | c60a2e6 | 2008-12-22 23:46:26 +0000 | [diff] [blame] | 152 | /* thread local storage */ |
| 153 | static inline __ALWAYS_INLINE uint32_t tls_get(uint entry) |
| 154 | { |
| 155 | return current_thread->tls[entry]; |
| 156 | } |
| 157 | |
| 158 | static inline __ALWAYS_INLINE uint32_t tls_set(uint entry, uint32_t val) |
| 159 | { |
| 160 | uint32_t oldval = current_thread->tls[entry]; |
| 161 | current_thread->tls[entry] = val; |
| 162 | return oldval; |
| 163 | } |
| 164 | |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 165 | /* wait queue stuff */ |
| 166 | #define WAIT_QUEUE_MAGIC 'wait' |
| 167 | |
| 168 | typedef struct wait_queue { |
| 169 | int magic; |
| 170 | struct list_node list; |
| 171 | int count; |
| 172 | } wait_queue_t; |
| 173 | |
| 174 | /* wait queue primitive */ |
| 175 | /* NOTE: must be inside critical section when using these */ |
| 176 | void wait_queue_init(wait_queue_t *); |
| 177 | |
| 178 | /* |
| 179 | * release all the threads on this wait queue with a return code of ERR_OBJECT_DESTROYED. |
| 180 | * the caller must assure that no other threads are operating on the wait queue during or |
| 181 | * after the call. |
| 182 | */ |
| 183 | void wait_queue_destroy(wait_queue_t *, bool reschedule); |
| 184 | |
| 185 | /* |
| 186 | * block on a wait queue. |
| 187 | * return status is whatever the caller of wait_queue_wake_*() specifies. |
| 188 | * a timeout other than INFINITE_TIME will set abort after the specified time |
| 189 | * and return ERR_TIMED_OUT. a timeout of 0 will immediately return. |
| 190 | */ |
| 191 | status_t wait_queue_block(wait_queue_t *, time_t timeout); |
| 192 | |
| 193 | /* |
| 194 | * release one or more threads from the wait queue. |
| 195 | * reschedule = should the system reschedule if any is released. |
| 196 | * wait_queue_error = what wait_queue_block() should return for the blocking thread. |
| 197 | */ |
| 198 | int wait_queue_wake_one(wait_queue_t *, bool reschedule, status_t wait_queue_error); |
| 199 | int wait_queue_wake_all(wait_queue_t *, bool reschedule, status_t wait_queue_error); |
| 200 | |
| 201 | /* |
| 202 | * remove the thread from whatever wait queue it's in. |
| 203 | * return an error if the thread is not currently blocked (or is the current thread) |
| 204 | */ |
| 205 | status_t thread_unblock_from_wait_queue(thread_t *t, bool reschedule, status_t wait_queue_error); |
| 206 | |
| 207 | /* thread level statistics */ |
Travis Geiselbrecht | 9045c06 | 2009-06-28 11:20:09 -0700 | [diff] [blame] | 208 | #if DEBUGLEVEL > 1 |
| 209 | #define THREAD_STATS 1 |
| 210 | #else |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 211 | #define THREAD_STATS 0 |
Travis Geiselbrecht | 9045c06 | 2009-06-28 11:20:09 -0700 | [diff] [blame] | 212 | #endif |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 213 | #if THREAD_STATS |
| 214 | struct thread_stats { |
| 215 | bigtime_t idle_time; |
| 216 | bigtime_t last_idle_timestamp; |
| 217 | int reschedules; |
| 218 | int context_switches; |
| 219 | int preempts; |
| 220 | int yields; |
| 221 | int interrupts; /* platform code increment this */ |
| 222 | int timer_ints; /* timer code increment this */ |
| 223 | int timers; /* timer code increment this */ |
| 224 | }; |
| 225 | |
| 226 | extern struct thread_stats thread_stats; |
| 227 | |
| 228 | #endif |
| 229 | |
| 230 | #endif |
| 231 | |