[f761f1eb] | 1 | /*
|
---|
[ea7890e7] | 2 | * Copyright (c) 2001-2007 Jakub Jermar
|
---|
[f761f1eb] | 3 | * All rights reserved.
|
---|
| 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 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
[174156fd] | 29 | /** @addtogroup kernel_generic_proc
|
---|
[b45c443] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
| 33 | */
|
---|
| 34 |
|
---|
[06e1e95] | 35 | #ifndef KERN_THREAD_H_
|
---|
| 36 | #define KERN_THREAD_H_
|
---|
[f761f1eb] | 37 |
|
---|
[b3f8fb7] | 38 | #include <synch/waitq.h>
|
---|
| 39 | #include <proc/task.h>
|
---|
[831a04d0] | 40 | #include <time/timeout.h>
|
---|
[b3f8fb7] | 41 | #include <cpu.h>
|
---|
[ea7890e7] | 42 | #include <synch/spinlock.h>
|
---|
[ef1eab7] | 43 | #include <adt/odict.h>
|
---|
[f76fed4] | 44 | #include <mm/slab.h>
|
---|
[b3f8fb7] | 45 | #include <arch/cpu.h>
|
---|
| 46 | #include <mm/tlb.h>
|
---|
[c0699467] | 47 | #include <abi/proc/uarg.h>
|
---|
[9a1b20c] | 48 | #include <udebug/udebug.h>
|
---|
[9ba415e] | 49 | #include <abi/proc/thread.h>
|
---|
[c0699467] | 50 | #include <abi/sysinfo.h>
|
---|
[1066041] | 51 | #include <arch.h>
|
---|
| 52 |
|
---|
[a6e55886] | 53 | #define THREAD CURRENT->thread
|
---|
[f761f1eb] | 54 |
|
---|
[da1bafb] | 55 | #define THREAD_NAME_BUFLEN 20
|
---|
[f761f1eb] | 56 |
|
---|
[a000878c] | 57 | extern const char *thread_states[];
|
---|
[f761f1eb] | 58 |
|
---|
[32fffef0] | 59 | /* Thread flags */
|
---|
[6eef3c4] | 60 | typedef enum {
|
---|
| 61 | THREAD_FLAG_NONE = 0,
|
---|
| 62 | /** Thread executes in user space. */
|
---|
| 63 | THREAD_FLAG_USPACE = (1 << 0),
|
---|
| 64 | /** Thread will be attached by the caller. */
|
---|
| 65 | THREAD_FLAG_NOATTACH = (1 << 1),
|
---|
| 66 | /** Thread accounting doesn't affect accumulated task accounting. */
|
---|
| 67 | THREAD_FLAG_UNCOUNTED = (1 << 2)
|
---|
| 68 | } thread_flags_t;
|
---|
[f761f1eb] | 69 |
|
---|
[4c60255] | 70 | /** Thread structure. There is one per thread. */
|
---|
| 71 | typedef struct thread {
|
---|
[da1bafb] | 72 | link_t rq_link; /**< Run queue link. */
|
---|
| 73 | link_t wq_link; /**< Wait queue link. */
|
---|
| 74 | link_t th_link; /**< Links to threads within containing task. */
|
---|
[a35b458] | 75 |
|
---|
[ef1eab7] | 76 | /** Link to @c threads ordered dictionary. */
|
---|
| 77 | odlink_t lthreads;
|
---|
[a35b458] | 78 |
|
---|
[4c60255] | 79 | /** Lock protecting thread structure.
|
---|
| 80 | *
|
---|
| 81 | * Protects the whole thread structure except list links above.
|
---|
| 82 | */
|
---|
[da1bafb] | 83 | IRQ_SPINLOCK_DECLARE(lock);
|
---|
[a35b458] | 84 |
|
---|
[4c60255] | 85 | char name[THREAD_NAME_BUFLEN];
|
---|
[a35b458] | 86 |
|
---|
[80bcaed] | 87 | /** Function implementing the thread. */
|
---|
[df58e44] | 88 | void (*thread_code)(void *);
|
---|
[80bcaed] | 89 | /** Argument passed to thread_code() function. */
|
---|
| 90 | void *thread_arg;
|
---|
[a35b458] | 91 |
|
---|
[80bcaed] | 92 | /**
|
---|
[df58e44] | 93 | * From here, the stored context is restored
|
---|
| 94 | * when the thread is scheduled.
|
---|
[80bcaed] | 95 | */
|
---|
[4c60255] | 96 | context_t saved_context;
|
---|
[a35b458] | 97 |
|
---|
[80bcaed] | 98 | /**
|
---|
[df58e44] | 99 | * From here, the stored timeout context
|
---|
| 100 | * is restored when sleep times out.
|
---|
[80bcaed] | 101 | */
|
---|
[4c60255] | 102 | context_t sleep_timeout_context;
|
---|
[a35b458] | 103 |
|
---|
[80bcaed] | 104 | /**
|
---|
[df58e44] | 105 | * From here, the stored interruption context
|
---|
| 106 | * is restored when sleep is interrupted.
|
---|
[80bcaed] | 107 | */
|
---|
[4c60255] | 108 | context_t sleep_interruption_context;
|
---|
[a35b458] | 109 |
|
---|
[80bcaed] | 110 | /** If true, the thread can be interrupted from sleep. */
|
---|
| 111 | bool sleep_interruptible;
|
---|
[b59318e] | 112 |
|
---|
| 113 | /**
|
---|
| 114 | * If true, and this thread's sleep returns without a wakeup
|
---|
| 115 | * (timed out or interrupted), waitq ignores the next wakeup.
|
---|
| 116 | * This is necessary for futex to be able to handle those conditions.
|
---|
| 117 | */
|
---|
| 118 | bool sleep_composable;
|
---|
| 119 |
|
---|
[80bcaed] | 120 | /** Wait queue in which this thread sleeps. */
|
---|
| 121 | waitq_t *sleep_queue;
|
---|
| 122 | /** Timeout used for timeoutable sleeping. */
|
---|
| 123 | timeout_t sleep_timeout;
|
---|
| 124 | /** Flag signalling sleep timeout in progress. */
|
---|
[da1bafb] | 125 | volatile bool timeout_pending;
|
---|
[a35b458] | 126 |
|
---|
[80bcaed] | 127 | /**
|
---|
| 128 | * True if this thread is executing copy_from_uspace().
|
---|
| 129 | * False otherwise.
|
---|
| 130 | */
|
---|
[4c60255] | 131 | bool in_copy_from_uspace;
|
---|
[a35b458] | 132 |
|
---|
[80bcaed] | 133 | /**
|
---|
| 134 | * True if this thread is executing copy_to_uspace().
|
---|
| 135 | * False otherwise.
|
---|
| 136 | */
|
---|
[4c60255] | 137 | bool in_copy_to_uspace;
|
---|
[a35b458] | 138 |
|
---|
[4c60255] | 139 | /**
|
---|
[80bcaed] | 140 | * If true, the thread will not go to sleep at all and will call
|
---|
| 141 | * thread_exit() before returning to userspace.
|
---|
[4c60255] | 142 | */
|
---|
[da1bafb] | 143 | bool interrupted;
|
---|
[a35b458] | 144 |
|
---|
[80bcaed] | 145 | /** If true, thread_join_timeout() cannot be used on this thread. */
|
---|
| 146 | bool detached;
|
---|
| 147 | /** Waitq for thread_join_timeout(). */
|
---|
| 148 | waitq_t join_wq;
|
---|
[ea7890e7] | 149 | /** Link used in the joiner_head list. */
|
---|
| 150 | link_t joiner_link;
|
---|
[a35b458] | 151 |
|
---|
[4c60255] | 152 | fpu_context_t *saved_fpu_context;
|
---|
[6eef3c4] | 153 | bool fpu_context_exists;
|
---|
[a35b458] | 154 |
|
---|
[4c60255] | 155 | /*
|
---|
| 156 | * Defined only if thread doesn't run.
|
---|
[80bcaed] | 157 | * It means that fpu context is in CPU that last time executes this
|
---|
| 158 | * thread. This disables migration.
|
---|
[4c60255] | 159 | */
|
---|
[6eef3c4] | 160 | bool fpu_context_engaged;
|
---|
[a35b458] | 161 |
|
---|
[43ac0cc] | 162 | /* The thread will not be migrated if nomigrate is non-zero. */
|
---|
[6eef3c4] | 163 | unsigned int nomigrate;
|
---|
[a35b458] | 164 |
|
---|
[6eef3c4] | 165 | /** Thread state. */
|
---|
[80bcaed] | 166 | state_t state;
|
---|
[a35b458] | 167 |
|
---|
[6eef3c4] | 168 | /** Thread CPU. */
|
---|
[80bcaed] | 169 | cpu_t *cpu;
|
---|
| 170 | /** Containing task. */
|
---|
| 171 | task_t *task;
|
---|
[6eef3c4] | 172 | /** Thread is wired to CPU. */
|
---|
| 173 | bool wired;
|
---|
| 174 | /** Thread was migrated to another CPU and has not run yet. */
|
---|
| 175 | bool stolen;
|
---|
| 176 | /** Thread is executed in user space. */
|
---|
| 177 | bool uspace;
|
---|
[a35b458] | 178 |
|
---|
[80bcaed] | 179 | /** Thread accounting. */
|
---|
[a2a00e8] | 180 | uint64_t ucycles;
|
---|
| 181 | uint64_t kcycles;
|
---|
[80bcaed] | 182 | /** Last sampled cycle. */
|
---|
| 183 | uint64_t last_cycle;
|
---|
[da1bafb] | 184 | /** Thread doesn't affect accumulated accounting. */
|
---|
[80bcaed] | 185 | bool uncounted;
|
---|
[a35b458] | 186 |
|
---|
[80bcaed] | 187 | /** Thread's priority. Implemented as index to CPU->rq */
|
---|
| 188 | int priority;
|
---|
| 189 | /** Thread ID. */
|
---|
[201abde] | 190 | thread_id_t tid;
|
---|
[8a64e81e] | 191 |
|
---|
[80bcaed] | 192 | /** Architecture-specific data. */
|
---|
| 193 | thread_arch_t arch;
|
---|
[a35b458] | 194 |
|
---|
[80bcaed] | 195 | /** Thread's kernel stack. */
|
---|
| 196 | uint8_t *kstack;
|
---|
[a35b458] | 197 |
|
---|
[9a1b20c] | 198 | #ifdef CONFIG_UDEBUG
|
---|
[5b7a107] | 199 | /**
|
---|
| 200 | * If true, the scheduler will print a stack trace
|
---|
| 201 | * to the kernel console upon scheduling this thread.
|
---|
| 202 | */
|
---|
| 203 | bool btrace;
|
---|
[a35b458] | 204 |
|
---|
[9a1b20c] | 205 | /** Debugging stuff */
|
---|
| 206 | udebug_thread_t udebug;
|
---|
[da1bafb] | 207 | #endif /* CONFIG_UDEBUG */
|
---|
[4c60255] | 208 | } thread_t;
|
---|
| 209 |
|
---|
[da1bafb] | 210 | IRQ_SPINLOCK_EXTERN(threads_lock);
|
---|
[ef1eab7] | 211 | extern odict_t threads;
|
---|
[f761f1eb] | 212 |
|
---|
| 213 | extern void thread_init(void);
|
---|
[da1bafb] | 214 | extern thread_t *thread_create(void (*)(void *), void *, task_t *,
|
---|
[6eef3c4] | 215 | thread_flags_t, const char *);
|
---|
| 216 | extern void thread_wire(thread_t *, cpu_t *);
|
---|
[d52b82ad] | 217 | extern void thread_attach(thread_t *, task_t *);
|
---|
| 218 | extern void thread_ready(thread_t *);
|
---|
[874621f] | 219 | extern void thread_exit(void) __attribute__((noreturn));
|
---|
[518dd43] | 220 | extern void thread_interrupt(thread_t *);
|
---|
| 221 | extern bool thread_interrupted(thread_t *);
|
---|
[f761f1eb] | 222 |
|
---|
[3fa424a9] | 223 | #ifndef thread_create_arch
|
---|
[deacd722] | 224 | extern errno_t thread_create_arch(thread_t *, thread_flags_t);
|
---|
[3fa424a9] | 225 | #endif
|
---|
[da1bafb] | 226 |
|
---|
[32fffef0] | 227 | #ifndef thr_constructor_arch
|
---|
[d52b82ad] | 228 | extern void thr_constructor_arch(thread_t *);
|
---|
[32fffef0] | 229 | #endif
|
---|
[da1bafb] | 230 |
|
---|
[32fffef0] | 231 | #ifndef thr_destructor_arch
|
---|
[d52b82ad] | 232 | extern void thr_destructor_arch(thread_t *);
|
---|
[32fffef0] | 233 | #endif
|
---|
[3fa424a9] | 234 |
|
---|
[d52b82ad] | 235 | extern void thread_sleep(uint32_t);
|
---|
| 236 | extern void thread_usleep(uint32_t);
|
---|
[f761f1eb] | 237 |
|
---|
[80bcaed] | 238 | #define thread_join(t) \
|
---|
| 239 | thread_join_timeout((t), SYNCH_NO_TIMEOUT, SYNCH_FLAGS_NONE)
|
---|
[da1bafb] | 240 |
|
---|
[b7fd2a0] | 241 | extern errno_t thread_join_timeout(thread_t *, uint32_t, unsigned int);
|
---|
[d52b82ad] | 242 | extern void thread_detach(thread_t *);
|
---|
[fe19611] | 243 |
|
---|
[48dcc69] | 244 | extern void thread_print_list(bool);
|
---|
[da1bafb] | 245 | extern void thread_destroy(thread_t *, bool);
|
---|
[e1b6742] | 246 | extern thread_t *thread_find_by_id(thread_id_t);
|
---|
[aab5e46] | 247 | extern size_t thread_count(void);
|
---|
| 248 | extern thread_t *thread_first(void);
|
---|
| 249 | extern thread_t *thread_next(thread_t *);
|
---|
[a2a00e8] | 250 | extern void thread_update_accounting(bool);
|
---|
[d52b82ad] | 251 | extern bool thread_exists(thread_t *);
|
---|
[f761f1eb] | 252 |
|
---|
[43ac0cc] | 253 | extern void thread_migration_disable(void);
|
---|
| 254 | extern void thread_migration_enable(void);
|
---|
| 255 |
|
---|
[5b7a107] | 256 | #ifdef CONFIG_UDEBUG
|
---|
[df58e44] | 257 | extern void thread_stack_trace(thread_id_t);
|
---|
[5b7a107] | 258 | #endif
|
---|
[f761f1eb] | 259 |
|
---|
[80bcaed] | 260 | /** Fpu context slab cache. */
|
---|
[82d515e9] | 261 | extern slab_cache_t *fpu_context_cache;
|
---|
[f76fed4] | 262 |
|
---|
[80bcaed] | 263 | /* Thread syscall prototypes. */
|
---|
[5a5269d] | 264 | extern sys_errno_t sys_thread_create(uspace_ptr_uspace_arg_t, uspace_ptr_char, size_t,
|
---|
| 265 | uspace_ptr_thread_id_t);
|
---|
[b7fd2a0] | 266 | extern sys_errno_t sys_thread_exit(int);
|
---|
[5a5269d] | 267 | extern sys_errno_t sys_thread_get_id(uspace_ptr_thread_id_t);
|
---|
[b7fd2a0] | 268 | extern sys_errno_t sys_thread_usleep(uint32_t);
|
---|
| 269 | extern sys_errno_t sys_thread_udelay(uint32_t);
|
---|
[9f52563] | 270 |
|
---|
[f761f1eb] | 271 | #endif
|
---|
[b45c443] | 272 |
|
---|
[764c302] | 273 | /** @}
|
---|
[b45c443] | 274 | */
|
---|