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