| 1 | /*
|
|---|
| 2 | * Copyright (c) 2001-2007 Jakub Jermar
|
|---|
| 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 |
|
|---|
| 29 | /** @addtogroup genericproc
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file
|
|---|
| 33 | */
|
|---|
| 34 |
|
|---|
| 35 | #ifndef KERN_THREAD_H_
|
|---|
| 36 | #define KERN_THREAD_H_
|
|---|
| 37 |
|
|---|
| 38 | #include <synch/waitq.h>
|
|---|
| 39 | #include <proc/task.h>
|
|---|
| 40 | #include <time/timeout.h>
|
|---|
| 41 | #include <cpu.h>
|
|---|
| 42 | #include <synch/spinlock.h>
|
|---|
| 43 | #include <synch/rcu_types.h>
|
|---|
| 44 | #include <adt/avl.h>
|
|---|
| 45 | #include <mm/slab.h>
|
|---|
| 46 | #include <arch/cpu.h>
|
|---|
| 47 | #include <mm/tlb.h>
|
|---|
| 48 | #include <abi/proc/uarg.h>
|
|---|
| 49 | #include <udebug/udebug.h>
|
|---|
| 50 | #include <abi/sysinfo.h>
|
|---|
| 51 | #include <arch.h>
|
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 | #define THREAD THE->thread
|
|---|
| 55 |
|
|---|
| 56 | #define THREAD_NAME_BUFLEN 20
|
|---|
| 57 |
|
|---|
| 58 | extern const char *thread_states[];
|
|---|
| 59 |
|
|---|
| 60 | /* Thread flags */
|
|---|
| 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;
|
|---|
| 70 |
|
|---|
| 71 | /** Thread structure. There is one per thread. */
|
|---|
| 72 | typedef struct thread {
|
|---|
| 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 |
|
|---|
| 77 | /** Threads linkage to the threads_tree. */
|
|---|
| 78 | avltree_node_t threads_tree_node;
|
|---|
| 79 |
|
|---|
| 80 | /** Lock protecting thread structure.
|
|---|
| 81 | *
|
|---|
| 82 | * Protects the whole thread structure except list links above.
|
|---|
| 83 | */
|
|---|
| 84 | IRQ_SPINLOCK_DECLARE(lock);
|
|---|
| 85 |
|
|---|
| 86 | char name[THREAD_NAME_BUFLEN];
|
|---|
| 87 |
|
|---|
| 88 | /** Function implementing the thread. */
|
|---|
| 89 | void (*thread_code)(void *);
|
|---|
| 90 | /** Argument passed to thread_code() function. */
|
|---|
| 91 | void *thread_arg;
|
|---|
| 92 |
|
|---|
| 93 | /**
|
|---|
| 94 | * From here, the stored context is restored
|
|---|
| 95 | * when the thread is scheduled.
|
|---|
| 96 | */
|
|---|
| 97 | context_t saved_context;
|
|---|
| 98 |
|
|---|
| 99 | /**
|
|---|
| 100 | * From here, the stored timeout context
|
|---|
| 101 | * is restored when sleep times out.
|
|---|
| 102 | */
|
|---|
| 103 | context_t sleep_timeout_context;
|
|---|
| 104 |
|
|---|
| 105 | /**
|
|---|
| 106 | * From here, the stored interruption context
|
|---|
| 107 | * is restored when sleep is interrupted.
|
|---|
| 108 | */
|
|---|
| 109 | context_t sleep_interruption_context;
|
|---|
| 110 |
|
|---|
| 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. */
|
|---|
| 118 | volatile bool timeout_pending;
|
|---|
| 119 |
|
|---|
| 120 | /**
|
|---|
| 121 | * True if this thread is executing copy_from_uspace().
|
|---|
| 122 | * False otherwise.
|
|---|
| 123 | */
|
|---|
| 124 | bool in_copy_from_uspace;
|
|---|
| 125 |
|
|---|
| 126 | /**
|
|---|
| 127 | * True if this thread is executing copy_to_uspace().
|
|---|
| 128 | * False otherwise.
|
|---|
| 129 | */
|
|---|
| 130 | bool in_copy_to_uspace;
|
|---|
| 131 |
|
|---|
| 132 | /**
|
|---|
| 133 | * If true, the thread will not go to sleep at all and will call
|
|---|
| 134 | * thread_exit() before returning to userspace.
|
|---|
| 135 | */
|
|---|
| 136 | bool interrupted;
|
|---|
| 137 |
|
|---|
| 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;
|
|---|
| 142 | /** Link used in the joiner_head list. */
|
|---|
| 143 | link_t joiner_link;
|
|---|
| 144 |
|
|---|
| 145 | fpu_context_t *saved_fpu_context;
|
|---|
| 146 | bool fpu_context_exists;
|
|---|
| 147 |
|
|---|
| 148 | /*
|
|---|
| 149 | * Defined only if thread doesn't run.
|
|---|
| 150 | * It means that fpu context is in CPU that last time executes this
|
|---|
| 151 | * thread. This disables migration.
|
|---|
| 152 | */
|
|---|
| 153 | bool fpu_context_engaged;
|
|---|
| 154 |
|
|---|
| 155 | /* The thread will not be migrated if nomigrate is non-zero. */
|
|---|
| 156 | unsigned int nomigrate;
|
|---|
| 157 |
|
|---|
| 158 | /** Thread state. */
|
|---|
| 159 | state_t state;
|
|---|
| 160 |
|
|---|
| 161 | /** Thread CPU. */
|
|---|
| 162 | cpu_t *cpu;
|
|---|
| 163 | /** Containing task. */
|
|---|
| 164 | task_t *task;
|
|---|
| 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;
|
|---|
| 171 |
|
|---|
| 172 | /** Ticks before preemption. */
|
|---|
| 173 | uint64_t ticks;
|
|---|
| 174 |
|
|---|
| 175 | /** Thread accounting. */
|
|---|
| 176 | uint64_t ucycles;
|
|---|
| 177 | uint64_t kcycles;
|
|---|
| 178 | /** Last sampled cycle. */
|
|---|
| 179 | uint64_t last_cycle;
|
|---|
| 180 | /** Thread doesn't affect accumulated accounting. */
|
|---|
| 181 | bool uncounted;
|
|---|
| 182 |
|
|---|
| 183 | /** Thread's priority. Implemented as index to CPU->rq */
|
|---|
| 184 | int priority;
|
|---|
| 185 | /** Thread ID. */
|
|---|
| 186 | thread_id_t tid;
|
|---|
| 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;
|
|---|
| 192 | /** True if the worker was blocked and is not running. Use thread->lock. */
|
|---|
| 193 | bool workq_blocked;
|
|---|
| 194 | /** True if the worker will block in order to become idle. Use workq->lock. */
|
|---|
| 195 | bool workq_idling;
|
|---|
| 196 |
|
|---|
| 197 | /** RCU thread related data. Protected by its own locks. */
|
|---|
| 198 | rcu_thread_data_t rcu;
|
|---|
| 199 |
|
|---|
| 200 | /** Architecture-specific data. */
|
|---|
| 201 | thread_arch_t arch;
|
|---|
| 202 |
|
|---|
| 203 | /** Thread's kernel stack. */
|
|---|
| 204 | uint8_t *kstack;
|
|---|
| 205 |
|
|---|
| 206 | #ifdef CONFIG_UDEBUG
|
|---|
| 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 |
|
|---|
| 213 | /** Debugging stuff */
|
|---|
| 214 | udebug_thread_t udebug;
|
|---|
| 215 | #endif /* CONFIG_UDEBUG */
|
|---|
| 216 | } thread_t;
|
|---|
| 217 |
|
|---|
| 218 | /** Thread list lock.
|
|---|
| 219 | *
|
|---|
| 220 | * This lock protects the threads_tree.
|
|---|
| 221 | * Must be acquired before T.lock for each T of type thread_t.
|
|---|
| 222 | *
|
|---|
| 223 | */
|
|---|
| 224 | IRQ_SPINLOCK_EXTERN(threads_lock);
|
|---|
| 225 |
|
|---|
| 226 | /** AVL tree containing all threads. */
|
|---|
| 227 | extern avltree_t threads_tree;
|
|---|
| 228 |
|
|---|
| 229 | extern void thread_init(void);
|
|---|
| 230 | extern thread_t *thread_create(void (*)(void *), void *, task_t *,
|
|---|
| 231 | thread_flags_t, const char *);
|
|---|
| 232 | extern void thread_wire(thread_t *, cpu_t *);
|
|---|
| 233 | extern void thread_attach(thread_t *, task_t *);
|
|---|
| 234 | extern void thread_ready(thread_t *);
|
|---|
| 235 | extern void thread_exit(void) __attribute__((noreturn));
|
|---|
| 236 | extern void thread_interrupt(thread_t *);
|
|---|
| 237 | extern bool thread_interrupted(thread_t *);
|
|---|
| 238 |
|
|---|
| 239 | #ifndef thread_create_arch
|
|---|
| 240 | extern void thread_create_arch(thread_t *);
|
|---|
| 241 | #endif
|
|---|
| 242 |
|
|---|
| 243 | #ifndef thr_constructor_arch
|
|---|
| 244 | extern void thr_constructor_arch(thread_t *);
|
|---|
| 245 | #endif
|
|---|
| 246 |
|
|---|
| 247 | #ifndef thr_destructor_arch
|
|---|
| 248 | extern void thr_destructor_arch(thread_t *);
|
|---|
| 249 | #endif
|
|---|
| 250 |
|
|---|
| 251 | extern void thread_sleep(uint32_t);
|
|---|
| 252 | extern void thread_usleep(uint32_t);
|
|---|
| 253 |
|
|---|
| 254 | #define thread_join(t) \
|
|---|
| 255 | thread_join_timeout((t), SYNCH_NO_TIMEOUT, SYNCH_FLAGS_NONE)
|
|---|
| 256 |
|
|---|
| 257 | extern int thread_join_timeout(thread_t *, uint32_t, unsigned int);
|
|---|
| 258 | extern void thread_detach(thread_t *);
|
|---|
| 259 |
|
|---|
| 260 | extern void thread_print_list(bool);
|
|---|
| 261 | extern void thread_destroy(thread_t *, bool);
|
|---|
| 262 | extern thread_t *thread_find_by_id(thread_id_t);
|
|---|
| 263 | extern void thread_update_accounting(bool);
|
|---|
| 264 | extern bool thread_exists(thread_t *);
|
|---|
| 265 |
|
|---|
| 266 | extern void thread_migration_disable(void);
|
|---|
| 267 | extern void thread_migration_enable(void);
|
|---|
| 268 |
|
|---|
| 269 | #ifdef CONFIG_UDEBUG
|
|---|
| 270 | extern void thread_stack_trace(thread_id_t);
|
|---|
| 271 | #endif
|
|---|
| 272 |
|
|---|
| 273 | /** Fpu context slab cache. */
|
|---|
| 274 | extern slab_cache_t *fpu_context_slab;
|
|---|
| 275 |
|
|---|
| 276 | /* Thread syscall prototypes. */
|
|---|
| 277 | extern sysarg_t sys_thread_create(uspace_arg_t *, char *, size_t,
|
|---|
| 278 | thread_id_t *);
|
|---|
| 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);
|
|---|
| 282 | extern sysarg_t sys_thread_udelay(uint32_t);
|
|---|
| 283 |
|
|---|
| 284 | #endif
|
|---|
| 285 |
|
|---|
| 286 | /** @}
|
|---|
| 287 | */
|
|---|