[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>
|
---|
[c0699467] | 50 | #include <abi/sysinfo.h>
|
---|
[1066041] | 51 | #include <arch.h>
|
---|
| 52 |
|
---|
| 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. */
|
---|
| 76 |
|
---|
[5dcee525] | 77 | /** Threads linkage to the threads_tree. */
|
---|
| 78 | avltree_node_t threads_tree_node;
|
---|
[4c60255] | 79 |
|
---|
| 80 | /** Lock protecting thread structure.
|
---|
| 81 | *
|
---|
| 82 | * Protects the whole thread structure except list links above.
|
---|
| 83 | */
|
---|
[da1bafb] | 84 | IRQ_SPINLOCK_DECLARE(lock);
|
---|
| 85 |
|
---|
[4c60255] | 86 | char name[THREAD_NAME_BUFLEN];
|
---|
[da1bafb] | 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;
|
---|
[da1bafb] | 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;
|
---|
[df58e44] | 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;
|
---|
[df58e44] | 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;
|
---|
[da1bafb] | 110 |
|
---|
[80bcaed] | 111 | /** If true, the thread can be interrupted from sleep. */
|
---|
| 112 | bool sleep_interruptible;
|
---|
| 113 | /** Wait queue in which this thread sleeps. */
|
---|
| 114 | waitq_t *sleep_queue;
|
---|
| 115 | /** Timeout used for timeoutable sleeping. */
|
---|
| 116 | timeout_t sleep_timeout;
|
---|
| 117 | /** Flag signalling sleep timeout in progress. */
|
---|
[da1bafb] | 118 | volatile bool timeout_pending;
|
---|
| 119 |
|
---|
[80bcaed] | 120 | /**
|
---|
| 121 | * True if this thread is executing copy_from_uspace().
|
---|
| 122 | * False otherwise.
|
---|
| 123 | */
|
---|
[4c60255] | 124 | bool in_copy_from_uspace;
|
---|
[df58e44] | 125 |
|
---|
[80bcaed] | 126 | /**
|
---|
| 127 | * True if this thread is executing copy_to_uspace().
|
---|
| 128 | * False otherwise.
|
---|
| 129 | */
|
---|
[4c60255] | 130 | bool in_copy_to_uspace;
|
---|
| 131 |
|
---|
| 132 | /**
|
---|
[80bcaed] | 133 | * If true, the thread will not go to sleep at all and will call
|
---|
| 134 | * thread_exit() before returning to userspace.
|
---|
[4c60255] | 135 | */
|
---|
[da1bafb] | 136 | bool interrupted;
|
---|
[4c60255] | 137 |
|
---|
[80bcaed] | 138 | /** If true, thread_join_timeout() cannot be used on this thread. */
|
---|
| 139 | bool detached;
|
---|
| 140 | /** Waitq for thread_join_timeout(). */
|
---|
| 141 | waitq_t join_wq;
|
---|
[ea7890e7] | 142 | /** Link used in the joiner_head list. */
|
---|
| 143 | link_t joiner_link;
|
---|
[da1bafb] | 144 |
|
---|
[4c60255] | 145 | fpu_context_t *saved_fpu_context;
|
---|
[6eef3c4] | 146 | bool fpu_context_exists;
|
---|
[da1bafb] | 147 |
|
---|
[4c60255] | 148 | /*
|
---|
| 149 | * Defined only if thread doesn't run.
|
---|
[80bcaed] | 150 | * It means that fpu context is in CPU that last time executes this
|
---|
| 151 | * thread. This disables migration.
|
---|
[4c60255] | 152 | */
|
---|
[6eef3c4] | 153 | bool fpu_context_engaged;
|
---|
[da1bafb] | 154 |
|
---|
[43ac0cc] | 155 | /* The thread will not be migrated if nomigrate is non-zero. */
|
---|
[6eef3c4] | 156 | unsigned int nomigrate;
|
---|
[43ac0cc] | 157 |
|
---|
[6eef3c4] | 158 | /** Thread state. */
|
---|
[80bcaed] | 159 | state_t state;
|
---|
[4c60255] | 160 |
|
---|
[6eef3c4] | 161 | /** Thread CPU. */
|
---|
[80bcaed] | 162 | cpu_t *cpu;
|
---|
| 163 | /** Containing task. */
|
---|
| 164 | task_t *task;
|
---|
[6eef3c4] | 165 | /** Thread is wired to CPU. */
|
---|
| 166 | bool wired;
|
---|
| 167 | /** Thread was migrated to another CPU and has not run yet. */
|
---|
| 168 | bool stolen;
|
---|
| 169 | /** Thread is executed in user space. */
|
---|
| 170 | bool uspace;
|
---|
[da1bafb] | 171 |
|
---|
[80bcaed] | 172 | /** Ticks before preemption. */
|
---|
| 173 | uint64_t ticks;
|
---|
[4c60255] | 174 |
|
---|
[80bcaed] | 175 | /** Thread accounting. */
|
---|
[a2a00e8] | 176 | uint64_t ucycles;
|
---|
| 177 | uint64_t kcycles;
|
---|
[80bcaed] | 178 | /** Last sampled cycle. */
|
---|
| 179 | uint64_t last_cycle;
|
---|
[da1bafb] | 180 | /** Thread doesn't affect accumulated accounting. */
|
---|
[80bcaed] | 181 | bool uncounted;
|
---|
[da1bafb] | 182 |
|
---|
[80bcaed] | 183 | /** Thread's priority. Implemented as index to CPU->rq */
|
---|
| 184 | int priority;
|
---|
| 185 | /** Thread ID. */
|
---|
[201abde] | 186 | thread_id_t tid;
|
---|
[8a64e81e] | 187 |
|
---|
| 188 | /** Work queue this thread belongs to or NULL. Immutable. */
|
---|
| 189 | struct work_queue *workq;
|
---|
| 190 | /** Links work queue threads. Protected by workq->lock. */
|
---|
| 191 | link_t workq_link;
|
---|
[181a746] | 192 | /** True if the worker was blocked and is not running. Use thread->lock. */
|
---|
[8a64e81e] | 193 | bool workq_blocked;
|
---|
[181a746] | 194 | /** True if the worker will block in order to become idle. Use workq->lock. */
|
---|
[8a64e81e] | 195 | bool workq_idling;
|
---|
[4c60255] | 196 |
|
---|
[181a746] | 197 | /** RCU thread related data. Protected by its own locks. */
|
---|
| 198 | rcu_thread_data_t rcu;
|
---|
| 199 |
|
---|
[80bcaed] | 200 | /** Architecture-specific data. */
|
---|
| 201 | thread_arch_t arch;
|
---|
[da1bafb] | 202 |
|
---|
[80bcaed] | 203 | /** Thread's kernel stack. */
|
---|
| 204 | uint8_t *kstack;
|
---|
[da1bafb] | 205 |
|
---|
[9a1b20c] | 206 | #ifdef CONFIG_UDEBUG
|
---|
[5b7a107] | 207 | /**
|
---|
| 208 | * If true, the scheduler will print a stack trace
|
---|
| 209 | * to the kernel console upon scheduling this thread.
|
---|
| 210 | */
|
---|
| 211 | bool btrace;
|
---|
| 212 |
|
---|
[9a1b20c] | 213 | /** Debugging stuff */
|
---|
| 214 | udebug_thread_t udebug;
|
---|
[da1bafb] | 215 | #endif /* CONFIG_UDEBUG */
|
---|
[4c60255] | 216 | } thread_t;
|
---|
| 217 |
|
---|
[05e2a7ad] | 218 | /** Thread list lock.
|
---|
| 219 | *
|
---|
[5dcee525] | 220 | * This lock protects the threads_tree.
|
---|
[05e2a7ad] | 221 | * Must be acquired before T.lock for each T of type thread_t.
|
---|
| 222 | *
|
---|
| 223 | */
|
---|
[da1bafb] | 224 | IRQ_SPINLOCK_EXTERN(threads_lock);
|
---|
[05e2a7ad] | 225 |
|
---|
[5dcee525] | 226 | /** AVL tree containing all threads. */
|
---|
| 227 | extern avltree_t threads_tree;
|
---|
[f761f1eb] | 228 |
|
---|
| 229 | extern void thread_init(void);
|
---|
[da1bafb] | 230 | extern thread_t *thread_create(void (*)(void *), void *, task_t *,
|
---|
[6eef3c4] | 231 | thread_flags_t, const char *);
|
---|
| 232 | extern void thread_wire(thread_t *, cpu_t *);
|
---|
[d52b82ad] | 233 | extern void thread_attach(thread_t *, task_t *);
|
---|
| 234 | extern void thread_ready(thread_t *);
|
---|
[874621f] | 235 | extern void thread_exit(void) __attribute__((noreturn));
|
---|
[518dd43] | 236 | extern void thread_interrupt(thread_t *);
|
---|
| 237 | extern bool thread_interrupted(thread_t *);
|
---|
[f761f1eb] | 238 |
|
---|
[3fa424a9] | 239 | #ifndef thread_create_arch
|
---|
[d52b82ad] | 240 | extern void thread_create_arch(thread_t *);
|
---|
[3fa424a9] | 241 | #endif
|
---|
[da1bafb] | 242 |
|
---|
[32fffef0] | 243 | #ifndef thr_constructor_arch
|
---|
[d52b82ad] | 244 | extern void thr_constructor_arch(thread_t *);
|
---|
[32fffef0] | 245 | #endif
|
---|
[da1bafb] | 246 |
|
---|
[32fffef0] | 247 | #ifndef thr_destructor_arch
|
---|
[d52b82ad] | 248 | extern void thr_destructor_arch(thread_t *);
|
---|
[32fffef0] | 249 | #endif
|
---|
[3fa424a9] | 250 |
|
---|
[d52b82ad] | 251 | extern void thread_sleep(uint32_t);
|
---|
| 252 | extern void thread_usleep(uint32_t);
|
---|
[f761f1eb] | 253 |
|
---|
[80bcaed] | 254 | #define thread_join(t) \
|
---|
| 255 | thread_join_timeout((t), SYNCH_NO_TIMEOUT, SYNCH_FLAGS_NONE)
|
---|
[da1bafb] | 256 |
|
---|
| 257 | extern int thread_join_timeout(thread_t *, uint32_t, unsigned int);
|
---|
[d52b82ad] | 258 | extern void thread_detach(thread_t *);
|
---|
[fe19611] | 259 |
|
---|
[48dcc69] | 260 | extern void thread_print_list(bool);
|
---|
[da1bafb] | 261 | extern void thread_destroy(thread_t *, bool);
|
---|
[e1b6742] | 262 | extern thread_t *thread_find_by_id(thread_id_t);
|
---|
[a2a00e8] | 263 | extern void thread_update_accounting(bool);
|
---|
[d52b82ad] | 264 | extern bool thread_exists(thread_t *);
|
---|
[f761f1eb] | 265 |
|
---|
[43ac0cc] | 266 | extern void thread_migration_disable(void);
|
---|
| 267 | extern void thread_migration_enable(void);
|
---|
| 268 |
|
---|
[5b7a107] | 269 | #ifdef CONFIG_UDEBUG
|
---|
[df58e44] | 270 | extern void thread_stack_trace(thread_id_t);
|
---|
[5b7a107] | 271 | #endif
|
---|
[f761f1eb] | 272 |
|
---|
[80bcaed] | 273 | /** Fpu context slab cache. */
|
---|
[f76fed4] | 274 | extern slab_cache_t *fpu_context_slab;
|
---|
| 275 |
|
---|
[80bcaed] | 276 | /* Thread syscall prototypes. */
|
---|
[96b02eb9] | 277 | extern sysarg_t sys_thread_create(uspace_arg_t *, char *, size_t,
|
---|
[d52b82ad] | 278 | thread_id_t *);
|
---|
[96b02eb9] | 279 | extern sysarg_t sys_thread_exit(int);
|
---|
| 280 | extern sysarg_t sys_thread_get_id(thread_id_t *);
|
---|
| 281 | extern sysarg_t sys_thread_usleep(uint32_t);
|
---|
[7e7b791] | 282 | extern sysarg_t sys_thread_udelay(uint32_t);
|
---|
[9f52563] | 283 |
|
---|
[f761f1eb] | 284 | #endif
|
---|
[b45c443] | 285 |
|
---|
[764c302] | 286 | /** @}
|
---|
[b45c443] | 287 | */
|
---|