[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 |
|
---|
[764c302] | 29 | /** @addtogroup genericproc
|
---|
[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>
|
---|
[8e3ed06] | 43 | #include <synch/rcu_types.h>
|
---|
[5dcee525] | 44 | #include <adt/avl.h>
|
---|
[f76fed4] | 45 | #include <mm/slab.h>
|
---|
[b3f8fb7] | 46 | #include <arch/cpu.h>
|
---|
| 47 | #include <mm/tlb.h>
|
---|
[c0699467] | 48 | #include <abi/proc/uarg.h>
|
---|
[9a1b20c] | 49 | #include <udebug/udebug.h>
|
---|
[9ba415e] | 50 | #include <abi/proc/thread.h>
|
---|
[c0699467] | 51 | #include <abi/sysinfo.h>
|
---|
[1066041] | 52 | #include <arch.h>
|
---|
| 53 |
|
---|
| 54 | #define THREAD THE->thread
|
---|
[f761f1eb] | 55 |
|
---|
[da1bafb] | 56 | #define THREAD_NAME_BUFLEN 20
|
---|
[f761f1eb] | 57 |
|
---|
[a000878c] | 58 | extern const char *thread_states[];
|
---|
[f761f1eb] | 59 |
|
---|
[32fffef0] | 60 | /* Thread flags */
|
---|
[6eef3c4] | 61 | typedef enum {
|
---|
| 62 | THREAD_FLAG_NONE = 0,
|
---|
| 63 | /** Thread executes in user space. */
|
---|
| 64 | THREAD_FLAG_USPACE = (1 << 0),
|
---|
| 65 | /** Thread will be attached by the caller. */
|
---|
| 66 | THREAD_FLAG_NOATTACH = (1 << 1),
|
---|
| 67 | /** Thread accounting doesn't affect accumulated task accounting. */
|
---|
| 68 | THREAD_FLAG_UNCOUNTED = (1 << 2)
|
---|
| 69 | } thread_flags_t;
|
---|
[f761f1eb] | 70 |
|
---|
[4c60255] | 71 | /** Thread structure. There is one per thread. */
|
---|
| 72 | typedef struct thread {
|
---|
[da1bafb] | 73 | link_t rq_link; /**< Run queue link. */
|
---|
| 74 | link_t wq_link; /**< Wait queue link. */
|
---|
| 75 | link_t th_link; /**< Links to threads within containing task. */
|
---|
[a35b458] | 76 |
|
---|
[5dcee525] | 77 | /** Threads linkage to the threads_tree. */
|
---|
| 78 | avltree_node_t threads_tree_node;
|
---|
[a35b458] | 79 |
|
---|
[4c60255] | 80 | /** Lock protecting thread structure.
|
---|
| 81 | *
|
---|
| 82 | * Protects the whole thread structure except list links above.
|
---|
| 83 | */
|
---|
[da1bafb] | 84 | IRQ_SPINLOCK_DECLARE(lock);
|
---|
[a35b458] | 85 |
|
---|
[4c60255] | 86 | char name[THREAD_NAME_BUFLEN];
|
---|
[a35b458] | 87 |
|
---|
[80bcaed] | 88 | /** Function implementing the thread. */
|
---|
[df58e44] | 89 | void (*thread_code)(void *);
|
---|
[80bcaed] | 90 | /** Argument passed to thread_code() function. */
|
---|
| 91 | void *thread_arg;
|
---|
[a35b458] | 92 |
|
---|
[80bcaed] | 93 | /**
|
---|
[df58e44] | 94 | * From here, the stored context is restored
|
---|
| 95 | * when the thread is scheduled.
|
---|
[80bcaed] | 96 | */
|
---|
[4c60255] | 97 | context_t saved_context;
|
---|
[a35b458] | 98 |
|
---|
[80bcaed] | 99 | /**
|
---|
[df58e44] | 100 | * From here, the stored timeout context
|
---|
| 101 | * is restored when sleep times out.
|
---|
[80bcaed] | 102 | */
|
---|
[4c60255] | 103 | context_t sleep_timeout_context;
|
---|
[a35b458] | 104 |
|
---|
[80bcaed] | 105 | /**
|
---|
[df58e44] | 106 | * From here, the stored interruption context
|
---|
| 107 | * is restored when sleep is interrupted.
|
---|
[80bcaed] | 108 | */
|
---|
[4c60255] | 109 | context_t sleep_interruption_context;
|
---|
[a35b458] | 110 |
|
---|
[80bcaed] | 111 | /** If true, the thread can be interrupted from sleep. */
|
---|
| 112 | bool sleep_interruptible;
|
---|
[b59318e] | 113 |
|
---|
| 114 | /**
|
---|
| 115 | * If true, and this thread's sleep returns without a wakeup
|
---|
| 116 | * (timed out or interrupted), waitq ignores the next wakeup.
|
---|
| 117 | * This is necessary for futex to be able to handle those conditions.
|
---|
| 118 | */
|
---|
| 119 | bool sleep_composable;
|
---|
| 120 |
|
---|
[80bcaed] | 121 | /** Wait queue in which this thread sleeps. */
|
---|
| 122 | waitq_t *sleep_queue;
|
---|
| 123 | /** Timeout used for timeoutable sleeping. */
|
---|
| 124 | timeout_t sleep_timeout;
|
---|
| 125 | /** Flag signalling sleep timeout in progress. */
|
---|
[da1bafb] | 126 | volatile bool timeout_pending;
|
---|
[a35b458] | 127 |
|
---|
[80bcaed] | 128 | /**
|
---|
| 129 | * True if this thread is executing copy_from_uspace().
|
---|
| 130 | * False otherwise.
|
---|
| 131 | */
|
---|
[4c60255] | 132 | bool in_copy_from_uspace;
|
---|
[a35b458] | 133 |
|
---|
[80bcaed] | 134 | /**
|
---|
| 135 | * True if this thread is executing copy_to_uspace().
|
---|
| 136 | * False otherwise.
|
---|
| 137 | */
|
---|
[4c60255] | 138 | bool in_copy_to_uspace;
|
---|
[a35b458] | 139 |
|
---|
[4c60255] | 140 | /**
|
---|
[80bcaed] | 141 | * If true, the thread will not go to sleep at all and will call
|
---|
| 142 | * thread_exit() before returning to userspace.
|
---|
[4c60255] | 143 | */
|
---|
[da1bafb] | 144 | bool interrupted;
|
---|
[a35b458] | 145 |
|
---|
[80bcaed] | 146 | /** If true, thread_join_timeout() cannot be used on this thread. */
|
---|
| 147 | bool detached;
|
---|
| 148 | /** Waitq for thread_join_timeout(). */
|
---|
| 149 | waitq_t join_wq;
|
---|
[ea7890e7] | 150 | /** Link used in the joiner_head list. */
|
---|
| 151 | link_t joiner_link;
|
---|
[a35b458] | 152 |
|
---|
[4c60255] | 153 | fpu_context_t *saved_fpu_context;
|
---|
[6eef3c4] | 154 | bool fpu_context_exists;
|
---|
[a35b458] | 155 |
|
---|
[4c60255] | 156 | /*
|
---|
| 157 | * Defined only if thread doesn't run.
|
---|
[80bcaed] | 158 | * It means that fpu context is in CPU that last time executes this
|
---|
| 159 | * thread. This disables migration.
|
---|
[4c60255] | 160 | */
|
---|
[6eef3c4] | 161 | bool fpu_context_engaged;
|
---|
[a35b458] | 162 |
|
---|
[43ac0cc] | 163 | /* The thread will not be migrated if nomigrate is non-zero. */
|
---|
[6eef3c4] | 164 | unsigned int nomigrate;
|
---|
[a35b458] | 165 |
|
---|
[6eef3c4] | 166 | /** Thread state. */
|
---|
[80bcaed] | 167 | state_t state;
|
---|
[a35b458] | 168 |
|
---|
[6eef3c4] | 169 | /** Thread CPU. */
|
---|
[80bcaed] | 170 | cpu_t *cpu;
|
---|
| 171 | /** Containing task. */
|
---|
| 172 | task_t *task;
|
---|
[6eef3c4] | 173 | /** Thread is wired to CPU. */
|
---|
| 174 | bool wired;
|
---|
| 175 | /** Thread was migrated to another CPU and has not run yet. */
|
---|
| 176 | bool stolen;
|
---|
| 177 | /** Thread is executed in user space. */
|
---|
| 178 | bool uspace;
|
---|
[a35b458] | 179 |
|
---|
[80bcaed] | 180 | /** Ticks before preemption. */
|
---|
| 181 | uint64_t ticks;
|
---|
[a35b458] | 182 |
|
---|
[80bcaed] | 183 | /** Thread accounting. */
|
---|
[a2a00e8] | 184 | uint64_t ucycles;
|
---|
| 185 | uint64_t kcycles;
|
---|
[80bcaed] | 186 | /** Last sampled cycle. */
|
---|
| 187 | uint64_t last_cycle;
|
---|
[da1bafb] | 188 | /** Thread doesn't affect accumulated accounting. */
|
---|
[80bcaed] | 189 | bool uncounted;
|
---|
[a35b458] | 190 |
|
---|
[80bcaed] | 191 | /** Thread's priority. Implemented as index to CPU->rq */
|
---|
| 192 | int priority;
|
---|
| 193 | /** Thread ID. */
|
---|
[201abde] | 194 | thread_id_t tid;
|
---|
[8a64e81e] | 195 |
|
---|
| 196 | /** Work queue this thread belongs to or NULL. Immutable. */
|
---|
| 197 | struct work_queue *workq;
|
---|
| 198 | /** Links work queue threads. Protected by workq->lock. */
|
---|
[1b20da0] | 199 | link_t workq_link;
|
---|
[181a746] | 200 | /** True if the worker was blocked and is not running. Use thread->lock. */
|
---|
[8a64e81e] | 201 | bool workq_blocked;
|
---|
[181a746] | 202 | /** True if the worker will block in order to become idle. Use workq->lock. */
|
---|
[8a64e81e] | 203 | bool workq_idling;
|
---|
[a35b458] | 204 |
|
---|
[181a746] | 205 | /** RCU thread related data. Protected by its own locks. */
|
---|
| 206 | rcu_thread_data_t rcu;
|
---|
[a35b458] | 207 |
|
---|
[80bcaed] | 208 | /** Architecture-specific data. */
|
---|
| 209 | thread_arch_t arch;
|
---|
[a35b458] | 210 |
|
---|
[80bcaed] | 211 | /** Thread's kernel stack. */
|
---|
| 212 | uint8_t *kstack;
|
---|
[a35b458] | 213 |
|
---|
[9a1b20c] | 214 | #ifdef CONFIG_UDEBUG
|
---|
[5b7a107] | 215 | /**
|
---|
| 216 | * If true, the scheduler will print a stack trace
|
---|
| 217 | * to the kernel console upon scheduling this thread.
|
---|
| 218 | */
|
---|
| 219 | bool btrace;
|
---|
[a35b458] | 220 |
|
---|
[9a1b20c] | 221 | /** Debugging stuff */
|
---|
| 222 | udebug_thread_t udebug;
|
---|
[da1bafb] | 223 | #endif /* CONFIG_UDEBUG */
|
---|
[4c60255] | 224 | } thread_t;
|
---|
| 225 |
|
---|
[05e2a7ad] | 226 | /** Thread list lock.
|
---|
| 227 | *
|
---|
[5dcee525] | 228 | * This lock protects the threads_tree.
|
---|
[05e2a7ad] | 229 | * Must be acquired before T.lock for each T of type thread_t.
|
---|
| 230 | *
|
---|
| 231 | */
|
---|
[da1bafb] | 232 | IRQ_SPINLOCK_EXTERN(threads_lock);
|
---|
[05e2a7ad] | 233 |
|
---|
[5dcee525] | 234 | /** AVL tree containing all threads. */
|
---|
| 235 | extern avltree_t threads_tree;
|
---|
[f761f1eb] | 236 |
|
---|
| 237 | extern void thread_init(void);
|
---|
[da1bafb] | 238 | extern thread_t *thread_create(void (*)(void *), void *, task_t *,
|
---|
[6eef3c4] | 239 | thread_flags_t, const char *);
|
---|
| 240 | extern void thread_wire(thread_t *, cpu_t *);
|
---|
[d52b82ad] | 241 | extern void thread_attach(thread_t *, task_t *);
|
---|
| 242 | extern void thread_ready(thread_t *);
|
---|
[874621f] | 243 | extern void thread_exit(void) __attribute__((noreturn));
|
---|
[518dd43] | 244 | extern void thread_interrupt(thread_t *);
|
---|
| 245 | extern bool thread_interrupted(thread_t *);
|
---|
[f761f1eb] | 246 |
|
---|
[3fa424a9] | 247 | #ifndef thread_create_arch
|
---|
[d52b82ad] | 248 | extern void thread_create_arch(thread_t *);
|
---|
[3fa424a9] | 249 | #endif
|
---|
[da1bafb] | 250 |
|
---|
[32fffef0] | 251 | #ifndef thr_constructor_arch
|
---|
[d52b82ad] | 252 | extern void thr_constructor_arch(thread_t *);
|
---|
[32fffef0] | 253 | #endif
|
---|
[da1bafb] | 254 |
|
---|
[32fffef0] | 255 | #ifndef thr_destructor_arch
|
---|
[d52b82ad] | 256 | extern void thr_destructor_arch(thread_t *);
|
---|
[32fffef0] | 257 | #endif
|
---|
[3fa424a9] | 258 |
|
---|
[d52b82ad] | 259 | extern void thread_sleep(uint32_t);
|
---|
| 260 | extern void thread_usleep(uint32_t);
|
---|
[f761f1eb] | 261 |
|
---|
[80bcaed] | 262 | #define thread_join(t) \
|
---|
| 263 | thread_join_timeout((t), SYNCH_NO_TIMEOUT, SYNCH_FLAGS_NONE)
|
---|
[da1bafb] | 264 |
|
---|
[b7fd2a0] | 265 | extern errno_t thread_join_timeout(thread_t *, uint32_t, unsigned int);
|
---|
[d52b82ad] | 266 | extern void thread_detach(thread_t *);
|
---|
[fe19611] | 267 |
|
---|
[48dcc69] | 268 | extern void thread_print_list(bool);
|
---|
[da1bafb] | 269 | extern void thread_destroy(thread_t *, bool);
|
---|
[e1b6742] | 270 | extern thread_t *thread_find_by_id(thread_id_t);
|
---|
[a2a00e8] | 271 | extern void thread_update_accounting(bool);
|
---|
[d52b82ad] | 272 | extern bool thread_exists(thread_t *);
|
---|
[f761f1eb] | 273 |
|
---|
[43ac0cc] | 274 | extern void thread_migration_disable(void);
|
---|
| 275 | extern void thread_migration_enable(void);
|
---|
| 276 |
|
---|
[5b7a107] | 277 | #ifdef CONFIG_UDEBUG
|
---|
[df58e44] | 278 | extern void thread_stack_trace(thread_id_t);
|
---|
[5b7a107] | 279 | #endif
|
---|
[f761f1eb] | 280 |
|
---|
[80bcaed] | 281 | /** Fpu context slab cache. */
|
---|
[82d515e9] | 282 | extern slab_cache_t *fpu_context_cache;
|
---|
[f76fed4] | 283 |
|
---|
[80bcaed] | 284 | /* Thread syscall prototypes. */
|
---|
[b7fd2a0] | 285 | extern sys_errno_t sys_thread_create(uspace_arg_t *, char *, size_t,
|
---|
[d52b82ad] | 286 | thread_id_t *);
|
---|
[b7fd2a0] | 287 | extern sys_errno_t sys_thread_exit(int);
|
---|
| 288 | extern sys_errno_t sys_thread_get_id(thread_id_t *);
|
---|
| 289 | extern sys_errno_t sys_thread_usleep(uint32_t);
|
---|
| 290 | extern sys_errno_t sys_thread_udelay(uint32_t);
|
---|
[9f52563] | 291 |
|
---|
[f761f1eb] | 292 | #endif
|
---|
[b45c443] | 293 |
|
---|
[764c302] | 294 | /** @}
|
---|
[b45c443] | 295 | */
|
---|