[f761f1eb] | 1 | /*
|
---|
[7ed8530] | 2 | * Copyright (c) 2010 Jakub Jermar
|
---|
[ef1eab7] | 3 | * Copyright (c) 2018 Jiri Svoboda
|
---|
[f761f1eb] | 4 | * All rights reserved.
|
---|
| 5 | *
|
---|
| 6 | * Redistribution and use in source and binary forms, with or without
|
---|
| 7 | * modification, are permitted provided that the following conditions
|
---|
| 8 | * are met:
|
---|
| 9 | *
|
---|
| 10 | * - Redistributions of source code must retain the above copyright
|
---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
---|
| 12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 13 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 14 | * documentation and/or other materials provided with the distribution.
|
---|
| 15 | * - The name of the author may not be used to endorse or promote products
|
---|
| 16 | * derived from this software without specific prior written permission.
|
---|
| 17 | *
|
---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 28 | */
|
---|
| 29 |
|
---|
[174156fd] | 30 | /** @addtogroup kernel_generic_proc
|
---|
[b45c443] | 31 | * @{
|
---|
| 32 | */
|
---|
| 33 |
|
---|
[9179d0a] | 34 | /**
|
---|
[b45c443] | 35 | * @file
|
---|
[da1bafb] | 36 | * @brief Thread management functions.
|
---|
[9179d0a] | 37 | */
|
---|
| 38 |
|
---|
[63e27ef] | 39 | #include <assert.h>
|
---|
[f761f1eb] | 40 | #include <proc/scheduler.h>
|
---|
| 41 | #include <proc/thread.h>
|
---|
| 42 | #include <proc/task.h>
|
---|
| 43 | #include <mm/frame.h>
|
---|
| 44 | #include <mm/page.h>
|
---|
| 45 | #include <arch/asm.h>
|
---|
[cce6acf] | 46 | #include <arch/cycle.h>
|
---|
[f761f1eb] | 47 | #include <arch.h>
|
---|
| 48 | #include <synch/spinlock.h>
|
---|
| 49 | #include <synch/waitq.h>
|
---|
[d314571] | 50 | #include <synch/syswaitq.h>
|
---|
[f761f1eb] | 51 | #include <cpu.h>
|
---|
[e535eeb] | 52 | #include <str.h>
|
---|
[f761f1eb] | 53 | #include <context.h>
|
---|
[5c9a08b] | 54 | #include <adt/list.h>
|
---|
[ef1eab7] | 55 | #include <adt/odict.h>
|
---|
[f761f1eb] | 56 | #include <time/clock.h>
|
---|
[b3f8fb7] | 57 | #include <time/timeout.h>
|
---|
[8d6c1f1] | 58 | #include <time/delay.h>
|
---|
[4ffa9e0] | 59 | #include <config.h>
|
---|
| 60 | #include <arch/interrupt.h>
|
---|
[26a8604f] | 61 | #include <smp/ipi.h>
|
---|
[23684b7] | 62 | #include <atomic.h>
|
---|
[b169619] | 63 | #include <memw.h>
|
---|
[bab75df6] | 64 | #include <stdio.h>
|
---|
[aafed15] | 65 | #include <stdlib.h>
|
---|
[9f52563] | 66 | #include <main/uinit.h>
|
---|
[e3c762cd] | 67 | #include <syscall/copy.h>
|
---|
| 68 | #include <errno.h>
|
---|
[aae365bc] | 69 | #include <debug.h>
|
---|
[111b9b9] | 70 | #include <halt.h>
|
---|
[52755f1] | 71 |
|
---|
[fe19611] | 72 | /** Thread states */
|
---|
[a000878c] | 73 | const char *thread_states[] = {
|
---|
[fe19611] | 74 | "Invalid",
|
---|
| 75 | "Running",
|
---|
| 76 | "Sleeping",
|
---|
| 77 | "Ready",
|
---|
| 78 | "Entering",
|
---|
| 79 | "Exiting",
|
---|
[48d14222] | 80 | "Lingering"
|
---|
[e1b6742] | 81 | };
|
---|
| 82 |
|
---|
[ef1eab7] | 83 | /** Lock protecting the @c threads ordered dictionary .
|
---|
[4e33b6b] | 84 | *
|
---|
| 85 | * For locking rules, see declaration thereof.
|
---|
| 86 | */
|
---|
[da1bafb] | 87 | IRQ_SPINLOCK_INITIALIZE(threads_lock);
|
---|
[88169d9] | 88 |
|
---|
[ef1eab7] | 89 | /** Ordered dictionary of all threads by their address (i.e. pointer to
|
---|
| 90 | * the thread_t structure).
|
---|
[88169d9] | 91 | *
|
---|
[ef1eab7] | 92 | * When a thread is found in the @c threads ordered dictionary, it is
|
---|
| 93 | * guaranteed to exist as long as the @c threads_lock is held.
|
---|
[da1bafb] | 94 | *
|
---|
[ef1eab7] | 95 | * Members are of type thread_t.
|
---|
[1871118] | 96 | *
|
---|
| 97 | * This structure contains weak references. Any reference from it must not leave
|
---|
| 98 | * threads_lock critical section unless strengthened via thread_try_ref().
|
---|
[88169d9] | 99 | */
|
---|
[ef1eab7] | 100 | odict_t threads;
|
---|
[f761f1eb] | 101 |
|
---|
[da1bafb] | 102 | IRQ_SPINLOCK_STATIC_INITIALIZE(tidlock);
|
---|
| 103 | static thread_id_t last_tid = 0;
|
---|
[f761f1eb] | 104 |
|
---|
[82d515e9] | 105 | static slab_cache_t *thread_cache;
|
---|
[da1bafb] | 106 |
|
---|
[ef1eab7] | 107 | static void *threads_getkey(odlink_t *);
|
---|
| 108 | static int threads_cmp(void *, void *);
|
---|
| 109 |
|
---|
[da1bafb] | 110 | /** Initialization and allocation for thread_t structure
|
---|
| 111 | *
|
---|
| 112 | */
|
---|
[b7fd2a0] | 113 | static errno_t thr_constructor(void *obj, unsigned int kmflags)
|
---|
[266294a9] | 114 | {
|
---|
[da1bafb] | 115 | thread_t *thread = (thread_t *) obj;
|
---|
[a35b458] | 116 |
|
---|
[da1bafb] | 117 | irq_spinlock_initialize(&thread->lock, "thread_t_lock");
|
---|
| 118 | link_initialize(&thread->rq_link);
|
---|
| 119 | link_initialize(&thread->wq_link);
|
---|
| 120 | link_initialize(&thread->th_link);
|
---|
[a35b458] | 121 |
|
---|
[32fffef0] | 122 | /* call the architecture-specific part of the constructor */
|
---|
[da1bafb] | 123 | thr_constructor_arch(thread);
|
---|
[a35b458] | 124 |
|
---|
[38ff925] | 125 | /*
|
---|
| 126 | * Allocate the kernel stack from the low-memory to prevent an infinite
|
---|
| 127 | * nesting of TLB-misses when accessing the stack from the part of the
|
---|
| 128 | * TLB-miss handler written in C.
|
---|
| 129 | *
|
---|
| 130 | * Note that low-memory is safe to be used for the stack as it will be
|
---|
| 131 | * covered by the kernel identity mapping, which guarantees not to
|
---|
| 132 | * nest TLB-misses infinitely (either via some hardware mechanism or
|
---|
[c477c80] | 133 | * by the construction of the assembly-language part of the TLB-miss
|
---|
[38ff925] | 134 | * handler).
|
---|
| 135 | *
|
---|
| 136 | * This restriction can be lifted once each architecture provides
|
---|
[c477c80] | 137 | * a similar guarantee, for example, by locking the kernel stack
|
---|
[38ff925] | 138 | * in the TLB whenever it is allocated from the high-memory and the
|
---|
| 139 | * thread is being scheduled to run.
|
---|
| 140 | */
|
---|
| 141 | kmflags |= FRAME_LOWMEM;
|
---|
| 142 | kmflags &= ~FRAME_HIGHMEM;
|
---|
[a35b458] | 143 |
|
---|
[128359eb] | 144 | /*
|
---|
| 145 | * NOTE: All kernel stacks must be aligned to STACK_SIZE,
|
---|
| 146 | * see CURRENT.
|
---|
| 147 | */
|
---|
[d1da1ff2] | 148 |
|
---|
[cd3b380] | 149 | uintptr_t stack_phys =
|
---|
| 150 | frame_alloc(STACK_FRAMES, kmflags, STACK_SIZE - 1);
|
---|
[0366d09d] | 151 | if (!stack_phys)
|
---|
[7f11dc6] | 152 | return ENOMEM;
|
---|
[a35b458] | 153 |
|
---|
[cd3b380] | 154 | thread->kstack = (uint8_t *) PA2KA(stack_phys);
|
---|
[a35b458] | 155 |
|
---|
[9a1b20c] | 156 | #ifdef CONFIG_UDEBUG
|
---|
[da1bafb] | 157 | mutex_initialize(&thread->udebug.lock, MUTEX_PASSIVE);
|
---|
[9a1b20c] | 158 | #endif
|
---|
[a35b458] | 159 |
|
---|
[7f11dc6] | 160 | return EOK;
|
---|
[266294a9] | 161 | }
|
---|
| 162 |
|
---|
| 163 | /** Destruction of thread_t object */
|
---|
[da1bafb] | 164 | static size_t thr_destructor(void *obj)
|
---|
[266294a9] | 165 | {
|
---|
[da1bafb] | 166 | thread_t *thread = (thread_t *) obj;
|
---|
[a35b458] | 167 |
|
---|
[32fffef0] | 168 | /* call the architecture-specific part of the destructor */
|
---|
[da1bafb] | 169 | thr_destructor_arch(thread);
|
---|
[a35b458] | 170 |
|
---|
[5df1963] | 171 | frame_free(KA2PA(thread->kstack), STACK_FRAMES);
|
---|
[a35b458] | 172 |
|
---|
[e7c4115d] | 173 | return STACK_FRAMES; /* number of frames freed */
|
---|
[266294a9] | 174 | }
|
---|
[70527f1] | 175 |
|
---|
| 176 | /** Initialize threads
|
---|
| 177 | *
|
---|
| 178 | * Initialize kernel threads support.
|
---|
| 179 | *
|
---|
| 180 | */
|
---|
[f761f1eb] | 181 | void thread_init(void)
|
---|
| 182 | {
|
---|
[43114c5] | 183 | THREAD = NULL;
|
---|
[a35b458] | 184 |
|
---|
[e3306d04] | 185 | atomic_store(&nrdy, 0);
|
---|
[0366d09d] | 186 | thread_cache = slab_cache_create("thread_t", sizeof(thread_t), _Alignof(thread_t),
|
---|
[6f4495f5] | 187 | thr_constructor, thr_destructor, 0);
|
---|
[a35b458] | 188 |
|
---|
[ef1eab7] | 189 | odict_initialize(&threads, threads_getkey, threads_cmp);
|
---|
[016acbe] | 190 | }
|
---|
[70527f1] | 191 |
|
---|
[6eef3c4] | 192 | /** Wire thread to the given CPU
|
---|
| 193 | *
|
---|
| 194 | * @param cpu CPU to wire the thread to.
|
---|
| 195 | *
|
---|
| 196 | */
|
---|
| 197 | void thread_wire(thread_t *thread, cpu_t *cpu)
|
---|
| 198 | {
|
---|
| 199 | irq_spinlock_lock(&thread->lock, true);
|
---|
[efed95a3] | 200 | atomic_set_unordered(&thread->cpu, cpu);
|
---|
[dd218ea] | 201 | thread->nomigrate++;
|
---|
[6eef3c4] | 202 | irq_spinlock_unlock(&thread->lock, true);
|
---|
| 203 | }
|
---|
| 204 |
|
---|
[0f4f1b2] | 205 | /** Start a thread that wasn't started yet since it was created.
|
---|
| 206 | *
|
---|
| 207 | * @param thread A reference to the newly created thread.
|
---|
| 208 | */
|
---|
| 209 | void thread_start(thread_t *thread)
|
---|
| 210 | {
|
---|
| 211 | assert(thread->state == Entering);
|
---|
[286da52] | 212 | thread_requeue_sleeping(thread_ref(thread));
|
---|
[f761f1eb] | 213 | }
|
---|
| 214 |
|
---|
[70527f1] | 215 | /** Create new thread
|
---|
| 216 | *
|
---|
| 217 | * Create a new thread.
|
---|
| 218 | *
|
---|
[da1bafb] | 219 | * @param func Thread's implementing function.
|
---|
| 220 | * @param arg Thread's implementing function argument.
|
---|
| 221 | * @param task Task to which the thread belongs. The caller must
|
---|
| 222 | * guarantee that the task won't cease to exist during the
|
---|
| 223 | * call. The task's lock may not be held.
|
---|
| 224 | * @param flags Thread flags.
|
---|
| 225 | * @param name Symbolic name (a copy is made).
|
---|
[70527f1] | 226 | *
|
---|
[da1bafb] | 227 | * @return New thread's structure on success, NULL on failure.
|
---|
[70527f1] | 228 | *
|
---|
| 229 | */
|
---|
[3bacee1] | 230 | thread_t *thread_create(void (*func)(void *), void *arg, task_t *task,
|
---|
[6eef3c4] | 231 | thread_flags_t flags, const char *name)
|
---|
[f761f1eb] | 232 | {
|
---|
[abf6c01] | 233 | thread_t *thread = (thread_t *) slab_alloc(thread_cache, FRAME_ATOMIC);
|
---|
[da1bafb] | 234 | if (!thread)
|
---|
[2a46e10] | 235 | return NULL;
|
---|
[a35b458] | 236 |
|
---|
[1871118] | 237 | refcount_init(&thread->refcount);
|
---|
| 238 |
|
---|
[deacd722] | 239 | if (thread_create_arch(thread, flags) != EOK) {
|
---|
| 240 | slab_free(thread_cache, thread);
|
---|
| 241 | return NULL;
|
---|
| 242 | }
|
---|
| 243 |
|
---|
[bb68433] | 244 | /* Not needed, but good for debugging */
|
---|
[26aafe8] | 245 | memsetb(thread->kstack, STACK_SIZE, 0);
|
---|
[a35b458] | 246 |
|
---|
[da1bafb] | 247 | irq_spinlock_lock(&tidlock, true);
|
---|
| 248 | thread->tid = ++last_tid;
|
---|
| 249 | irq_spinlock_unlock(&tidlock, true);
|
---|
[a35b458] | 250 |
|
---|
[6a0e568] | 251 | context_create(&thread->saved_context, thread_main_func,
|
---|
| 252 | thread->kstack, STACK_SIZE);
|
---|
[a35b458] | 253 |
|
---|
[a6e55886] | 254 | current_initialize((current_t *) thread->kstack);
|
---|
[a35b458] | 255 |
|
---|
[da1bafb] | 256 | str_cpy(thread->name, THREAD_NAME_BUFLEN, name);
|
---|
[a35b458] | 257 |
|
---|
[da1bafb] | 258 | thread->thread_code = func;
|
---|
| 259 | thread->thread_arg = arg;
|
---|
| 260 | thread->ucycles = 0;
|
---|
| 261 | thread->kcycles = 0;
|
---|
[6eef3c4] | 262 | thread->uncounted =
|
---|
| 263 | ((flags & THREAD_FLAG_UNCOUNTED) == THREAD_FLAG_UNCOUNTED);
|
---|
[da1bafb] | 264 | thread->priority = -1; /* Start in rq[0] */
|
---|
[efed95a3] | 265 | atomic_init(&thread->cpu, NULL);
|
---|
[6eef3c4] | 266 | thread->stolen = false;
|
---|
| 267 | thread->uspace =
|
---|
| 268 | ((flags & THREAD_FLAG_USPACE) == THREAD_FLAG_USPACE);
|
---|
[a35b458] | 269 |
|
---|
[43ac0cc] | 270 | thread->nomigrate = 0;
|
---|
[da1bafb] | 271 | thread->state = Entering;
|
---|
[a35b458] | 272 |
|
---|
[111b9b9] | 273 | atomic_init(&thread->sleep_queue, NULL);
|
---|
[a35b458] | 274 |
|
---|
[da1bafb] | 275 | thread->in_copy_from_uspace = false;
|
---|
| 276 | thread->in_copy_to_uspace = false;
|
---|
[a35b458] | 277 |
|
---|
[da1bafb] | 278 | thread->interrupted = false;
|
---|
[111b9b9] | 279 | atomic_init(&thread->sleep_state, SLEEP_INITIAL);
|
---|
| 280 |
|
---|
[da1bafb] | 281 | waitq_initialize(&thread->join_wq);
|
---|
[a35b458] | 282 |
|
---|
[da1bafb] | 283 | thread->task = task;
|
---|
[a35b458] | 284 |
|
---|
[6eef3c4] | 285 | thread->fpu_context_exists = false;
|
---|
[a35b458] | 286 |
|
---|
[ef1eab7] | 287 | odlink_initialize(&thread->lthreads);
|
---|
[a35b458] | 288 |
|
---|
[9a1b20c] | 289 | #ifdef CONFIG_UDEBUG
|
---|
[5b7a107] | 290 | /* Initialize debugging stuff */
|
---|
| 291 | thread->btrace = false;
|
---|
[da1bafb] | 292 | udebug_thread_initialize(&thread->udebug);
|
---|
[9a1b20c] | 293 | #endif
|
---|
[a35b458] | 294 |
|
---|
[6eef3c4] | 295 | if ((flags & THREAD_FLAG_NOATTACH) != THREAD_FLAG_NOATTACH)
|
---|
[da1bafb] | 296 | thread_attach(thread, task);
|
---|
[a35b458] | 297 |
|
---|
[da1bafb] | 298 | return thread;
|
---|
[d8431986] | 299 | }
|
---|
| 300 |
|
---|
| 301 | /** Destroy thread memory structure
|
---|
| 302 | *
|
---|
| 303 | * Detach thread from all queues, cpus etc. and destroy it.
|
---|
[da1bafb] | 304 | *
|
---|
[11d2c983] | 305 | * @param obj Thread to be destroyed.
|
---|
[d8431986] | 306 | *
|
---|
| 307 | */
|
---|
[1871118] | 308 | static void thread_destroy(void *obj)
|
---|
[d8431986] | 309 | {
|
---|
[1871118] | 310 | thread_t *thread = (thread_t *) obj;
|
---|
| 311 |
|
---|
[11d2c983] | 312 | assert_link_not_used(&thread->rq_link);
|
---|
| 313 | assert_link_not_used(&thread->wq_link);
|
---|
| 314 |
|
---|
[63e27ef] | 315 | assert(thread->task);
|
---|
[11d2c983] | 316 |
|
---|
| 317 | ipl_t ipl = interrupts_disable();
|
---|
| 318 |
|
---|
| 319 | /* Remove thread from global list. */
|
---|
| 320 | irq_spinlock_lock(&threads_lock, false);
|
---|
| 321 | odict_remove(&thread->lthreads);
|
---|
| 322 | irq_spinlock_unlock(&threads_lock, false);
|
---|
| 323 |
|
---|
[c7326f21] | 324 | /* Remove thread from task's list and accumulate accounting. */
|
---|
| 325 | irq_spinlock_lock(&thread->task->lock, false);
|
---|
| 326 |
|
---|
| 327 | list_remove(&thread->th_link);
|
---|
| 328 |
|
---|
| 329 | /*
|
---|
| 330 | * No other CPU has access to this thread anymore, so we don't need
|
---|
| 331 | * thread->lock for accessing thread's fields after this point.
|
---|
| 332 | */
|
---|
| 333 |
|
---|
| 334 | if (!thread->uncounted) {
|
---|
| 335 | thread->task->ucycles += thread->ucycles;
|
---|
| 336 | thread->task->kcycles += thread->kcycles;
|
---|
| 337 | }
|
---|
| 338 |
|
---|
| 339 | irq_spinlock_unlock(&thread->task->lock, false);
|
---|
[11d2c983] | 340 |
|
---|
| 341 | assert((thread->state == Exiting) || (thread->state == Lingering));
|
---|
[a35b458] | 342 |
|
---|
[c7326f21] | 343 | /* Clear cpu->fpu_owner if set to this thread. */
|
---|
[169815e] | 344 | #ifdef CONFIG_FPU_LAZY
|
---|
[efed95a3] | 345 | cpu_t *cpu = atomic_get_unordered(&thread->cpu);
|
---|
| 346 | if (cpu) {
|
---|
[f3dbe27] | 347 | /*
|
---|
| 348 | * We need to lock for this because the old CPU can concurrently try
|
---|
| 349 | * to dump this thread's FPU state, in which case we need to wait for
|
---|
| 350 | * it to finish. An atomic compare-and-swap wouldn't be enough.
|
---|
| 351 | */
|
---|
[efed95a3] | 352 | irq_spinlock_lock(&cpu->fpu_lock, false);
|
---|
[f3dbe27] | 353 |
|
---|
[efed95a3] | 354 | if (atomic_get_unordered(&cpu->fpu_owner) == thread)
|
---|
| 355 | atomic_set_unordered(&cpu->fpu_owner, NULL);
|
---|
[f3dbe27] | 356 |
|
---|
[efed95a3] | 357 | irq_spinlock_unlock(&cpu->fpu_lock, false);
|
---|
[169815e] | 358 | }
|
---|
| 359 | #endif
|
---|
[a35b458] | 360 |
|
---|
[11d2c983] | 361 | interrupts_restore(ipl);
|
---|
[a35b458] | 362 |
|
---|
[ea7890e7] | 363 | /*
|
---|
[7ed8530] | 364 | * Drop the reference to the containing task.
|
---|
[ea7890e7] | 365 | */
|
---|
[da1bafb] | 366 | task_release(thread->task);
|
---|
[11d2c983] | 367 | thread->task = NULL;
|
---|
| 368 |
|
---|
[82d515e9] | 369 | slab_free(thread_cache, thread);
|
---|
[d8431986] | 370 | }
|
---|
| 371 |
|
---|
[1871118] | 372 | void thread_put(thread_t *thread)
|
---|
| 373 | {
|
---|
| 374 | if (refcount_down(&thread->refcount)) {
|
---|
| 375 | thread_destroy(thread);
|
---|
| 376 | }
|
---|
| 377 | }
|
---|
| 378 |
|
---|
[d8431986] | 379 | /** Make the thread visible to the system.
|
---|
| 380 | *
|
---|
| 381 | * Attach the thread structure to the current task and make it visible in the
|
---|
[5dcee525] | 382 | * threads_tree.
|
---|
[d8431986] | 383 | *
|
---|
[da1bafb] | 384 | * @param t Thread to be attached to the task.
|
---|
| 385 | * @param task Task to which the thread is to be attached.
|
---|
| 386 | *
|
---|
[d8431986] | 387 | */
|
---|
[da1bafb] | 388 | void thread_attach(thread_t *thread, task_t *task)
|
---|
[d8431986] | 389 | {
|
---|
[1871118] | 390 | ipl_t ipl = interrupts_disable();
|
---|
| 391 |
|
---|
[d8431986] | 392 | /*
|
---|
[9a1b20c] | 393 | * Attach to the specified task.
|
---|
[d8431986] | 394 | */
|
---|
[1871118] | 395 | irq_spinlock_lock(&task->lock, false);
|
---|
[a35b458] | 396 |
|
---|
[7ed8530] | 397 | /* Hold a reference to the task. */
|
---|
| 398 | task_hold(task);
|
---|
[a35b458] | 399 |
|
---|
[9a1b20c] | 400 | /* Must not count kbox thread into lifecount */
|
---|
[6eef3c4] | 401 | if (thread->uspace)
|
---|
[9a1b20c] | 402 | atomic_inc(&task->lifecount);
|
---|
[a35b458] | 403 |
|
---|
[55b77d9] | 404 | list_append(&thread->th_link, &task->threads);
|
---|
[a35b458] | 405 |
|
---|
[1871118] | 406 | irq_spinlock_unlock(&task->lock, false);
|
---|
[a35b458] | 407 |
|
---|
[bb68433] | 408 | /*
|
---|
[ef1eab7] | 409 | * Register this thread in the system-wide dictionary.
|
---|
[bb68433] | 410 | */
|
---|
[1871118] | 411 | irq_spinlock_lock(&threads_lock, false);
|
---|
[ef1eab7] | 412 | odict_insert(&thread->lthreads, &threads, NULL);
|
---|
[1871118] | 413 | irq_spinlock_unlock(&threads_lock, false);
|
---|
| 414 |
|
---|
| 415 | interrupts_restore(ipl);
|
---|
[f761f1eb] | 416 | }
|
---|
| 417 |
|
---|
[0182a665] | 418 | /** Terminate thread.
|
---|
[70527f1] | 419 | *
|
---|
[da1bafb] | 420 | * End current thread execution and switch it to the exiting state.
|
---|
| 421 | * All pending timeouts are executed.
|
---|
| 422 | *
|
---|
[70527f1] | 423 | */
|
---|
[f761f1eb] | 424 | void thread_exit(void)
|
---|
| 425 | {
|
---|
[6eef3c4] | 426 | if (THREAD->uspace) {
|
---|
[9a1b20c] | 427 | #ifdef CONFIG_UDEBUG
|
---|
| 428 | /* Generate udebug THREAD_E event */
|
---|
| 429 | udebug_thread_e_event();
|
---|
[a35b458] | 430 |
|
---|
[0ac99db] | 431 | /*
|
---|
| 432 | * This thread will not execute any code or system calls from
|
---|
| 433 | * now on.
|
---|
| 434 | */
|
---|
| 435 | udebug_stoppable_begin();
|
---|
[9a1b20c] | 436 | #endif
|
---|
| 437 | if (atomic_predec(&TASK->lifecount) == 0) {
|
---|
| 438 | /*
|
---|
| 439 | * We are the last userspace thread in the task that
|
---|
| 440 | * still has not exited. With the exception of the
|
---|
| 441 | * moment the task was created, new userspace threads
|
---|
| 442 | * can only be created by threads of the same task.
|
---|
| 443 | * We are safe to perform cleanup.
|
---|
[da1bafb] | 444 | *
|
---|
[9a1b20c] | 445 | */
|
---|
[ea7890e7] | 446 | ipc_cleanup();
|
---|
[d314571] | 447 | sys_waitq_task_cleanup();
|
---|
[3bacee1] | 448 | LOG("Cleanup of task %" PRIu64 " completed.", TASK->taskid);
|
---|
[ea7890e7] | 449 | }
|
---|
| 450 | }
|
---|
[a35b458] | 451 |
|
---|
[151c050] | 452 | scheduler_enter(Exiting);
|
---|
| 453 | unreachable();
|
---|
[f761f1eb] | 454 | }
|
---|
| 455 |
|
---|
[518dd43] | 456 | /** Interrupts an existing thread so that it may exit as soon as possible.
|
---|
[1b20da0] | 457 | *
|
---|
| 458 | * Threads that are blocked waiting for a synchronization primitive
|
---|
[897fd8f1] | 459 | * are woken up with a return code of EINTR if the
|
---|
[518dd43] | 460 | * blocking call was interruptable. See waitq_sleep_timeout().
|
---|
[1b20da0] | 461 | *
|
---|
[518dd43] | 462 | * Interrupted threads automatically exit when returning back to user space.
|
---|
[1b20da0] | 463 | *
|
---|
[1871118] | 464 | * @param thread A valid thread object.
|
---|
[518dd43] | 465 | */
|
---|
[111b9b9] | 466 | void thread_interrupt(thread_t *thread)
|
---|
[518dd43] | 467 | {
|
---|
[63e27ef] | 468 | assert(thread != NULL);
|
---|
[111b9b9] | 469 | thread->interrupted = true;
|
---|
| 470 | thread_wakeup(thread);
|
---|
| 471 | }
|
---|
[a35b458] | 472 |
|
---|
[111b9b9] | 473 | /** Prepare for putting the thread to sleep.
|
---|
| 474 | *
|
---|
| 475 | * @returns whether the thread is currently terminating. If THREAD_OK
|
---|
| 476 | * is returned, the thread is guaranteed to be woken up instantly if the thread
|
---|
| 477 | * is terminated at any time between this function's return and
|
---|
| 478 | * thread_wait_finish(). If THREAD_TERMINATING is returned, the thread can still
|
---|
| 479 | * go to sleep, but doing so will delay termination.
|
---|
| 480 | */
|
---|
| 481 | thread_termination_state_t thread_wait_start(void)
|
---|
| 482 | {
|
---|
| 483 | assert(THREAD != NULL);
|
---|
[a35b458] | 484 |
|
---|
[111b9b9] | 485 | /*
|
---|
| 486 | * This is an exchange rather than a store so that we can use the acquire
|
---|
| 487 | * semantics, which is needed to ensure that code after this operation sees
|
---|
| 488 | * memory ops made before thread_wakeup() in other thread, if that wakeup
|
---|
| 489 | * was reset by this operation.
|
---|
| 490 | *
|
---|
| 491 | * In particular, we need this to ensure we can't miss the thread being
|
---|
| 492 | * terminated concurrently with a synchronization primitive preparing to
|
---|
| 493 | * sleep.
|
---|
| 494 | */
|
---|
| 495 | (void) atomic_exchange_explicit(&THREAD->sleep_state, SLEEP_INITIAL,
|
---|
| 496 | memory_order_acquire);
|
---|
[a35b458] | 497 |
|
---|
[111b9b9] | 498 | return THREAD->interrupted ? THREAD_TERMINATING : THREAD_OK;
|
---|
| 499 | }
|
---|
[a35b458] | 500 |
|
---|
[111b9b9] | 501 | static void thread_wait_timeout_callback(void *arg)
|
---|
| 502 | {
|
---|
| 503 | thread_wakeup(arg);
|
---|
| 504 | }
|
---|
| 505 |
|
---|
| 506 | /**
|
---|
| 507 | * Suspends this thread's execution until thread_wakeup() is called on it,
|
---|
| 508 | * or deadline is reached.
|
---|
| 509 | *
|
---|
| 510 | * The way this would normally be used is that the current thread call
|
---|
| 511 | * thread_wait_start(), and if interruption has not been signaled, stores
|
---|
| 512 | * a reference to itself in a synchronized structure (such as waitq).
|
---|
| 513 | * After that, it releases any spinlocks it might hold and calls this function.
|
---|
| 514 | *
|
---|
| 515 | * The thread doing the wakeup will acquire the thread's reference from said
|
---|
| 516 | * synchronized structure and calls thread_wakeup() on it.
|
---|
| 517 | *
|
---|
| 518 | * Notably, there can be more than one thread performing wakeup.
|
---|
| 519 | * The number of performed calls to thread_wakeup(), or their relative
|
---|
| 520 | * ordering with thread_wait_finish(), does not matter. However, calls to
|
---|
| 521 | * thread_wakeup() are expected to be synchronized with thread_wait_start()
|
---|
| 522 | * with which they are associated, otherwise wakeups may be missed.
|
---|
| 523 | * However, the operation of thread_wakeup() is defined at any time,
|
---|
| 524 | * synchronization notwithstanding (in the sense of C un/defined behavior),
|
---|
| 525 | * and is in fact used to interrupt waiting threads by external events.
|
---|
| 526 | * The waiting thread must operate correctly in face of spurious wakeups,
|
---|
| 527 | * and clean up its reference in the synchronization structure if necessary.
|
---|
| 528 | *
|
---|
| 529 | * Returns THREAD_WAIT_TIMEOUT if timeout fired, which is a necessary condition
|
---|
| 530 | * for it to have been waken up by the timeout, but the caller must assume
|
---|
| 531 | * that proper wakeups, timeouts and interrupts may occur concurrently, so
|
---|
| 532 | * the fact timeout has been registered does not necessarily mean the thread
|
---|
| 533 | * has not been woken up or interrupted.
|
---|
| 534 | */
|
---|
| 535 | thread_wait_result_t thread_wait_finish(deadline_t deadline)
|
---|
| 536 | {
|
---|
| 537 | assert(THREAD != NULL);
|
---|
| 538 |
|
---|
| 539 | timeout_t timeout;
|
---|
| 540 |
|
---|
[5663872] | 541 | /* Extra check to avoid going to scheduler if we don't need to. */
|
---|
| 542 | if (atomic_load_explicit(&THREAD->sleep_state, memory_order_acquire) !=
|
---|
| 543 | SLEEP_INITIAL)
|
---|
| 544 | return THREAD_WAIT_SUCCESS;
|
---|
[111b9b9] | 545 |
|
---|
[5663872] | 546 | if (deadline != DEADLINE_NEVER) {
|
---|
[111b9b9] | 547 | timeout_initialize(&timeout);
|
---|
| 548 | timeout_register_deadline(&timeout, deadline,
|
---|
| 549 | thread_wait_timeout_callback, THREAD);
|
---|
| 550 | }
|
---|
| 551 |
|
---|
[151c050] | 552 | scheduler_enter(Sleeping);
|
---|
[111b9b9] | 553 |
|
---|
| 554 | if (deadline != DEADLINE_NEVER && !timeout_unregister(&timeout)) {
|
---|
| 555 | return THREAD_WAIT_TIMEOUT;
|
---|
| 556 | } else {
|
---|
| 557 | return THREAD_WAIT_SUCCESS;
|
---|
| 558 | }
|
---|
| 559 | }
|
---|
| 560 |
|
---|
| 561 | void thread_wakeup(thread_t *thread)
|
---|
| 562 | {
|
---|
| 563 | assert(thread != NULL);
|
---|
| 564 |
|
---|
| 565 | int state = atomic_exchange_explicit(&thread->sleep_state, SLEEP_WOKE,
|
---|
[5663872] | 566 | memory_order_acq_rel);
|
---|
[111b9b9] | 567 |
|
---|
| 568 | if (state == SLEEP_ASLEEP) {
|
---|
| 569 | /*
|
---|
| 570 | * Only one thread gets to do this.
|
---|
| 571 | * The reference consumed here is the reference implicitly passed to
|
---|
| 572 | * the waking thread by the sleeper in thread_wait_finish().
|
---|
| 573 | */
|
---|
[286da52] | 574 | thread_requeue_sleeping(thread);
|
---|
[111b9b9] | 575 | }
|
---|
[518dd43] | 576 | }
|
---|
| 577 |
|
---|
[43ac0cc] | 578 | /** Prevent the current thread from being migrated to another processor. */
|
---|
| 579 | void thread_migration_disable(void)
|
---|
| 580 | {
|
---|
[63e27ef] | 581 | assert(THREAD);
|
---|
[a35b458] | 582 |
|
---|
[43ac0cc] | 583 | THREAD->nomigrate++;
|
---|
| 584 | }
|
---|
| 585 |
|
---|
| 586 | /** Allow the current thread to be migrated to another processor. */
|
---|
| 587 | void thread_migration_enable(void)
|
---|
| 588 | {
|
---|
[63e27ef] | 589 | assert(THREAD);
|
---|
| 590 | assert(THREAD->nomigrate > 0);
|
---|
[a35b458] | 591 |
|
---|
[6eef3c4] | 592 | if (THREAD->nomigrate > 0)
|
---|
| 593 | THREAD->nomigrate--;
|
---|
[43ac0cc] | 594 | }
|
---|
| 595 |
|
---|
[70527f1] | 596 | /** Thread sleep
|
---|
| 597 | *
|
---|
| 598 | * Suspend execution of the current thread.
|
---|
| 599 | *
|
---|
| 600 | * @param sec Number of seconds to sleep.
|
---|
| 601 | *
|
---|
| 602 | */
|
---|
[7f1c620] | 603 | void thread_sleep(uint32_t sec)
|
---|
[f761f1eb] | 604 | {
|
---|
[7c3fb9b] | 605 | /*
|
---|
| 606 | * Sleep in 1000 second steps to support
|
---|
| 607 | * full argument range
|
---|
| 608 | */
|
---|
[22e6802] | 609 | while (sec > 0) {
|
---|
| 610 | uint32_t period = (sec > 1000) ? 1000 : sec;
|
---|
[a35b458] | 611 |
|
---|
[22e6802] | 612 | thread_usleep(period * 1000000);
|
---|
| 613 | sec -= period;
|
---|
| 614 | }
|
---|
[f761f1eb] | 615 | }
|
---|
[70527f1] | 616 |
|
---|
[5110d0a] | 617 | errno_t thread_join(thread_t *thread)
|
---|
| 618 | {
|
---|
| 619 | return thread_join_timeout(thread, SYNCH_NO_TIMEOUT, SYNCH_FLAGS_NONE);
|
---|
| 620 | }
|
---|
| 621 |
|
---|
[fe19611] | 622 | /** Wait for another thread to exit.
|
---|
[a064d4f] | 623 | * After successful wait, the thread reference is destroyed.
|
---|
[fe19611] | 624 | *
|
---|
[da1bafb] | 625 | * @param thread Thread to join on exit.
|
---|
| 626 | * @param usec Timeout in microseconds.
|
---|
| 627 | * @param flags Mode of operation.
|
---|
[fe19611] | 628 | *
|
---|
| 629 | * @return An error code from errno.h or an error code from synch.h.
|
---|
[da1bafb] | 630 | *
|
---|
[fe19611] | 631 | */
|
---|
[b7fd2a0] | 632 | errno_t thread_join_timeout(thread_t *thread, uint32_t usec, unsigned int flags)
|
---|
[fe19611] | 633 | {
|
---|
[0f4f1b2] | 634 | assert(thread != NULL);
|
---|
| 635 |
|
---|
[da1bafb] | 636 | if (thread == THREAD)
|
---|
[fe19611] | 637 | return EINVAL;
|
---|
[a35b458] | 638 |
|
---|
[da1bafb] | 639 | irq_spinlock_lock(&thread->lock, true);
|
---|
[1871118] | 640 | state_t state = thread->state;
|
---|
[da1bafb] | 641 | irq_spinlock_unlock(&thread->lock, true);
|
---|
[a35b458] | 642 |
|
---|
[a064d4f] | 643 | errno_t rc = EOK;
|
---|
| 644 |
|
---|
| 645 | if (state != Exiting)
|
---|
| 646 | rc = _waitq_sleep_timeout(&thread->join_wq, usec, flags);
|
---|
| 647 |
|
---|
| 648 | if (rc == EOK)
|
---|
| 649 | thread_put(thread);
|
---|
| 650 |
|
---|
| 651 | return rc;
|
---|
[fe19611] | 652 | }
|
---|
| 653 |
|
---|
[0f4f1b2] | 654 | void thread_detach(thread_t *thread)
|
---|
| 655 | {
|
---|
| 656 | thread_put(thread);
|
---|
| 657 | }
|
---|
| 658 |
|
---|
[70527f1] | 659 | /** Thread usleep
|
---|
| 660 | *
|
---|
| 661 | * Suspend execution of the current thread.
|
---|
| 662 | *
|
---|
| 663 | * @param usec Number of microseconds to sleep.
|
---|
| 664 | *
|
---|
[1b20da0] | 665 | */
|
---|
[7f1c620] | 666 | void thread_usleep(uint32_t usec)
|
---|
[f761f1eb] | 667 | {
|
---|
| 668 | waitq_t wq;
|
---|
[a35b458] | 669 |
|
---|
[f761f1eb] | 670 | waitq_initialize(&wq);
|
---|
[a35b458] | 671 |
|
---|
[111b9b9] | 672 | (void) waitq_sleep_timeout(&wq, usec);
|
---|
[151c050] | 673 | }
|
---|
| 674 |
|
---|
| 675 | /** Allow other threads to run. */
|
---|
| 676 | void thread_yield(void)
|
---|
| 677 | {
|
---|
| 678 | assert(THREAD != NULL);
|
---|
| 679 | scheduler_enter(Running);
|
---|
[f761f1eb] | 680 | }
|
---|
| 681 |
|
---|
[ef1eab7] | 682 | static void thread_print(thread_t *thread, bool additional)
|
---|
[5dcee525] | 683 | {
|
---|
[1ba37fa] | 684 | uint64_t ucycles, kcycles;
|
---|
| 685 | char usuffix, ksuffix;
|
---|
[da1bafb] | 686 | order_suffix(thread->ucycles, &ucycles, &usuffix);
|
---|
| 687 | order_suffix(thread->kcycles, &kcycles, &ksuffix);
|
---|
[a35b458] | 688 |
|
---|
[577f042a] | 689 | char *name;
|
---|
| 690 | if (str_cmp(thread->name, "uinit") == 0)
|
---|
| 691 | name = thread->task->name;
|
---|
| 692 | else
|
---|
| 693 | name = thread->name;
|
---|
[a35b458] | 694 |
|
---|
[ef1eab7] | 695 | if (additional)
|
---|
[c1b073b7] | 696 | printf("%-8" PRIu64 " %p %p %9" PRIu64 "%c %9" PRIu64 "%c ",
|
---|
[577f042a] | 697 | thread->tid, thread->thread_code, thread->kstack,
|
---|
| 698 | ucycles, usuffix, kcycles, ksuffix);
|
---|
[48dcc69] | 699 | else
|
---|
[c1b073b7] | 700 | printf("%-8" PRIu64 " %-14s %p %-8s %p %-5" PRIu32 "\n",
|
---|
[577f042a] | 701 | thread->tid, name, thread, thread_states[thread->state],
|
---|
[26aafe8] | 702 | thread->task, thread->task->container);
|
---|
[a35b458] | 703 |
|
---|
[ef1eab7] | 704 | if (additional) {
|
---|
[efed95a3] | 705 | cpu_t *cpu = atomic_get_unordered(&thread->cpu);
|
---|
| 706 | if (cpu)
|
---|
| 707 | printf("%-5u", cpu->id);
|
---|
[48dcc69] | 708 | else
|
---|
| 709 | printf("none ");
|
---|
[a35b458] | 710 |
|
---|
[48dcc69] | 711 | if (thread->state == Sleeping) {
|
---|
[c1b073b7] | 712 | printf(" %p", thread->sleep_queue);
|
---|
[48dcc69] | 713 | }
|
---|
[a35b458] | 714 |
|
---|
[48dcc69] | 715 | printf("\n");
|
---|
[43b1e86] | 716 | }
|
---|
[5dcee525] | 717 | }
|
---|
| 718 |
|
---|
[da1bafb] | 719 | /** Print list of threads debug info
|
---|
[48dcc69] | 720 | *
|
---|
| 721 | * @param additional Print additional information.
|
---|
[da1bafb] | 722 | *
|
---|
| 723 | */
|
---|
[48dcc69] | 724 | void thread_print_list(bool additional)
|
---|
[55ab0f1] | 725 | {
|
---|
[ef1eab7] | 726 | thread_t *thread;
|
---|
| 727 |
|
---|
[1871118] | 728 | /* Accessing system-wide threads list through thread_first()/thread_next(). */
|
---|
[da1bafb] | 729 | irq_spinlock_lock(&threads_lock, true);
|
---|
[a35b458] | 730 |
|
---|
[c1b073b7] | 731 | if (sizeof(void *) <= 4) {
|
---|
| 732 | if (additional)
|
---|
| 733 | printf("[id ] [code ] [stack ] [ucycles ] [kcycles ]"
|
---|
| 734 | " [cpu] [waitqueue]\n");
|
---|
| 735 | else
|
---|
| 736 | printf("[id ] [name ] [address ] [state ] [task ]"
|
---|
| 737 | " [ctn]\n");
|
---|
| 738 | } else {
|
---|
| 739 | if (additional) {
|
---|
| 740 | printf("[id ] [code ] [stack ] [ucycles ] [kcycles ]"
|
---|
| 741 | " [cpu] [waitqueue ]\n");
|
---|
| 742 | } else
|
---|
| 743 | printf("[id ] [name ] [address ] [state ]"
|
---|
| 744 | " [task ] [ctn]\n");
|
---|
| 745 | }
|
---|
[a35b458] | 746 |
|
---|
[aab5e46] | 747 | thread = thread_first();
|
---|
| 748 | while (thread != NULL) {
|
---|
[ef1eab7] | 749 | thread_print(thread, additional);
|
---|
[aab5e46] | 750 | thread = thread_next(thread);
|
---|
[ef1eab7] | 751 | }
|
---|
[a35b458] | 752 |
|
---|
[da1bafb] | 753 | irq_spinlock_unlock(&threads_lock, true);
|
---|
[55ab0f1] | 754 | }
|
---|
[9f52563] | 755 |
|
---|
[1871118] | 756 | static bool thread_exists(thread_t *thread)
|
---|
[016acbe] | 757 | {
|
---|
[ef1eab7] | 758 | odlink_t *odlink = odict_find_eq(&threads, thread, NULL);
|
---|
| 759 | return odlink != NULL;
|
---|
[016acbe] | 760 | }
|
---|
| 761 |
|
---|
[1871118] | 762 | /** Check whether the thread exists, and if so, return a reference to it.
|
---|
| 763 | */
|
---|
| 764 | thread_t *thread_try_get(thread_t *thread)
|
---|
| 765 | {
|
---|
| 766 | irq_spinlock_lock(&threads_lock, true);
|
---|
| 767 |
|
---|
| 768 | if (thread_exists(thread)) {
|
---|
| 769 | /* Try to strengthen the reference. */
|
---|
| 770 | thread = thread_try_ref(thread);
|
---|
| 771 | } else {
|
---|
| 772 | thread = NULL;
|
---|
| 773 | }
|
---|
| 774 |
|
---|
| 775 | irq_spinlock_unlock(&threads_lock, true);
|
---|
| 776 |
|
---|
| 777 | return thread;
|
---|
| 778 | }
|
---|
| 779 |
|
---|
[cce6acf] | 780 | /** Update accounting of current thread.
|
---|
| 781 | *
|
---|
| 782 | * Note that thread_lock on THREAD must be already held and
|
---|
| 783 | * interrupts must be already disabled.
|
---|
| 784 | *
|
---|
[da1bafb] | 785 | * @param user True to update user accounting, false for kernel.
|
---|
| 786 | *
|
---|
[cce6acf] | 787 | */
|
---|
[a2a00e8] | 788 | void thread_update_accounting(bool user)
|
---|
[cce6acf] | 789 | {
|
---|
| 790 | uint64_t time = get_cycle();
|
---|
[1d432f9] | 791 |
|
---|
[63e27ef] | 792 | assert(interrupts_disabled());
|
---|
| 793 | assert(irq_spinlock_locked(&THREAD->lock));
|
---|
[a35b458] | 794 |
|
---|
[da1bafb] | 795 | if (user)
|
---|
[a2a00e8] | 796 | THREAD->ucycles += time - THREAD->last_cycle;
|
---|
[da1bafb] | 797 | else
|
---|
[a2a00e8] | 798 | THREAD->kcycles += time - THREAD->last_cycle;
|
---|
[a35b458] | 799 |
|
---|
[cce6acf] | 800 | THREAD->last_cycle = time;
|
---|
| 801 | }
|
---|
| 802 |
|
---|
[e1b6742] | 803 | /** Find thread structure corresponding to thread ID.
|
---|
| 804 | *
|
---|
| 805 | * The threads_lock must be already held by the caller of this function and
|
---|
| 806 | * interrupts must be disabled.
|
---|
| 807 | *
|
---|
[1871118] | 808 | * The returned reference is weak.
|
---|
| 809 | * If the caller needs to keep it, thread_try_ref() must be used to upgrade
|
---|
| 810 | * to a strong reference _before_ threads_lock is released.
|
---|
| 811 | *
|
---|
[e1b6742] | 812 | * @param id Thread ID.
|
---|
| 813 | *
|
---|
| 814 | * @return Thread structure address or NULL if there is no such thread ID.
|
---|
| 815 | *
|
---|
| 816 | */
|
---|
| 817 | thread_t *thread_find_by_id(thread_id_t thread_id)
|
---|
| 818 | {
|
---|
[ef1eab7] | 819 | thread_t *thread;
|
---|
| 820 |
|
---|
[63e27ef] | 821 | assert(interrupts_disabled());
|
---|
| 822 | assert(irq_spinlock_locked(&threads_lock));
|
---|
[a35b458] | 823 |
|
---|
[aab5e46] | 824 | thread = thread_first();
|
---|
| 825 | while (thread != NULL) {
|
---|
[ef1eab7] | 826 | if (thread->tid == thread_id)
|
---|
| 827 | return thread;
|
---|
[a35b458] | 828 |
|
---|
[aab5e46] | 829 | thread = thread_next(thread);
|
---|
[ef1eab7] | 830 | }
|
---|
[a35b458] | 831 |
|
---|
[ef1eab7] | 832 | return NULL;
|
---|
[e1b6742] | 833 | }
|
---|
| 834 |
|
---|
[aab5e46] | 835 | /** Get count of threads.
|
---|
| 836 | *
|
---|
| 837 | * @return Number of threads in the system
|
---|
| 838 | */
|
---|
| 839 | size_t thread_count(void)
|
---|
| 840 | {
|
---|
| 841 | assert(interrupts_disabled());
|
---|
| 842 | assert(irq_spinlock_locked(&threads_lock));
|
---|
| 843 |
|
---|
| 844 | return odict_count(&threads);
|
---|
| 845 | }
|
---|
| 846 |
|
---|
| 847 | /** Get first thread.
|
---|
| 848 | *
|
---|
| 849 | * @return Pointer to first thread or @c NULL if there are none.
|
---|
| 850 | */
|
---|
| 851 | thread_t *thread_first(void)
|
---|
| 852 | {
|
---|
| 853 | odlink_t *odlink;
|
---|
| 854 |
|
---|
| 855 | assert(interrupts_disabled());
|
---|
| 856 | assert(irq_spinlock_locked(&threads_lock));
|
---|
| 857 |
|
---|
| 858 | odlink = odict_first(&threads);
|
---|
| 859 | if (odlink == NULL)
|
---|
| 860 | return NULL;
|
---|
| 861 |
|
---|
| 862 | return odict_get_instance(odlink, thread_t, lthreads);
|
---|
| 863 | }
|
---|
| 864 |
|
---|
| 865 | /** Get next thread.
|
---|
| 866 | *
|
---|
| 867 | * @param cur Current thread
|
---|
| 868 | * @return Pointer to next thread or @c NULL if there are no more threads.
|
---|
| 869 | */
|
---|
| 870 | thread_t *thread_next(thread_t *cur)
|
---|
| 871 | {
|
---|
| 872 | odlink_t *odlink;
|
---|
| 873 |
|
---|
| 874 | assert(interrupts_disabled());
|
---|
| 875 | assert(irq_spinlock_locked(&threads_lock));
|
---|
| 876 |
|
---|
| 877 | odlink = odict_next(&cur->lthreads, &threads);
|
---|
| 878 | if (odlink == NULL)
|
---|
| 879 | return NULL;
|
---|
| 880 |
|
---|
| 881 | return odict_get_instance(odlink, thread_t, lthreads);
|
---|
| 882 | }
|
---|
| 883 |
|
---|
[5b7a107] | 884 | #ifdef CONFIG_UDEBUG
|
---|
| 885 |
|
---|
[df58e44] | 886 | void thread_stack_trace(thread_id_t thread_id)
|
---|
| 887 | {
|
---|
| 888 | irq_spinlock_lock(&threads_lock, true);
|
---|
[1871118] | 889 | thread_t *thread = thread_try_ref(thread_find_by_id(thread_id));
|
---|
| 890 | irq_spinlock_unlock(&threads_lock, true);
|
---|
[a35b458] | 891 |
|
---|
[df58e44] | 892 | if (thread == NULL) {
|
---|
| 893 | printf("No such thread.\n");
|
---|
| 894 | return;
|
---|
| 895 | }
|
---|
[a35b458] | 896 |
|
---|
[df58e44] | 897 | /*
|
---|
| 898 | * Schedule a stack trace to be printed
|
---|
| 899 | * just before the thread is scheduled next.
|
---|
| 900 | *
|
---|
| 901 | * If the thread is sleeping then try to interrupt
|
---|
| 902 | * the sleep. Any request for printing an uspace stack
|
---|
| 903 | * trace from within the kernel should be always
|
---|
| 904 | * considered a last resort debugging means, therefore
|
---|
| 905 | * forcing the thread's sleep to be interrupted
|
---|
| 906 | * is probably justifiable.
|
---|
| 907 | */
|
---|
[a35b458] | 908 |
|
---|
[1871118] | 909 | irq_spinlock_lock(&thread->lock, true);
|
---|
| 910 |
|
---|
[df58e44] | 911 | bool sleeping = false;
|
---|
| 912 | istate_t *istate = thread->udebug.uspace_state;
|
---|
| 913 | if (istate != NULL) {
|
---|
| 914 | printf("Scheduling thread stack trace.\n");
|
---|
| 915 | thread->btrace = true;
|
---|
| 916 | if (thread->state == Sleeping)
|
---|
| 917 | sleeping = true;
|
---|
| 918 | } else
|
---|
| 919 | printf("Thread interrupt state not available.\n");
|
---|
[a35b458] | 920 |
|
---|
[1871118] | 921 | irq_spinlock_unlock(&thread->lock, true);
|
---|
[a35b458] | 922 |
|
---|
[df58e44] | 923 | if (sleeping)
|
---|
[111b9b9] | 924 | thread_wakeup(thread);
|
---|
[a35b458] | 925 |
|
---|
[1871118] | 926 | thread_put(thread);
|
---|
[df58e44] | 927 | }
|
---|
[e1b6742] | 928 |
|
---|
[5b7a107] | 929 | #endif /* CONFIG_UDEBUG */
|
---|
[e1b6742] | 930 |
|
---|
[ef1eab7] | 931 | /** Get key function for the @c threads ordered dictionary.
|
---|
| 932 | *
|
---|
| 933 | * @param odlink Link
|
---|
| 934 | * @return Pointer to thread structure cast as 'void *'
|
---|
| 935 | */
|
---|
| 936 | static void *threads_getkey(odlink_t *odlink)
|
---|
| 937 | {
|
---|
| 938 | thread_t *thread = odict_get_instance(odlink, thread_t, lthreads);
|
---|
| 939 | return (void *) thread;
|
---|
| 940 | }
|
---|
| 941 |
|
---|
| 942 | /** Key comparison function for the @c threads ordered dictionary.
|
---|
| 943 | *
|
---|
| 944 | * @param a Pointer to thread A
|
---|
| 945 | * @param b Pointer to thread B
|
---|
| 946 | * @return -1, 0, 1 iff pointer to A is less than, equal to, greater than B
|
---|
| 947 | */
|
---|
| 948 | static int threads_cmp(void *a, void *b)
|
---|
| 949 | {
|
---|
| 950 | if (a > b)
|
---|
| 951 | return -1;
|
---|
| 952 | else if (a == b)
|
---|
| 953 | return 0;
|
---|
| 954 | else
|
---|
| 955 | return +1;
|
---|
| 956 | }
|
---|
| 957 |
|
---|
[9f52563] | 958 | /** Process syscall to create new thread.
|
---|
| 959 | *
|
---|
| 960 | */
|
---|
[5a5269d] | 961 | sys_errno_t sys_thread_create(uspace_ptr_uspace_arg_t uspace_uarg, uspace_ptr_char uspace_name,
|
---|
| 962 | size_t name_len, uspace_ptr_thread_id_t uspace_thread_id)
|
---|
[9f52563] | 963 | {
|
---|
[24345a5] | 964 | if (name_len > THREAD_NAME_BUFLEN - 1)
|
---|
[7faabb7] | 965 | name_len = THREAD_NAME_BUFLEN - 1;
|
---|
[a35b458] | 966 |
|
---|
[da1bafb] | 967 | char namebuf[THREAD_NAME_BUFLEN];
|
---|
[b7fd2a0] | 968 | errno_t rc = copy_from_uspace(namebuf, uspace_name, name_len);
|
---|
[a53ed3a] | 969 | if (rc != EOK)
|
---|
[b7fd2a0] | 970 | return (sys_errno_t) rc;
|
---|
[a35b458] | 971 |
|
---|
[b60c582] | 972 | namebuf[name_len] = 0;
|
---|
[a35b458] | 973 |
|
---|
[4680ef5] | 974 | /*
|
---|
| 975 | * In case of failure, kernel_uarg will be deallocated in this function.
|
---|
| 976 | * In case of success, kernel_uarg will be freed in uinit().
|
---|
| 977 | */
|
---|
[da1bafb] | 978 | uspace_arg_t *kernel_uarg =
|
---|
[11b285d] | 979 | (uspace_arg_t *) malloc(sizeof(uspace_arg_t));
|
---|
[7473807] | 980 | if (!kernel_uarg)
|
---|
| 981 | return (sys_errno_t) ENOMEM;
|
---|
[a35b458] | 982 |
|
---|
[e3c762cd] | 983 | rc = copy_from_uspace(kernel_uarg, uspace_uarg, sizeof(uspace_arg_t));
|
---|
[a53ed3a] | 984 | if (rc != EOK) {
|
---|
[e3c762cd] | 985 | free(kernel_uarg);
|
---|
[b7fd2a0] | 986 | return (sys_errno_t) rc;
|
---|
[e3c762cd] | 987 | }
|
---|
[a35b458] | 988 |
|
---|
[da1bafb] | 989 | thread_t *thread = thread_create(uinit, kernel_uarg, TASK,
|
---|
[6eef3c4] | 990 | THREAD_FLAG_USPACE | THREAD_FLAG_NOATTACH, namebuf);
|
---|
[da1bafb] | 991 | if (thread) {
|
---|
[5a5269d] | 992 | if (uspace_thread_id) {
|
---|
[da1bafb] | 993 | rc = copy_to_uspace(uspace_thread_id, &thread->tid,
|
---|
| 994 | sizeof(thread->tid));
|
---|
[a53ed3a] | 995 | if (rc != EOK) {
|
---|
[d8431986] | 996 | /*
|
---|
| 997 | * We have encountered a failure, but the thread
|
---|
| 998 | * has already been created. We need to undo its
|
---|
| 999 | * creation now.
|
---|
| 1000 | */
|
---|
[a35b458] | 1001 |
|
---|
[d8431986] | 1002 | /*
|
---|
[ea7890e7] | 1003 | * The new thread structure is initialized, but
|
---|
| 1004 | * is still not visible to the system.
|
---|
[d8431986] | 1005 | * We can safely deallocate it.
|
---|
| 1006 | */
|
---|
[82d515e9] | 1007 | slab_free(thread_cache, thread);
|
---|
[da1bafb] | 1008 | free(kernel_uarg);
|
---|
[a35b458] | 1009 |
|
---|
[b7fd2a0] | 1010 | return (sys_errno_t) rc;
|
---|
[3bacee1] | 1011 | }
|
---|
[d8431986] | 1012 | }
|
---|
[a35b458] | 1013 |
|
---|
[9a1b20c] | 1014 | #ifdef CONFIG_UDEBUG
|
---|
[13964ef] | 1015 | /*
|
---|
| 1016 | * Generate udebug THREAD_B event and attach the thread.
|
---|
| 1017 | * This must be done atomically (with the debug locks held),
|
---|
| 1018 | * otherwise we would either miss some thread or receive
|
---|
| 1019 | * THREAD_B events for threads that already existed
|
---|
| 1020 | * and could be detected with THREAD_READ before.
|
---|
| 1021 | */
|
---|
[da1bafb] | 1022 | udebug_thread_b_event_attach(thread, TASK);
|
---|
[13964ef] | 1023 | #else
|
---|
[da1bafb] | 1024 | thread_attach(thread, TASK);
|
---|
[9a1b20c] | 1025 | #endif
|
---|
[286da52] | 1026 | thread_start(thread);
|
---|
| 1027 | thread_put(thread);
|
---|
[a35b458] | 1028 |
|
---|
[d8431986] | 1029 | return 0;
|
---|
[201abde] | 1030 | } else
|
---|
[0f250f9] | 1031 | free(kernel_uarg);
|
---|
[a35b458] | 1032 |
|
---|
[b7fd2a0] | 1033 | return (sys_errno_t) ENOMEM;
|
---|
[9f52563] | 1034 | }
|
---|
| 1035 |
|
---|
| 1036 | /** Process syscall to terminate thread.
|
---|
| 1037 | *
|
---|
| 1038 | */
|
---|
[b7fd2a0] | 1039 | sys_errno_t sys_thread_exit(int uspace_status)
|
---|
[9f52563] | 1040 | {
|
---|
[68091bd] | 1041 | thread_exit();
|
---|
[9f52563] | 1042 | }
|
---|
[b45c443] | 1043 |
|
---|
[3ce7f082] | 1044 | /** Syscall for getting TID.
|
---|
| 1045 | *
|
---|
[201abde] | 1046 | * @param uspace_thread_id Userspace address of 8-byte buffer where to store
|
---|
| 1047 | * current thread ID.
|
---|
| 1048 | *
|
---|
| 1049 | * @return 0 on success or an error code from @ref errno.h.
|
---|
[da1bafb] | 1050 | *
|
---|
[b45c443] | 1051 | */
|
---|
[5a5269d] | 1052 | sys_errno_t sys_thread_get_id(uspace_ptr_thread_id_t uspace_thread_id)
|
---|
[3ce7f082] | 1053 | {
|
---|
| 1054 | /*
|
---|
| 1055 | * No need to acquire lock on THREAD because tid
|
---|
| 1056 | * remains constant for the lifespan of the thread.
|
---|
[da1bafb] | 1057 | *
|
---|
[3ce7f082] | 1058 | */
|
---|
[b7fd2a0] | 1059 | return (sys_errno_t) copy_to_uspace(uspace_thread_id, &THREAD->tid,
|
---|
[201abde] | 1060 | sizeof(THREAD->tid));
|
---|
[3ce7f082] | 1061 | }
|
---|
[6f4495f5] | 1062 |
|
---|
[d9ece1cb] | 1063 | /** Syscall wrapper for sleeping. */
|
---|
[b7fd2a0] | 1064 | sys_errno_t sys_thread_usleep(uint32_t usec)
|
---|
[d9ece1cb] | 1065 | {
|
---|
[22e6802] | 1066 | thread_usleep(usec);
|
---|
[d9ece1cb] | 1067 | return 0;
|
---|
| 1068 | }
|
---|
| 1069 |
|
---|
[b7fd2a0] | 1070 | sys_errno_t sys_thread_udelay(uint32_t usec)
|
---|
[7e7b791] | 1071 | {
|
---|
[8d6c1f1] | 1072 | delay(usec);
|
---|
[7e7b791] | 1073 | return 0;
|
---|
| 1074 | }
|
---|
| 1075 |
|
---|
[3ce7f082] | 1076 | /** @}
|
---|
| 1077 | */
|
---|