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