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