rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2008-2015 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 | |
| 26 | #include <sys/types.h> |
| 27 | #include <list.h> |
| 28 | #include <compiler.h> |
| 29 | #include <arch/defines.h> |
| 30 | #include <arch/ops.h> |
| 31 | #include <arch/thread.h> |
| 32 | #include <kernel/wait.h> |
| 33 | #include <kernel/spinlock.h> |
| 34 | #include <debug.h> |
| 35 | |
| 36 | __BEGIN_CDECLS; |
| 37 | |
| 38 | /* debug-enable runtime checks */ |
| 39 | #if LK_DEBUGLEVEL > 1 |
| 40 | #define THREAD_STATS 1 |
| 41 | #define THREAD_STACK_BOUNDS_CHECK 1 |
| 42 | #ifndef THREAD_STACK_PADDING_SIZE |
| 43 | #define THREAD_STACK_PADDING_SIZE 256 |
| 44 | #endif |
| 45 | #endif |
| 46 | |
| 47 | enum thread_state { |
| 48 | THREAD_SUSPENDED = 0, |
| 49 | THREAD_READY, |
| 50 | THREAD_RUNNING, |
| 51 | THREAD_BLOCKED, |
| 52 | THREAD_SLEEPING, |
| 53 | THREAD_DEATH, |
| 54 | }; |
| 55 | |
| 56 | typedef int (*thread_start_routine)(void *arg); |
| 57 | |
| 58 | /* thread local storage */ |
| 59 | enum thread_tls_list { |
| 60 | #ifdef WITH_LIB_UTHREAD |
| 61 | TLS_ENTRY_UTHREAD, |
| 62 | #endif |
| 63 | #ifdef WITH_LIB_LKUSER |
| 64 | TLS_ENTRY_LKUSER, |
| 65 | #endif |
| 66 | MAX_TLS_ENTRY |
| 67 | }; |
| 68 | |
| 69 | #define THREAD_FLAG_DETACHED (1<<0) |
| 70 | #define THREAD_FLAG_FREE_STACK (1<<1) |
| 71 | #define THREAD_FLAG_FREE_STRUCT (1<<2) |
| 72 | #define THREAD_FLAG_REAL_TIME (1<<3) |
| 73 | #define THREAD_FLAG_IDLE (1<<4) |
| 74 | #define THREAD_FLAG_DEBUG_STACK_BOUNDS_CHECK (1<<5) |
| 75 | |
| 76 | #define THREAD_MAGIC 'thrd' |
| 77 | |
| 78 | typedef struct thread { |
| 79 | int magic; |
| 80 | struct list_node thread_list_node; |
| 81 | |
| 82 | /* active bits */ |
| 83 | struct list_node queue_node; |
| 84 | int priority; |
| 85 | enum thread_state state; |
| 86 | int remaining_quantum; |
| 87 | unsigned int flags; |
| 88 | #if WITH_SMP |
| 89 | int curr_cpu; |
| 90 | int pinned_cpu; /* only run on pinned_cpu if >= 0 */ |
| 91 | #endif |
| 92 | |
| 93 | /* if blocked, a pointer to the wait queue */ |
| 94 | struct wait_queue *blocking_wait_queue; |
| 95 | status_t wait_queue_block_ret; |
| 96 | |
| 97 | /* architecture stuff */ |
| 98 | struct arch_thread arch; |
| 99 | |
| 100 | /* stack stuff */ |
| 101 | void *stack; |
| 102 | size_t stack_size; |
| 103 | |
| 104 | /* entry point */ |
| 105 | thread_start_routine entry; |
| 106 | void *arg; |
| 107 | |
| 108 | /* return code */ |
| 109 | int retcode; |
| 110 | struct wait_queue retcode_wait_queue; |
| 111 | |
| 112 | /* thread local storage */ |
| 113 | uintptr_t tls[MAX_TLS_ENTRY]; |
| 114 | |
| 115 | char name[32]; |
| 116 | } thread_t; |
| 117 | |
| 118 | #if WITH_SMP |
| 119 | #define thread_curr_cpu(t) ((t)->curr_cpu) |
| 120 | #define thread_pinned_cpu(t) ((t)->pinned_cpu) |
| 121 | #define thread_set_curr_cpu(t,c) ((t)->curr_cpu = (c)) |
| 122 | #define thread_set_pinned_cpu(t, c) ((t)->pinned_cpu = (c)) |
| 123 | #else |
| 124 | #define thread_curr_cpu(t) (0) |
| 125 | #define thread_pinned_cpu(t) (-1) |
| 126 | #define thread_set_curr_cpu(t,c) do {} while(0) |
| 127 | #define thread_set_pinned_cpu(t, c) do {} while(0) |
| 128 | #endif |
| 129 | |
| 130 | /* thread priority */ |
| 131 | #define NUM_PRIORITIES 32 |
| 132 | #define LOWEST_PRIORITY 0 |
| 133 | #define HIGHEST_PRIORITY (NUM_PRIORITIES - 1) |
| 134 | #define DPC_PRIORITY (NUM_PRIORITIES - 2) |
| 135 | #define IDLE_PRIORITY LOWEST_PRIORITY |
| 136 | #define LOW_PRIORITY (NUM_PRIORITIES / 4) |
| 137 | #define DEFAULT_PRIORITY (NUM_PRIORITIES / 2) |
| 138 | #define HIGH_PRIORITY ((NUM_PRIORITIES / 4) * 3) |
| 139 | |
| 140 | /* stack size */ |
| 141 | #ifdef CUSTOM_DEFAULT_STACK_SIZE |
| 142 | #define DEFAULT_STACK_SIZE CUSTOM_DEFAULT_STACK_SIZE |
| 143 | #else |
| 144 | #define DEFAULT_STACK_SIZE ARCH_DEFAULT_STACK_SIZE |
| 145 | #endif |
| 146 | |
| 147 | /* functions */ |
| 148 | void thread_init_early(void); |
| 149 | void thread_init(void); |
| 150 | void thread_become_idle(void) __NO_RETURN; |
| 151 | void thread_secondary_cpu_init_early(void); |
| 152 | void thread_secondary_cpu_entry(void) __NO_RETURN; |
| 153 | void thread_set_name(const char *name); |
| 154 | void thread_set_priority(int priority); |
| 155 | thread_t *thread_create(const char *name, thread_start_routine entry, void *arg, int priority, size_t stack_size); |
| 156 | thread_t *thread_create_etc(thread_t *t, const char *name, thread_start_routine entry, void *arg, int priority, void *stack, size_t stack_size); |
| 157 | status_t thread_resume(thread_t *); |
| 158 | void thread_exit(int retcode) __NO_RETURN; |
| 159 | void thread_sleep(lk_time_t delay); |
| 160 | status_t thread_detach(thread_t *t); |
| 161 | status_t thread_join(thread_t *t, int *retcode, lk_time_t timeout); |
| 162 | status_t thread_detach_and_resume(thread_t *t); |
| 163 | status_t thread_set_real_time(thread_t *t); |
| 164 | |
| 165 | void dump_thread(thread_t *t); |
| 166 | void arch_dump_thread(thread_t *t); |
| 167 | void dump_all_threads(void); |
| 168 | |
| 169 | /* scheduler routines */ |
| 170 | void thread_yield(void); /* give up the cpu voluntarily */ |
| 171 | void thread_preempt(void); /* get preempted (inserted into head of run queue) */ |
| 172 | void thread_block(void); /* block on something and reschedule */ |
| 173 | void thread_unblock(thread_t *t, bool resched); /* go back in the run queue */ |
| 174 | |
| 175 | #ifdef WITH_LIB_UTHREAD |
| 176 | void uthread_context_switch(thread_t *oldthread, thread_t *newthread); |
| 177 | #endif |
| 178 | |
| 179 | /* called on every timer tick for the scheduler to do quantum expiration */ |
| 180 | struct timer; |
| 181 | enum handler_return thread_timer_tick(struct timer *, lk_time_t now, void *arg); |
| 182 | |
| 183 | /* the current thread */ |
| 184 | thread_t *get_current_thread(void); |
| 185 | void set_current_thread(thread_t *); |
| 186 | |
| 187 | /* scheduler lock */ |
| 188 | extern spin_lock_t thread_lock; |
| 189 | |
| 190 | #define THREAD_LOCK(state) spin_lock_saved_state_t state; spin_lock_irqsave(&thread_lock, state) |
| 191 | #define THREAD_UNLOCK(state) spin_unlock_irqrestore(&thread_lock, state) |
| 192 | |
| 193 | /* thread local storage */ |
| 194 | static inline __ALWAYS_INLINE uintptr_t tls_get(uint entry) |
| 195 | { |
| 196 | return get_current_thread()->tls[entry]; |
| 197 | } |
| 198 | |
| 199 | static inline __ALWAYS_INLINE uintptr_t __tls_set(uint entry, uintptr_t val) |
| 200 | { |
| 201 | uintptr_t oldval = get_current_thread()->tls[entry]; |
| 202 | get_current_thread()->tls[entry] = val; |
| 203 | return oldval; |
| 204 | } |
| 205 | |
| 206 | #define tls_set(e,v) \ |
| 207 | ({ \ |
| 208 | STATIC_ASSERT((e) < MAX_TLS_ENTRY); \ |
| 209 | __tls_set(e, v); \ |
| 210 | }) |
| 211 | |
| 212 | /* thread level statistics */ |
| 213 | #if THREAD_STATS |
| 214 | struct thread_stats { |
| 215 | lk_bigtime_t idle_time; |
| 216 | lk_bigtime_t last_idle_timestamp; |
| 217 | ulong reschedules; |
| 218 | ulong context_switches; |
| 219 | ulong preempts; |
| 220 | ulong yields; |
| 221 | ulong interrupts; /* platform code increment this */ |
| 222 | ulong timer_ints; /* timer code increment this */ |
| 223 | ulong timers; /* timer code increment this */ |
| 224 | |
| 225 | #if WITH_SMP |
| 226 | ulong reschedule_ipis; |
| 227 | #endif |
| 228 | }; |
| 229 | |
| 230 | extern struct thread_stats thread_stats[SMP_MAX_CPUS]; |
| 231 | |
| 232 | #define THREAD_STATS_INC(name) do { thread_stats[arch_curr_cpu_num()].name++; } while(0) |
| 233 | |
| 234 | #else |
| 235 | |
| 236 | #define THREAD_STATS_INC(name) do { } while (0) |
| 237 | |
| 238 | #endif |
| 239 | |
| 240 | __END_CDECLS; |
| 241 | |
| 242 | #endif |
| 243 | |
| 244 | /* vim: set ts=4 sw=4 noexpandtab: */ |