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