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