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