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