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