[f761f1eb] | 1 | /*
|
---|
[df4ed85] | 2 | * Copyright (c) 2001-2004 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 |
|
---|
[cc73a8a1] | 29 | /** @addtogroup genericproc
|
---|
[b45c443] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 |
|
---|
[9179d0a] | 33 | /**
|
---|
[b45c443] | 34 | * @file
|
---|
[9179d0a] | 35 | * @brief Thread management functions.
|
---|
| 36 | */
|
---|
| 37 |
|
---|
[f761f1eb] | 38 | #include <proc/scheduler.h>
|
---|
| 39 | #include <proc/thread.h>
|
---|
| 40 | #include <proc/task.h>
|
---|
[0f250f9] | 41 | #include <proc/uarg.h>
|
---|
[f761f1eb] | 42 | #include <mm/frame.h>
|
---|
| 43 | #include <mm/page.h>
|
---|
| 44 | #include <arch/asm.h>
|
---|
[cce6acf] | 45 | #include <arch/cycle.h>
|
---|
[f761f1eb] | 46 | #include <arch.h>
|
---|
| 47 | #include <synch/synch.h>
|
---|
| 48 | #include <synch/spinlock.h>
|
---|
| 49 | #include <synch/waitq.h>
|
---|
| 50 | #include <synch/rwlock.h>
|
---|
| 51 | #include <cpu.h>
|
---|
| 52 | #include <func.h>
|
---|
| 53 | #include <context.h>
|
---|
[016acbe] | 54 | #include <adt/btree.h>
|
---|
[5c9a08b] | 55 | #include <adt/list.h>
|
---|
[f761f1eb] | 56 | #include <time/clock.h>
|
---|
[b3f8fb7] | 57 | #include <time/timeout.h>
|
---|
[4ffa9e0] | 58 | #include <config.h>
|
---|
| 59 | #include <arch/interrupt.h>
|
---|
[26a8604f] | 60 | #include <smp/ipi.h>
|
---|
[f2ffad4] | 61 | #include <arch/faddr.h>
|
---|
[23684b7] | 62 | #include <atomic.h>
|
---|
[9c0a9b3] | 63 | #include <memstr.h>
|
---|
[55ab0f1] | 64 | #include <print.h>
|
---|
[266294a9] | 65 | #include <mm/slab.h>
|
---|
| 66 | #include <debug.h>
|
---|
[9f52563] | 67 | #include <main/uinit.h>
|
---|
[e3c762cd] | 68 | #include <syscall/copy.h>
|
---|
| 69 | #include <errno.h>
|
---|
[f761f1eb] | 70 |
|
---|
[fe19611] | 71 |
|
---|
| 72 | /** Thread states */
|
---|
| 73 | char *thread_states[] = {
|
---|
| 74 | "Invalid",
|
---|
| 75 | "Running",
|
---|
| 76 | "Sleeping",
|
---|
| 77 | "Ready",
|
---|
| 78 | "Entering",
|
---|
| 79 | "Exiting",
|
---|
| 80 | "Undead"
|
---|
| 81 | };
|
---|
[f761f1eb] | 82 |
|
---|
[4e33b6b] | 83 | /** Lock protecting the threads_btree B+tree.
|
---|
| 84 | *
|
---|
| 85 | * For locking rules, see declaration thereof.
|
---|
| 86 | */
|
---|
[016acbe] | 87 | SPINLOCK_INITIALIZE(threads_lock);
|
---|
[88169d9] | 88 |
|
---|
| 89 | /** B+tree of all threads.
|
---|
| 90 | *
|
---|
[4e33b6b] | 91 | * When a thread is found in the threads_btree B+tree, it is guaranteed to
|
---|
| 92 | * exist as long as the threads_lock is held.
|
---|
[88169d9] | 93 | */
|
---|
| 94 | btree_t threads_btree;
|
---|
[f761f1eb] | 95 |
|
---|
[dc747e3] | 96 | SPINLOCK_INITIALIZE(tidlock);
|
---|
[201abde] | 97 | thread_id_t last_tid = 0;
|
---|
[f761f1eb] | 98 |
|
---|
[266294a9] | 99 | static slab_cache_t *thread_slab;
|
---|
[f76fed4] | 100 | #ifdef ARCH_HAS_FPU
|
---|
| 101 | slab_cache_t *fpu_context_slab;
|
---|
| 102 | #endif
|
---|
[266294a9] | 103 |
|
---|
[4e33b6b] | 104 | /** Thread wrapper.
|
---|
[70527f1] | 105 | *
|
---|
[4e33b6b] | 106 | * This wrapper is provided to ensure that every thread makes a call to
|
---|
| 107 | * thread_exit() when its implementing function returns.
|
---|
[f761f1eb] | 108 | *
|
---|
[22f7769] | 109 | * interrupts_disable() is assumed.
|
---|
[70527f1] | 110 | *
|
---|
[f761f1eb] | 111 | */
|
---|
[e16e036a] | 112 | static void cushion(void)
|
---|
[f761f1eb] | 113 | {
|
---|
[43114c5] | 114 | void (*f)(void *) = THREAD->thread_code;
|
---|
| 115 | void *arg = THREAD->thread_arg;
|
---|
[449dc1ed] | 116 | THREAD->last_cycle = get_cycle();
|
---|
[f761f1eb] | 117 |
|
---|
[0313ff0] | 118 | /* This is where each thread wakes up after its creation */
|
---|
[43114c5] | 119 | spinlock_unlock(&THREAD->lock);
|
---|
[22f7769] | 120 | interrupts_enable();
|
---|
[f761f1eb] | 121 |
|
---|
| 122 | f(arg);
|
---|
[0313ff0] | 123 |
|
---|
| 124 | /* Accumulate accounting to the task */
|
---|
| 125 | ipl_t ipl = interrupts_disable();
|
---|
| 126 |
|
---|
| 127 | spinlock_lock(&THREAD->lock);
|
---|
[62b6d17] | 128 | if (!THREAD->uncounted) {
|
---|
| 129 | thread_update_accounting();
|
---|
| 130 | uint64_t cycles = THREAD->cycles;
|
---|
| 131 | THREAD->cycles = 0;
|
---|
| 132 | spinlock_unlock(&THREAD->lock);
|
---|
| 133 |
|
---|
| 134 | spinlock_lock(&TASK->lock);
|
---|
| 135 | TASK->cycles += cycles;
|
---|
| 136 | spinlock_unlock(&TASK->lock);
|
---|
| 137 | } else
|
---|
| 138 | spinlock_unlock(&THREAD->lock);
|
---|
[0313ff0] | 139 |
|
---|
| 140 | interrupts_restore(ipl);
|
---|
| 141 |
|
---|
[f761f1eb] | 142 | thread_exit();
|
---|
| 143 | /* not reached */
|
---|
| 144 | }
|
---|
| 145 |
|
---|
[266294a9] | 146 | /** Initialization and allocation for thread_t structure */
|
---|
| 147 | static int thr_constructor(void *obj, int kmflags)
|
---|
| 148 | {
|
---|
[764c302] | 149 | thread_t *t = (thread_t *) obj;
|
---|
[266294a9] | 150 |
|
---|
| 151 | spinlock_initialize(&t->lock, "thread_t_lock");
|
---|
| 152 | link_initialize(&t->rq_link);
|
---|
| 153 | link_initialize(&t->wq_link);
|
---|
| 154 | link_initialize(&t->th_link);
|
---|
[32fffef0] | 155 |
|
---|
| 156 | /* call the architecture-specific part of the constructor */
|
---|
| 157 | thr_constructor_arch(t);
|
---|
[266294a9] | 158 |
|
---|
[f76fed4] | 159 | #ifdef ARCH_HAS_FPU
|
---|
| 160 | # ifdef CONFIG_FPU_LAZY
|
---|
| 161 | t->saved_fpu_context = NULL;
|
---|
| 162 | # else
|
---|
| 163 | t->saved_fpu_context = slab_alloc(fpu_context_slab,kmflags);
|
---|
| 164 | if (!t->saved_fpu_context)
|
---|
| 165 | return -1;
|
---|
| 166 | # endif
|
---|
| 167 | #endif
|
---|
| 168 |
|
---|
[4184e76] | 169 | t->kstack = (uint8_t *) frame_alloc(STACK_FRAMES, FRAME_KA | kmflags);
|
---|
[e45f81a] | 170 | if (! t->kstack) {
|
---|
[f76fed4] | 171 | #ifdef ARCH_HAS_FPU
|
---|
| 172 | if (t->saved_fpu_context)
|
---|
| 173 | slab_free(fpu_context_slab,t->saved_fpu_context);
|
---|
| 174 | #endif
|
---|
[266294a9] | 175 | return -1;
|
---|
[f76fed4] | 176 | }
|
---|
[266294a9] | 177 |
|
---|
| 178 | return 0;
|
---|
| 179 | }
|
---|
| 180 |
|
---|
| 181 | /** Destruction of thread_t object */
|
---|
| 182 | static int thr_destructor(void *obj)
|
---|
| 183 | {
|
---|
[764c302] | 184 | thread_t *t = (thread_t *) obj;
|
---|
[266294a9] | 185 |
|
---|
[32fffef0] | 186 | /* call the architecture-specific part of the destructor */
|
---|
| 187 | thr_destructor_arch(t);
|
---|
| 188 |
|
---|
[2e9eae2] | 189 | frame_free(KA2PA(t->kstack));
|
---|
[f76fed4] | 190 | #ifdef ARCH_HAS_FPU
|
---|
| 191 | if (t->saved_fpu_context)
|
---|
| 192 | slab_free(fpu_context_slab,t->saved_fpu_context);
|
---|
| 193 | #endif
|
---|
[266294a9] | 194 | return 1; /* One page freed */
|
---|
| 195 | }
|
---|
[70527f1] | 196 |
|
---|
| 197 | /** Initialize threads
|
---|
| 198 | *
|
---|
| 199 | * Initialize kernel threads support.
|
---|
| 200 | *
|
---|
| 201 | */
|
---|
[f761f1eb] | 202 | void thread_init(void)
|
---|
| 203 | {
|
---|
[43114c5] | 204 | THREAD = NULL;
|
---|
[80d2bdb] | 205 | atomic_set(&nrdy,0);
|
---|
[4e33b6b] | 206 | thread_slab = slab_cache_create("thread_slab", sizeof(thread_t), 0,
|
---|
[6f4495f5] | 207 | thr_constructor, thr_destructor, 0);
|
---|
[4e33b6b] | 208 |
|
---|
[f76fed4] | 209 | #ifdef ARCH_HAS_FPU
|
---|
[4e33b6b] | 210 | fpu_context_slab = slab_cache_create("fpu_slab", sizeof(fpu_context_t),
|
---|
[6f4495f5] | 211 | FPU_CONTEXT_ALIGN, NULL, NULL, 0);
|
---|
[f76fed4] | 212 | #endif
|
---|
[f761f1eb] | 213 |
|
---|
[016acbe] | 214 | btree_create(&threads_btree);
|
---|
| 215 | }
|
---|
[70527f1] | 216 |
|
---|
| 217 | /** Make thread ready
|
---|
| 218 | *
|
---|
| 219 | * Switch thread t to the ready state.
|
---|
| 220 | *
|
---|
| 221 | * @param t Thread to make ready.
|
---|
| 222 | *
|
---|
| 223 | */
|
---|
[f761f1eb] | 224 | void thread_ready(thread_t *t)
|
---|
| 225 | {
|
---|
| 226 | cpu_t *cpu;
|
---|
| 227 | runq_t *r;
|
---|
[22f7769] | 228 | ipl_t ipl;
|
---|
[80d2bdb] | 229 | int i, avg;
|
---|
[f761f1eb] | 230 |
|
---|
[22f7769] | 231 | ipl = interrupts_disable();
|
---|
[f761f1eb] | 232 |
|
---|
| 233 | spinlock_lock(&t->lock);
|
---|
| 234 |
|
---|
[fbcfd458] | 235 | ASSERT(! (t->state == Ready));
|
---|
| 236 |
|
---|
[4e33b6b] | 237 | i = (t->priority < RQ_COUNT - 1) ? ++t->priority : t->priority;
|
---|
[f761f1eb] | 238 |
|
---|
[8262010] | 239 | cpu = CPU;
|
---|
[32fffef0] | 240 | if (t->flags & THREAD_FLAG_WIRED) {
|
---|
[4365d10] | 241 | ASSERT(t->cpu != NULL);
|
---|
[f761f1eb] | 242 | cpu = t->cpu;
|
---|
| 243 | }
|
---|
[81c4c6da] | 244 | t->state = Ready;
|
---|
[f761f1eb] | 245 | spinlock_unlock(&t->lock);
|
---|
| 246 |
|
---|
[70527f1] | 247 | /*
|
---|
[f761f1eb] | 248 | * Append t to respective ready queue on respective processor.
|
---|
| 249 | */
|
---|
| 250 | r = &cpu->rq[i];
|
---|
| 251 | spinlock_lock(&r->lock);
|
---|
| 252 | list_append(&t->rq_link, &r->rq_head);
|
---|
| 253 | r->n++;
|
---|
| 254 | spinlock_unlock(&r->lock);
|
---|
| 255 |
|
---|
[59e07c91] | 256 | atomic_inc(&nrdy);
|
---|
[80d2bdb] | 257 | avg = atomic_get(&nrdy) / config.cpu_active;
|
---|
[248fc1a] | 258 | atomic_inc(&cpu->nrdy);
|
---|
[76cec1e] | 259 |
|
---|
[22f7769] | 260 | interrupts_restore(ipl);
|
---|
[f761f1eb] | 261 | }
|
---|
| 262 |
|
---|
[266294a9] | 263 | /** Destroy thread memory structure
|
---|
| 264 | *
|
---|
| 265 | * Detach thread from all queues, cpus etc. and destroy it.
|
---|
| 266 | *
|
---|
| 267 | * Assume thread->lock is held!!
|
---|
| 268 | */
|
---|
| 269 | void thread_destroy(thread_t *t)
|
---|
| 270 | {
|
---|
[4e33b6b] | 271 | bool destroy_task = false;
|
---|
[7509ddc] | 272 |
|
---|
[c778c1a] | 273 | ASSERT(t->state == Exiting || t->state == Undead);
|
---|
[266294a9] | 274 | ASSERT(t->task);
|
---|
| 275 | ASSERT(t->cpu);
|
---|
| 276 |
|
---|
| 277 | spinlock_lock(&t->cpu->lock);
|
---|
[4e33b6b] | 278 | if(t->cpu->fpu_owner == t)
|
---|
| 279 | t->cpu->fpu_owner = NULL;
|
---|
[266294a9] | 280 | spinlock_unlock(&t->cpu->lock);
|
---|
| 281 |
|
---|
[7509ddc] | 282 | spinlock_unlock(&t->lock);
|
---|
| 283 |
|
---|
| 284 | spinlock_lock(&threads_lock);
|
---|
[7f1c620] | 285 | btree_remove(&threads_btree, (btree_key_t) ((uintptr_t ) t), NULL);
|
---|
[7509ddc] | 286 | spinlock_unlock(&threads_lock);
|
---|
| 287 |
|
---|
[266294a9] | 288 | /*
|
---|
| 289 | * Detach from the containing task.
|
---|
| 290 | */
|
---|
| 291 | spinlock_lock(&t->task->lock);
|
---|
| 292 | list_remove(&t->th_link);
|
---|
[7509ddc] | 293 | if (--t->task->refcount == 0) {
|
---|
| 294 | t->task->accept_new_threads = false;
|
---|
| 295 | destroy_task = true;
|
---|
| 296 | }
|
---|
| 297 | spinlock_unlock(&t->task->lock);
|
---|
[266294a9] | 298 |
|
---|
[7509ddc] | 299 | if (destroy_task)
|
---|
| 300 | task_destroy(t->task);
|
---|
[266294a9] | 301 |
|
---|
| 302 | slab_free(thread_slab, t);
|
---|
| 303 | }
|
---|
| 304 |
|
---|
[70527f1] | 305 | /** Create new thread
|
---|
| 306 | *
|
---|
| 307 | * Create a new thread.
|
---|
| 308 | *
|
---|
[62b6d17] | 309 | * @param func Thread's implementing function.
|
---|
| 310 | * @param arg Thread's implementing function argument.
|
---|
| 311 | * @param task Task to which the thread belongs.
|
---|
| 312 | * @param flags Thread flags.
|
---|
| 313 | * @param name Symbolic name.
|
---|
[4e33b6b] | 314 | * @param uncounted Thread's accounting doesn't affect accumulated task
|
---|
| 315 | * accounting.
|
---|
[70527f1] | 316 | *
|
---|
| 317 | * @return New thread's structure on success, NULL on failure.
|
---|
| 318 | *
|
---|
| 319 | */
|
---|
[4e33b6b] | 320 | thread_t *thread_create(void (* func)(void *), void *arg, task_t *task,
|
---|
| 321 | int flags, char *name, bool uncounted)
|
---|
[f761f1eb] | 322 | {
|
---|
| 323 | thread_t *t;
|
---|
[bb68433] | 324 | ipl_t ipl;
|
---|
| 325 |
|
---|
[266294a9] | 326 | t = (thread_t *) slab_alloc(thread_slab, 0);
|
---|
[2a46e10] | 327 | if (!t)
|
---|
| 328 | return NULL;
|
---|
[f761f1eb] | 329 |
|
---|
[bb68433] | 330 | /* Not needed, but good for debugging */
|
---|
[4e33b6b] | 331 | memsetb((uintptr_t) t->kstack, THREAD_STACK_SIZE * 1 << STACK_FRAMES,
|
---|
[6f4495f5] | 332 | 0);
|
---|
[bb68433] | 333 |
|
---|
| 334 | ipl = interrupts_disable();
|
---|
| 335 | spinlock_lock(&tidlock);
|
---|
| 336 | t->tid = ++last_tid;
|
---|
| 337 | spinlock_unlock(&tidlock);
|
---|
| 338 | interrupts_restore(ipl);
|
---|
| 339 |
|
---|
| 340 | context_save(&t->saved_context);
|
---|
[4e33b6b] | 341 | context_set(&t->saved_context, FADDR(cushion), (uintptr_t) t->kstack,
|
---|
[6f4495f5] | 342 | THREAD_STACK_SIZE);
|
---|
[bb68433] | 343 |
|
---|
| 344 | the_initialize((the_t *) t->kstack);
|
---|
| 345 |
|
---|
| 346 | ipl = interrupts_disable();
|
---|
| 347 | t->saved_context.ipl = interrupts_read();
|
---|
| 348 | interrupts_restore(ipl);
|
---|
| 349 |
|
---|
[9f52563] | 350 | memcpy(t->name, name, THREAD_NAME_BUFLEN);
|
---|
| 351 |
|
---|
[bb68433] | 352 | t->thread_code = func;
|
---|
| 353 | t->thread_arg = arg;
|
---|
| 354 | t->ticks = -1;
|
---|
[cce6acf] | 355 | t->cycles = 0;
|
---|
[62b6d17] | 356 | t->uncounted = uncounted;
|
---|
[bb68433] | 357 | t->priority = -1; /* start in rq[0] */
|
---|
| 358 | t->cpu = NULL;
|
---|
[32fffef0] | 359 | t->flags = flags;
|
---|
[bb68433] | 360 | t->state = Entering;
|
---|
| 361 | t->call_me = NULL;
|
---|
| 362 | t->call_me_with = NULL;
|
---|
| 363 |
|
---|
| 364 | timeout_initialize(&t->sleep_timeout);
|
---|
[116d1ef4] | 365 | t->sleep_interruptible = false;
|
---|
[bb68433] | 366 | t->sleep_queue = NULL;
|
---|
| 367 | t->timeout_pending = 0;
|
---|
[e3c762cd] | 368 |
|
---|
| 369 | t->in_copy_from_uspace = false;
|
---|
| 370 | t->in_copy_to_uspace = false;
|
---|
[7509ddc] | 371 |
|
---|
| 372 | t->interrupted = false;
|
---|
[48e7dd6] | 373 | t->join_type = None;
|
---|
[fe19611] | 374 | t->detached = false;
|
---|
| 375 | waitq_initialize(&t->join_wq);
|
---|
| 376 |
|
---|
[bb68433] | 377 | t->rwlock_holder_type = RWLOCK_NONE;
|
---|
[6a27d63] | 378 |
|
---|
[bb68433] | 379 | t->task = task;
|
---|
| 380 |
|
---|
[6f8a426] | 381 | t->fpu_context_exists = 0;
|
---|
| 382 | t->fpu_context_engaged = 0;
|
---|
[32fffef0] | 383 |
|
---|
[4e33b6b] | 384 | /* might depend on previous initialization */
|
---|
| 385 | thread_create_arch(t);
|
---|
[bb68433] | 386 |
|
---|
[7509ddc] | 387 | /*
|
---|
| 388 | * Attach to the containing task.
|
---|
| 389 | */
|
---|
[0182a665] | 390 | ipl = interrupts_disable();
|
---|
[7509ddc] | 391 | spinlock_lock(&task->lock);
|
---|
| 392 | if (!task->accept_new_threads) {
|
---|
| 393 | spinlock_unlock(&task->lock);
|
---|
| 394 | slab_free(thread_slab, t);
|
---|
[0182a665] | 395 | interrupts_restore(ipl);
|
---|
[7509ddc] | 396 | return NULL;
|
---|
| 397 | }
|
---|
| 398 | list_append(&t->th_link, &task->th_head);
|
---|
[b91bb65] | 399 | if (task->refcount++ == 0)
|
---|
| 400 | task->main_thread = t;
|
---|
[7509ddc] | 401 | spinlock_unlock(&task->lock);
|
---|
| 402 |
|
---|
[bb68433] | 403 | /*
|
---|
| 404 | * Register this thread in the system-wide list.
|
---|
| 405 | */
|
---|
| 406 | spinlock_lock(&threads_lock);
|
---|
[4e33b6b] | 407 | btree_insert(&threads_btree, (btree_key_t) ((uintptr_t) t), (void *) t,
|
---|
[6f4495f5] | 408 | NULL);
|
---|
[bb68433] | 409 | spinlock_unlock(&threads_lock);
|
---|
| 410 |
|
---|
| 411 | interrupts_restore(ipl);
|
---|
[6f8a426] | 412 |
|
---|
[f761f1eb] | 413 | return t;
|
---|
| 414 | }
|
---|
| 415 |
|
---|
[0182a665] | 416 | /** Terminate thread.
|
---|
[70527f1] | 417 | *
|
---|
[4e33b6b] | 418 | * End current thread execution and switch it to the exiting state. All pending
|
---|
| 419 | * timeouts are executed.
|
---|
[70527f1] | 420 | */
|
---|
[f761f1eb] | 421 | void thread_exit(void)
|
---|
| 422 | {
|
---|
[22f7769] | 423 | ipl_t ipl;
|
---|
[f761f1eb] | 424 |
|
---|
| 425 | restart:
|
---|
[22f7769] | 426 | ipl = interrupts_disable();
|
---|
[43114c5] | 427 | spinlock_lock(&THREAD->lock);
|
---|
[4e33b6b] | 428 | if (THREAD->timeout_pending) {
|
---|
| 429 | /* busy waiting for timeouts in progress */
|
---|
[43114c5] | 430 | spinlock_unlock(&THREAD->lock);
|
---|
[22f7769] | 431 | interrupts_restore(ipl);
|
---|
[f761f1eb] | 432 | goto restart;
|
---|
| 433 | }
|
---|
[43114c5] | 434 | THREAD->state = Exiting;
|
---|
| 435 | spinlock_unlock(&THREAD->lock);
|
---|
[f761f1eb] | 436 | scheduler();
|
---|
[874621f] | 437 |
|
---|
| 438 | /* Not reached */
|
---|
| 439 | while (1)
|
---|
| 440 | ;
|
---|
[f761f1eb] | 441 | }
|
---|
| 442 |
|
---|
[70527f1] | 443 |
|
---|
| 444 | /** Thread sleep
|
---|
| 445 | *
|
---|
| 446 | * Suspend execution of the current thread.
|
---|
| 447 | *
|
---|
| 448 | * @param sec Number of seconds to sleep.
|
---|
| 449 | *
|
---|
| 450 | */
|
---|
[7f1c620] | 451 | void thread_sleep(uint32_t sec)
|
---|
[f761f1eb] | 452 | {
|
---|
[4e33b6b] | 453 | thread_usleep(sec * 1000000);
|
---|
[f761f1eb] | 454 | }
|
---|
[70527f1] | 455 |
|
---|
[fe19611] | 456 | /** Wait for another thread to exit.
|
---|
| 457 | *
|
---|
| 458 | * @param t Thread to join on exit.
|
---|
| 459 | * @param usec Timeout in microseconds.
|
---|
| 460 | * @param flags Mode of operation.
|
---|
| 461 | *
|
---|
| 462 | * @return An error code from errno.h or an error code from synch.h.
|
---|
| 463 | */
|
---|
[7f1c620] | 464 | int thread_join_timeout(thread_t *t, uint32_t usec, int flags)
|
---|
[fe19611] | 465 | {
|
---|
| 466 | ipl_t ipl;
|
---|
| 467 | int rc;
|
---|
| 468 |
|
---|
| 469 | if (t == THREAD)
|
---|
| 470 | return EINVAL;
|
---|
| 471 |
|
---|
| 472 | /*
|
---|
| 473 | * Since thread join can only be called once on an undetached thread,
|
---|
| 474 | * the thread pointer is guaranteed to be still valid.
|
---|
| 475 | */
|
---|
| 476 |
|
---|
| 477 | ipl = interrupts_disable();
|
---|
| 478 | spinlock_lock(&t->lock);
|
---|
| 479 | ASSERT(!t->detached);
|
---|
| 480 | spinlock_unlock(&t->lock);
|
---|
[7b3e7f4] | 481 | interrupts_restore(ipl);
|
---|
[fe19611] | 482 |
|
---|
[0182a665] | 483 | rc = waitq_sleep_timeout(&t->join_wq, usec, flags);
|
---|
| 484 |
|
---|
[fe19611] | 485 | return rc;
|
---|
| 486 | }
|
---|
| 487 |
|
---|
| 488 | /** Detach thread.
|
---|
| 489 | *
|
---|
| 490 | * Mark the thread as detached, if the thread is already in the Undead state,
|
---|
| 491 | * deallocate its resources.
|
---|
| 492 | *
|
---|
| 493 | * @param t Thread to be detached.
|
---|
| 494 | */
|
---|
| 495 | void thread_detach(thread_t *t)
|
---|
| 496 | {
|
---|
| 497 | ipl_t ipl;
|
---|
| 498 |
|
---|
| 499 | /*
|
---|
[31d8e10] | 500 | * Since the thread is expected not to be already detached,
|
---|
[fe19611] | 501 | * pointer to it must be still valid.
|
---|
| 502 | */
|
---|
| 503 | ipl = interrupts_disable();
|
---|
| 504 | spinlock_lock(&t->lock);
|
---|
| 505 | ASSERT(!t->detached);
|
---|
| 506 | if (t->state == Undead) {
|
---|
| 507 | thread_destroy(t); /* unlocks &t->lock */
|
---|
| 508 | interrupts_restore(ipl);
|
---|
| 509 | return;
|
---|
| 510 | } else {
|
---|
| 511 | t->detached = true;
|
---|
| 512 | }
|
---|
| 513 | spinlock_unlock(&t->lock);
|
---|
| 514 | interrupts_restore(ipl);
|
---|
| 515 | }
|
---|
| 516 |
|
---|
[70527f1] | 517 | /** Thread usleep
|
---|
| 518 | *
|
---|
| 519 | * Suspend execution of the current thread.
|
---|
| 520 | *
|
---|
| 521 | * @param usec Number of microseconds to sleep.
|
---|
| 522 | *
|
---|
| 523 | */
|
---|
[7f1c620] | 524 | void thread_usleep(uint32_t usec)
|
---|
[f761f1eb] | 525 | {
|
---|
| 526 | waitq_t wq;
|
---|
| 527 |
|
---|
| 528 | waitq_initialize(&wq);
|
---|
| 529 |
|
---|
[116d1ef4] | 530 | (void) waitq_sleep_timeout(&wq, usec, SYNCH_FLAGS_NON_BLOCKING);
|
---|
[f761f1eb] | 531 | }
|
---|
| 532 |
|
---|
[70527f1] | 533 | /** Register thread out-of-context invocation
|
---|
| 534 | *
|
---|
| 535 | * Register a function and its argument to be executed
|
---|
| 536 | * on next context switch to the current thread.
|
---|
| 537 | *
|
---|
| 538 | * @param call_me Out-of-context function.
|
---|
| 539 | * @param call_me_with Out-of-context function argument.
|
---|
| 540 | *
|
---|
| 541 | */
|
---|
[f761f1eb] | 542 | void thread_register_call_me(void (* call_me)(void *), void *call_me_with)
|
---|
| 543 | {
|
---|
[22f7769] | 544 | ipl_t ipl;
|
---|
[f761f1eb] | 545 |
|
---|
[22f7769] | 546 | ipl = interrupts_disable();
|
---|
[43114c5] | 547 | spinlock_lock(&THREAD->lock);
|
---|
| 548 | THREAD->call_me = call_me;
|
---|
| 549 | THREAD->call_me_with = call_me_with;
|
---|
| 550 | spinlock_unlock(&THREAD->lock);
|
---|
[22f7769] | 551 | interrupts_restore(ipl);
|
---|
[f761f1eb] | 552 | }
|
---|
[55ab0f1] | 553 |
|
---|
| 554 | /** Print list of threads debug info */
|
---|
| 555 | void thread_print_list(void)
|
---|
| 556 | {
|
---|
| 557 | link_t *cur;
|
---|
| 558 | ipl_t ipl;
|
---|
| 559 |
|
---|
| 560 | /* Messing with thread structures, avoid deadlock */
|
---|
| 561 | ipl = interrupts_disable();
|
---|
| 562 | spinlock_lock(&threads_lock);
|
---|
[cce6acf] | 563 |
|
---|
[6f4495f5] | 564 | printf("tid name address state task ctx code "
|
---|
| 565 | " stack cycles cpu kstack waitqueue\n");
|
---|
| 566 | printf("------ ---------- ---------- -------- ---------- --- --------"
|
---|
| 567 | "-- ---------- ---------- ---- ---------- ----------\n");
|
---|
[55ab0f1] | 568 |
|
---|
[6f4495f5] | 569 | for (cur = threads_btree.leaf_head.next;
|
---|
| 570 | cur != &threads_btree.leaf_head; cur = cur->next) {
|
---|
[016acbe] | 571 | btree_node_t *node;
|
---|
[4184e76] | 572 | unsigned int i;
|
---|
[016acbe] | 573 |
|
---|
| 574 | node = list_get_instance(cur, btree_node_t, leaf_link);
|
---|
| 575 | for (i = 0; i < node->keys; i++) {
|
---|
| 576 | thread_t *t;
|
---|
| 577 |
|
---|
| 578 | t = (thread_t *) node->value[i];
|
---|
[cce6acf] | 579 |
|
---|
| 580 | uint64_t cycles;
|
---|
| 581 | char suffix;
|
---|
[95155b0c] | 582 | order(t->cycles, &cycles, &suffix);
|
---|
[cce6acf] | 583 |
|
---|
[201abde] | 584 | printf("%-6llu %-10s %#10zx %-8s %#10zx %-3ld %#10zx "
|
---|
[6f4495f5] | 585 | "%#10zx %9llu%c ", t->tid, t->name, t,
|
---|
| 586 | thread_states[t->state], t->task, t->task->context,
|
---|
| 587 | t->thread_code, t->kstack, cycles, suffix);
|
---|
[cce6acf] | 588 |
|
---|
[016acbe] | 589 | if (t->cpu)
|
---|
[cce6acf] | 590 | printf("%-4zd", t->cpu->id);
|
---|
[016acbe] | 591 | else
|
---|
| 592 | printf("none");
|
---|
[cce6acf] | 593 |
|
---|
| 594 | if (t->state == Sleeping)
|
---|
[6f4495f5] | 595 | printf(" %#10zx %#10zx", t->kstack,
|
---|
| 596 | t->sleep_queue);
|
---|
[cce6acf] | 597 |
|
---|
[016acbe] | 598 | printf("\n");
|
---|
| 599 | }
|
---|
[55ab0f1] | 600 | }
|
---|
| 601 |
|
---|
| 602 | spinlock_unlock(&threads_lock);
|
---|
[37c57f2] | 603 | interrupts_restore(ipl);
|
---|
[55ab0f1] | 604 | }
|
---|
[9f52563] | 605 |
|
---|
[016acbe] | 606 | /** Check whether thread exists.
|
---|
| 607 | *
|
---|
| 608 | * Note that threads_lock must be already held and
|
---|
| 609 | * interrupts must be already disabled.
|
---|
| 610 | *
|
---|
| 611 | * @param t Pointer to thread.
|
---|
| 612 | *
|
---|
| 613 | * @return True if thread t is known to the system, false otherwise.
|
---|
| 614 | */
|
---|
| 615 | bool thread_exists(thread_t *t)
|
---|
| 616 | {
|
---|
| 617 | btree_node_t *leaf;
|
---|
| 618 |
|
---|
[6f4495f5] | 619 | return btree_search(&threads_btree, (btree_key_t) ((uintptr_t) t),
|
---|
| 620 | &leaf) != NULL;
|
---|
[016acbe] | 621 | }
|
---|
| 622 |
|
---|
[cce6acf] | 623 |
|
---|
| 624 | /** Update accounting of current thread.
|
---|
| 625 | *
|
---|
| 626 | * Note that thread_lock on THREAD must be already held and
|
---|
| 627 | * interrupts must be already disabled.
|
---|
| 628 | *
|
---|
| 629 | */
|
---|
| 630 | void thread_update_accounting(void)
|
---|
| 631 | {
|
---|
| 632 | uint64_t time = get_cycle();
|
---|
| 633 | THREAD->cycles += time - THREAD->last_cycle;
|
---|
| 634 | THREAD->last_cycle = time;
|
---|
| 635 | }
|
---|
| 636 |
|
---|
[9f52563] | 637 | /** Process syscall to create new thread.
|
---|
| 638 | *
|
---|
| 639 | */
|
---|
[201abde] | 640 | unative_t sys_thread_create(uspace_arg_t *uspace_uarg, char *uspace_name, thread_id_t *uspace_thread_id)
|
---|
[9f52563] | 641 | {
|
---|
[68091bd] | 642 | thread_t *t;
|
---|
| 643 | char namebuf[THREAD_NAME_BUFLEN];
|
---|
[45fb65c] | 644 | uspace_arg_t *kernel_uarg;
|
---|
[e3c762cd] | 645 | int rc;
|
---|
[9f52563] | 646 |
|
---|
[e3c762cd] | 647 | rc = copy_from_uspace(namebuf, uspace_name, THREAD_NAME_BUFLEN);
|
---|
| 648 | if (rc != 0)
|
---|
[7f1c620] | 649 | return (unative_t) rc;
|
---|
[0f250f9] | 650 |
|
---|
| 651 | kernel_uarg = (uspace_arg_t *) malloc(sizeof(uspace_arg_t), 0);
|
---|
[e3c762cd] | 652 | rc = copy_from_uspace(kernel_uarg, uspace_uarg, sizeof(uspace_arg_t));
|
---|
| 653 | if (rc != 0) {
|
---|
| 654 | free(kernel_uarg);
|
---|
[7f1c620] | 655 | return (unative_t) rc;
|
---|
[e3c762cd] | 656 | }
|
---|
[9f52563] | 657 |
|
---|
[6f4495f5] | 658 | t = thread_create(uinit, kernel_uarg, TASK, THREAD_FLAG_USPACE, namebuf,
|
---|
| 659 | false);
|
---|
| 660 | if (t) {
|
---|
[68091bd] | 661 | thread_ready(t);
|
---|
[201abde] | 662 | if (uspace_thread_id != NULL)
|
---|
| 663 | return (unative_t) copy_to_uspace(uspace_thread_id, &t->tid,
|
---|
[de33dab] | 664 | sizeof(t->tid));
|
---|
[201abde] | 665 | else
|
---|
| 666 | return 0;
|
---|
| 667 | } else
|
---|
[0f250f9] | 668 | free(kernel_uarg);
|
---|
[9f52563] | 669 |
|
---|
[7f1c620] | 670 | return (unative_t) ENOMEM;
|
---|
[9f52563] | 671 | }
|
---|
| 672 |
|
---|
| 673 | /** Process syscall to terminate thread.
|
---|
| 674 | *
|
---|
| 675 | */
|
---|
[7f1c620] | 676 | unative_t sys_thread_exit(int uspace_status)
|
---|
[9f52563] | 677 | {
|
---|
[68091bd] | 678 | thread_exit();
|
---|
| 679 | /* Unreachable */
|
---|
| 680 | return 0;
|
---|
[9f52563] | 681 | }
|
---|
[b45c443] | 682 |
|
---|
[3ce7f082] | 683 | /** Syscall for getting TID.
|
---|
| 684 | *
|
---|
[201abde] | 685 | * @param uspace_thread_id Userspace address of 8-byte buffer where to store
|
---|
| 686 | * current thread ID.
|
---|
| 687 | *
|
---|
| 688 | * @return 0 on success or an error code from @ref errno.h.
|
---|
[b45c443] | 689 | */
|
---|
[201abde] | 690 | unative_t sys_thread_get_id(thread_id_t *uspace_thread_id)
|
---|
[3ce7f082] | 691 | {
|
---|
| 692 | /*
|
---|
| 693 | * No need to acquire lock on THREAD because tid
|
---|
| 694 | * remains constant for the lifespan of the thread.
|
---|
| 695 | */
|
---|
[201abde] | 696 | return (unative_t) copy_to_uspace(uspace_thread_id, &THREAD->tid,
|
---|
| 697 | sizeof(THREAD->tid));
|
---|
[3ce7f082] | 698 | }
|
---|
[6f4495f5] | 699 |
|
---|
[3ce7f082] | 700 | /** @}
|
---|
| 701 | */
|
---|