1 | /*
|
---|
2 | * Copyright (c) 2010 Jakub Jermar
|
---|
3 | * Copyright (c) 2018 Jiri Svoboda
|
---|
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 |
|
---|
30 | /** @addtogroup kernel_generic_proc
|
---|
31 | * @{
|
---|
32 | */
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * @file
|
---|
36 | * @brief Thread management functions.
|
---|
37 | */
|
---|
38 |
|
---|
39 | #include <assert.h>
|
---|
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>
|
---|
46 | #include <arch/cycle.h>
|
---|
47 | #include <arch.h>
|
---|
48 | #include <synch/spinlock.h>
|
---|
49 | #include <synch/waitq.h>
|
---|
50 | #include <cpu.h>
|
---|
51 | #include <str.h>
|
---|
52 | #include <context.h>
|
---|
53 | #include <adt/list.h>
|
---|
54 | #include <adt/odict.h>
|
---|
55 | #include <time/clock.h>
|
---|
56 | #include <time/timeout.h>
|
---|
57 | #include <time/delay.h>
|
---|
58 | #include <config.h>
|
---|
59 | #include <arch/interrupt.h>
|
---|
60 | #include <smp/ipi.h>
|
---|
61 | #include <arch/faddr.h>
|
---|
62 | #include <atomic.h>
|
---|
63 | #include <mem.h>
|
---|
64 | #include <stdio.h>
|
---|
65 | #include <mm/slab.h>
|
---|
66 | #include <main/uinit.h>
|
---|
67 | #include <syscall/copy.h>
|
---|
68 | #include <errno.h>
|
---|
69 | #include <debug.h>
|
---|
70 |
|
---|
71 | /** Thread states */
|
---|
72 | const char *thread_states[] = {
|
---|
73 | "Invalid",
|
---|
74 | "Running",
|
---|
75 | "Sleeping",
|
---|
76 | "Ready",
|
---|
77 | "Entering",
|
---|
78 | "Exiting",
|
---|
79 | "Lingering"
|
---|
80 | };
|
---|
81 |
|
---|
82 | /** Lock protecting the @c threads ordered dictionary .
|
---|
83 | *
|
---|
84 | * For locking rules, see declaration thereof.
|
---|
85 | */
|
---|
86 | IRQ_SPINLOCK_INITIALIZE(threads_lock);
|
---|
87 |
|
---|
88 | /** Ordered dictionary of all threads by their address (i.e. pointer to
|
---|
89 | * the thread_t structure).
|
---|
90 | *
|
---|
91 | * When a thread is found in the @c threads ordered dictionary, it is
|
---|
92 | * guaranteed to exist as long as the @c threads_lock is held.
|
---|
93 | *
|
---|
94 | * Members are of type thread_t.
|
---|
95 | */
|
---|
96 | odict_t threads;
|
---|
97 |
|
---|
98 | IRQ_SPINLOCK_STATIC_INITIALIZE(tidlock);
|
---|
99 | static thread_id_t last_tid = 0;
|
---|
100 |
|
---|
101 | static slab_cache_t *thread_cache;
|
---|
102 |
|
---|
103 | #ifdef CONFIG_FPU
|
---|
104 | slab_cache_t *fpu_context_cache;
|
---|
105 | #endif
|
---|
106 |
|
---|
107 | static void *threads_getkey(odlink_t *);
|
---|
108 | static int threads_cmp(void *, void *);
|
---|
109 |
|
---|
110 | /** Thread wrapper.
|
---|
111 | *
|
---|
112 | * This wrapper is provided to ensure that every thread makes a call to
|
---|
113 | * thread_exit() when its implementing function returns.
|
---|
114 | *
|
---|
115 | * interrupts_disable() is assumed.
|
---|
116 | *
|
---|
117 | */
|
---|
118 | static void cushion(void)
|
---|
119 | {
|
---|
120 | void (*f)(void *) = THREAD->thread_code;
|
---|
121 | void *arg = THREAD->thread_arg;
|
---|
122 | THREAD->last_cycle = get_cycle();
|
---|
123 |
|
---|
124 | /* This is where each thread wakes up after its creation */
|
---|
125 | irq_spinlock_unlock(&THREAD->lock, false);
|
---|
126 | interrupts_enable();
|
---|
127 |
|
---|
128 | f(arg);
|
---|
129 |
|
---|
130 | /* Accumulate accounting to the task */
|
---|
131 | irq_spinlock_lock(&THREAD->lock, true);
|
---|
132 | if (!THREAD->uncounted) {
|
---|
133 | thread_update_accounting(true);
|
---|
134 | uint64_t ucycles = THREAD->ucycles;
|
---|
135 | THREAD->ucycles = 0;
|
---|
136 | uint64_t kcycles = THREAD->kcycles;
|
---|
137 | THREAD->kcycles = 0;
|
---|
138 |
|
---|
139 | irq_spinlock_pass(&THREAD->lock, &TASK->lock);
|
---|
140 | TASK->ucycles += ucycles;
|
---|
141 | TASK->kcycles += kcycles;
|
---|
142 | irq_spinlock_unlock(&TASK->lock, true);
|
---|
143 | } else
|
---|
144 | irq_spinlock_unlock(&THREAD->lock, true);
|
---|
145 |
|
---|
146 | thread_exit();
|
---|
147 |
|
---|
148 | /* Not reached */
|
---|
149 | }
|
---|
150 |
|
---|
151 | /** Initialization and allocation for thread_t structure
|
---|
152 | *
|
---|
153 | */
|
---|
154 | static errno_t thr_constructor(void *obj, unsigned int kmflags)
|
---|
155 | {
|
---|
156 | thread_t *thread = (thread_t *) obj;
|
---|
157 |
|
---|
158 | irq_spinlock_initialize(&thread->lock, "thread_t_lock");
|
---|
159 | link_initialize(&thread->rq_link);
|
---|
160 | link_initialize(&thread->wq_link);
|
---|
161 | link_initialize(&thread->th_link);
|
---|
162 |
|
---|
163 | /* call the architecture-specific part of the constructor */
|
---|
164 | thr_constructor_arch(thread);
|
---|
165 |
|
---|
166 | #ifdef CONFIG_FPU
|
---|
167 | thread->saved_fpu_context = slab_alloc(fpu_context_cache, kmflags);
|
---|
168 | if (!thread->saved_fpu_context)
|
---|
169 | return ENOMEM;
|
---|
170 | #endif /* CONFIG_FPU */
|
---|
171 |
|
---|
172 | /*
|
---|
173 | * Allocate the kernel stack from the low-memory to prevent an infinite
|
---|
174 | * nesting of TLB-misses when accessing the stack from the part of the
|
---|
175 | * TLB-miss handler written in C.
|
---|
176 | *
|
---|
177 | * Note that low-memory is safe to be used for the stack as it will be
|
---|
178 | * covered by the kernel identity mapping, which guarantees not to
|
---|
179 | * nest TLB-misses infinitely (either via some hardware mechanism or
|
---|
180 | * by the construciton of the assembly-language part of the TLB-miss
|
---|
181 | * handler).
|
---|
182 | *
|
---|
183 | * This restriction can be lifted once each architecture provides
|
---|
184 | * a similar guarantee, for example by locking the kernel stack
|
---|
185 | * in the TLB whenever it is allocated from the high-memory and the
|
---|
186 | * thread is being scheduled to run.
|
---|
187 | */
|
---|
188 | kmflags |= FRAME_LOWMEM;
|
---|
189 | kmflags &= ~FRAME_HIGHMEM;
|
---|
190 |
|
---|
191 | // XXX: All kernel stacks must be aligned to STACK_SIZE,
|
---|
192 | // see get_stack_base().
|
---|
193 |
|
---|
194 | uintptr_t stack_phys =
|
---|
195 | frame_alloc(STACK_FRAMES, kmflags, STACK_SIZE - 1);
|
---|
196 | if (!stack_phys) {
|
---|
197 | #ifdef CONFIG_FPU
|
---|
198 | assert(thread->saved_fpu_context);
|
---|
199 | slab_free(fpu_context_cache, thread->saved_fpu_context);
|
---|
200 | #endif
|
---|
201 | return ENOMEM;
|
---|
202 | }
|
---|
203 |
|
---|
204 | thread->kstack = (uint8_t *) PA2KA(stack_phys);
|
---|
205 |
|
---|
206 | #ifdef CONFIG_UDEBUG
|
---|
207 | mutex_initialize(&thread->udebug.lock, MUTEX_PASSIVE);
|
---|
208 | #endif
|
---|
209 |
|
---|
210 | return EOK;
|
---|
211 | }
|
---|
212 |
|
---|
213 | /** Destruction of thread_t object */
|
---|
214 | static size_t thr_destructor(void *obj)
|
---|
215 | {
|
---|
216 | thread_t *thread = (thread_t *) obj;
|
---|
217 |
|
---|
218 | /* call the architecture-specific part of the destructor */
|
---|
219 | thr_destructor_arch(thread);
|
---|
220 |
|
---|
221 | frame_free(KA2PA(thread->kstack), STACK_FRAMES);
|
---|
222 |
|
---|
223 | #ifdef CONFIG_FPU
|
---|
224 | assert(thread->saved_fpu_context);
|
---|
225 | slab_free(fpu_context_cache, thread->saved_fpu_context);
|
---|
226 | #endif
|
---|
227 |
|
---|
228 | return STACK_FRAMES; /* number of frames freed */
|
---|
229 | }
|
---|
230 |
|
---|
231 | /** Initialize threads
|
---|
232 | *
|
---|
233 | * Initialize kernel threads support.
|
---|
234 | *
|
---|
235 | */
|
---|
236 | void thread_init(void)
|
---|
237 | {
|
---|
238 | THREAD = NULL;
|
---|
239 |
|
---|
240 | atomic_store(&nrdy, 0);
|
---|
241 | thread_cache = slab_cache_create("thread_t", sizeof(thread_t), 0,
|
---|
242 | thr_constructor, thr_destructor, 0);
|
---|
243 |
|
---|
244 | #ifdef CONFIG_FPU
|
---|
245 | fpu_context_cache = slab_cache_create("fpu_context_t",
|
---|
246 | sizeof(fpu_context_t), FPU_CONTEXT_ALIGN, NULL, NULL, 0);
|
---|
247 | #endif
|
---|
248 |
|
---|
249 | odict_initialize(&threads, threads_getkey, threads_cmp);
|
---|
250 | }
|
---|
251 |
|
---|
252 | /** Wire thread to the given CPU
|
---|
253 | *
|
---|
254 | * @param cpu CPU to wire the thread to.
|
---|
255 | *
|
---|
256 | */
|
---|
257 | void thread_wire(thread_t *thread, cpu_t *cpu)
|
---|
258 | {
|
---|
259 | irq_spinlock_lock(&thread->lock, true);
|
---|
260 | thread->cpu = cpu;
|
---|
261 | thread->wired = true;
|
---|
262 | irq_spinlock_unlock(&thread->lock, true);
|
---|
263 | }
|
---|
264 |
|
---|
265 | /** Invoked right before thread_ready() readies the thread. thread is locked. */
|
---|
266 | static void before_thread_is_ready(thread_t *thread)
|
---|
267 | {
|
---|
268 | assert(irq_spinlock_locked(&thread->lock));
|
---|
269 | }
|
---|
270 |
|
---|
271 | /** Make thread ready
|
---|
272 | *
|
---|
273 | * Switch thread to the ready state.
|
---|
274 | *
|
---|
275 | * @param thread Thread to make ready.
|
---|
276 | *
|
---|
277 | */
|
---|
278 | void thread_ready(thread_t *thread)
|
---|
279 | {
|
---|
280 | irq_spinlock_lock(&thread->lock, true);
|
---|
281 |
|
---|
282 | assert(thread->state != Ready);
|
---|
283 |
|
---|
284 | before_thread_is_ready(thread);
|
---|
285 |
|
---|
286 | int i = (thread->priority < RQ_COUNT - 1) ?
|
---|
287 | ++thread->priority : thread->priority;
|
---|
288 |
|
---|
289 | cpu_t *cpu;
|
---|
290 | if (thread->wired || thread->nomigrate || thread->fpu_context_engaged) {
|
---|
291 | /* Cannot ready to another CPU */
|
---|
292 | assert(thread->cpu != NULL);
|
---|
293 | cpu = thread->cpu;
|
---|
294 | } else if (thread->stolen) {
|
---|
295 | /* Ready to the stealing CPU */
|
---|
296 | cpu = CPU;
|
---|
297 | } else if (thread->cpu) {
|
---|
298 | /* Prefer the CPU on which the thread ran last */
|
---|
299 | assert(thread->cpu != NULL);
|
---|
300 | cpu = thread->cpu;
|
---|
301 | } else {
|
---|
302 | cpu = CPU;
|
---|
303 | }
|
---|
304 |
|
---|
305 | thread->state = Ready;
|
---|
306 |
|
---|
307 | irq_spinlock_pass(&thread->lock, &(cpu->rq[i].lock));
|
---|
308 |
|
---|
309 | /*
|
---|
310 | * Append thread to respective ready queue
|
---|
311 | * on respective processor.
|
---|
312 | */
|
---|
313 |
|
---|
314 | list_append(&thread->rq_link, &cpu->rq[i].rq);
|
---|
315 | cpu->rq[i].n++;
|
---|
316 | irq_spinlock_unlock(&(cpu->rq[i].lock), true);
|
---|
317 |
|
---|
318 | atomic_inc(&nrdy);
|
---|
319 | atomic_inc(&cpu->nrdy);
|
---|
320 | }
|
---|
321 |
|
---|
322 | /** Create new thread
|
---|
323 | *
|
---|
324 | * Create a new thread.
|
---|
325 | *
|
---|
326 | * @param func Thread's implementing function.
|
---|
327 | * @param arg Thread's implementing function argument.
|
---|
328 | * @param task Task to which the thread belongs. The caller must
|
---|
329 | * guarantee that the task won't cease to exist during the
|
---|
330 | * call. The task's lock may not be held.
|
---|
331 | * @param flags Thread flags.
|
---|
332 | * @param name Symbolic name (a copy is made).
|
---|
333 | *
|
---|
334 | * @return New thread's structure on success, NULL on failure.
|
---|
335 | *
|
---|
336 | */
|
---|
337 | thread_t *thread_create(void (*func)(void *), void *arg, task_t *task,
|
---|
338 | thread_flags_t flags, const char *name)
|
---|
339 | {
|
---|
340 | thread_t *thread = (thread_t *) slab_alloc(thread_cache, 0);
|
---|
341 | if (!thread)
|
---|
342 | return NULL;
|
---|
343 |
|
---|
344 | if (thread_create_arch(thread, flags) != EOK) {
|
---|
345 | slab_free(thread_cache, thread);
|
---|
346 | return NULL;
|
---|
347 | }
|
---|
348 |
|
---|
349 | /* Not needed, but good for debugging */
|
---|
350 | memsetb(thread->kstack, STACK_SIZE, 0);
|
---|
351 |
|
---|
352 | irq_spinlock_lock(&tidlock, true);
|
---|
353 | thread->tid = ++last_tid;
|
---|
354 | irq_spinlock_unlock(&tidlock, true);
|
---|
355 |
|
---|
356 | memset(&thread->saved_context, 0, sizeof(thread->saved_context));
|
---|
357 | context_set(&thread->saved_context, FADDR(cushion),
|
---|
358 | (uintptr_t) thread->kstack, STACK_SIZE);
|
---|
359 |
|
---|
360 | current_initialize((current_t *) thread->kstack);
|
---|
361 |
|
---|
362 | ipl_t ipl = interrupts_disable();
|
---|
363 | thread->saved_context.ipl = interrupts_read();
|
---|
364 | interrupts_restore(ipl);
|
---|
365 |
|
---|
366 | str_cpy(thread->name, THREAD_NAME_BUFLEN, name);
|
---|
367 |
|
---|
368 | thread->thread_code = func;
|
---|
369 | thread->thread_arg = arg;
|
---|
370 | thread->ticks = -1;
|
---|
371 | thread->ucycles = 0;
|
---|
372 | thread->kcycles = 0;
|
---|
373 | thread->uncounted =
|
---|
374 | ((flags & THREAD_FLAG_UNCOUNTED) == THREAD_FLAG_UNCOUNTED);
|
---|
375 | thread->priority = -1; /* Start in rq[0] */
|
---|
376 | thread->cpu = NULL;
|
---|
377 | thread->wired = false;
|
---|
378 | thread->stolen = false;
|
---|
379 | thread->uspace =
|
---|
380 | ((flags & THREAD_FLAG_USPACE) == THREAD_FLAG_USPACE);
|
---|
381 |
|
---|
382 | thread->nomigrate = 0;
|
---|
383 | thread->state = Entering;
|
---|
384 |
|
---|
385 | timeout_initialize(&thread->sleep_timeout);
|
---|
386 | thread->sleep_interruptible = false;
|
---|
387 | thread->sleep_composable = false;
|
---|
388 | thread->sleep_queue = NULL;
|
---|
389 | thread->timeout_pending = false;
|
---|
390 |
|
---|
391 | thread->in_copy_from_uspace = false;
|
---|
392 | thread->in_copy_to_uspace = false;
|
---|
393 |
|
---|
394 | thread->interrupted = false;
|
---|
395 | thread->detached = false;
|
---|
396 | waitq_initialize(&thread->join_wq);
|
---|
397 |
|
---|
398 | thread->task = task;
|
---|
399 |
|
---|
400 | thread->fpu_context_exists = false;
|
---|
401 | thread->fpu_context_engaged = false;
|
---|
402 |
|
---|
403 | odlink_initialize(&thread->lthreads);
|
---|
404 |
|
---|
405 | #ifdef CONFIG_UDEBUG
|
---|
406 | /* Initialize debugging stuff */
|
---|
407 | thread->btrace = false;
|
---|
408 | udebug_thread_initialize(&thread->udebug);
|
---|
409 | #endif
|
---|
410 |
|
---|
411 | if ((flags & THREAD_FLAG_NOATTACH) != THREAD_FLAG_NOATTACH)
|
---|
412 | thread_attach(thread, task);
|
---|
413 |
|
---|
414 | return thread;
|
---|
415 | }
|
---|
416 |
|
---|
417 | /** Destroy thread memory structure
|
---|
418 | *
|
---|
419 | * Detach thread from all queues, cpus etc. and destroy it.
|
---|
420 | *
|
---|
421 | * @param thread Thread to be destroyed.
|
---|
422 | * @param irq_res Indicate whether it should unlock thread->lock
|
---|
423 | * in interrupts-restore mode.
|
---|
424 | *
|
---|
425 | */
|
---|
426 | void thread_destroy(thread_t *thread, bool irq_res)
|
---|
427 | {
|
---|
428 | assert(irq_spinlock_locked(&thread->lock));
|
---|
429 | assert((thread->state == Exiting) || (thread->state == Lingering));
|
---|
430 | assert(thread->task);
|
---|
431 | assert(thread->cpu);
|
---|
432 |
|
---|
433 | irq_spinlock_lock(&thread->cpu->lock, false);
|
---|
434 | if (thread->cpu->fpu_owner == thread)
|
---|
435 | thread->cpu->fpu_owner = NULL;
|
---|
436 | irq_spinlock_unlock(&thread->cpu->lock, false);
|
---|
437 |
|
---|
438 | irq_spinlock_pass(&thread->lock, &threads_lock);
|
---|
439 |
|
---|
440 | odict_remove(&thread->lthreads);
|
---|
441 |
|
---|
442 | irq_spinlock_pass(&threads_lock, &thread->task->lock);
|
---|
443 |
|
---|
444 | /*
|
---|
445 | * Detach from the containing task.
|
---|
446 | */
|
---|
447 | list_remove(&thread->th_link);
|
---|
448 | irq_spinlock_unlock(&thread->task->lock, irq_res);
|
---|
449 |
|
---|
450 | /*
|
---|
451 | * Drop the reference to the containing task.
|
---|
452 | */
|
---|
453 | task_release(thread->task);
|
---|
454 | slab_free(thread_cache, thread);
|
---|
455 | }
|
---|
456 |
|
---|
457 | /** Make the thread visible to the system.
|
---|
458 | *
|
---|
459 | * Attach the thread structure to the current task and make it visible in the
|
---|
460 | * threads_tree.
|
---|
461 | *
|
---|
462 | * @param t Thread to be attached to the task.
|
---|
463 | * @param task Task to which the thread is to be attached.
|
---|
464 | *
|
---|
465 | */
|
---|
466 | void thread_attach(thread_t *thread, task_t *task)
|
---|
467 | {
|
---|
468 | /*
|
---|
469 | * Attach to the specified task.
|
---|
470 | */
|
---|
471 | irq_spinlock_lock(&task->lock, true);
|
---|
472 |
|
---|
473 | /* Hold a reference to the task. */
|
---|
474 | task_hold(task);
|
---|
475 |
|
---|
476 | /* Must not count kbox thread into lifecount */
|
---|
477 | if (thread->uspace)
|
---|
478 | atomic_inc(&task->lifecount);
|
---|
479 |
|
---|
480 | list_append(&thread->th_link, &task->threads);
|
---|
481 |
|
---|
482 | irq_spinlock_pass(&task->lock, &threads_lock);
|
---|
483 |
|
---|
484 | /*
|
---|
485 | * Register this thread in the system-wide dictionary.
|
---|
486 | */
|
---|
487 | odict_insert(&thread->lthreads, &threads, NULL);
|
---|
488 | irq_spinlock_unlock(&threads_lock, true);
|
---|
489 | }
|
---|
490 |
|
---|
491 | /** Terminate thread.
|
---|
492 | *
|
---|
493 | * End current thread execution and switch it to the exiting state.
|
---|
494 | * All pending timeouts are executed.
|
---|
495 | *
|
---|
496 | */
|
---|
497 | void thread_exit(void)
|
---|
498 | {
|
---|
499 | if (THREAD->uspace) {
|
---|
500 | #ifdef CONFIG_UDEBUG
|
---|
501 | /* Generate udebug THREAD_E event */
|
---|
502 | udebug_thread_e_event();
|
---|
503 |
|
---|
504 | /*
|
---|
505 | * This thread will not execute any code or system calls from
|
---|
506 | * now on.
|
---|
507 | */
|
---|
508 | udebug_stoppable_begin();
|
---|
509 | #endif
|
---|
510 | if (atomic_predec(&TASK->lifecount) == 0) {
|
---|
511 | /*
|
---|
512 | * We are the last userspace thread in the task that
|
---|
513 | * still has not exited. With the exception of the
|
---|
514 | * moment the task was created, new userspace threads
|
---|
515 | * can only be created by threads of the same task.
|
---|
516 | * We are safe to perform cleanup.
|
---|
517 | *
|
---|
518 | */
|
---|
519 | ipc_cleanup();
|
---|
520 | futex_task_cleanup();
|
---|
521 | LOG("Cleanup of task %" PRIu64 " completed.", TASK->taskid);
|
---|
522 | }
|
---|
523 | }
|
---|
524 |
|
---|
525 | restart:
|
---|
526 | irq_spinlock_lock(&THREAD->lock, true);
|
---|
527 | if (THREAD->timeout_pending) {
|
---|
528 | /* Busy waiting for timeouts in progress */
|
---|
529 | irq_spinlock_unlock(&THREAD->lock, true);
|
---|
530 | goto restart;
|
---|
531 | }
|
---|
532 |
|
---|
533 | THREAD->state = Exiting;
|
---|
534 | irq_spinlock_unlock(&THREAD->lock, true);
|
---|
535 |
|
---|
536 | scheduler();
|
---|
537 |
|
---|
538 | /* Not reached */
|
---|
539 | while (true)
|
---|
540 | ;
|
---|
541 | }
|
---|
542 |
|
---|
543 | /** Interrupts an existing thread so that it may exit as soon as possible.
|
---|
544 | *
|
---|
545 | * Threads that are blocked waiting for a synchronization primitive
|
---|
546 | * are woken up with a return code of EINTR if the
|
---|
547 | * blocking call was interruptable. See waitq_sleep_timeout().
|
---|
548 | *
|
---|
549 | * The caller must guarantee the thread object is valid during the entire
|
---|
550 | * function, eg by holding the threads_lock lock.
|
---|
551 | *
|
---|
552 | * Interrupted threads automatically exit when returning back to user space.
|
---|
553 | *
|
---|
554 | * @param thread A valid thread object. The caller must guarantee it
|
---|
555 | * will remain valid until thread_interrupt() exits.
|
---|
556 | */
|
---|
557 | void thread_interrupt(thread_t *thread)
|
---|
558 | {
|
---|
559 | assert(thread != NULL);
|
---|
560 |
|
---|
561 | irq_spinlock_lock(&thread->lock, true);
|
---|
562 |
|
---|
563 | thread->interrupted = true;
|
---|
564 | bool sleeping = (thread->state == Sleeping);
|
---|
565 |
|
---|
566 | irq_spinlock_unlock(&thread->lock, true);
|
---|
567 |
|
---|
568 | if (sleeping)
|
---|
569 | waitq_interrupt_sleep(thread);
|
---|
570 | }
|
---|
571 |
|
---|
572 | /** Returns true if the thread was interrupted.
|
---|
573 | *
|
---|
574 | * @param thread A valid thread object. User must guarantee it will
|
---|
575 | * be alive during the entire call.
|
---|
576 | * @return true if the thread was already interrupted via thread_interrupt().
|
---|
577 | */
|
---|
578 | bool thread_interrupted(thread_t *thread)
|
---|
579 | {
|
---|
580 | assert(thread != NULL);
|
---|
581 |
|
---|
582 | bool interrupted;
|
---|
583 |
|
---|
584 | irq_spinlock_lock(&thread->lock, true);
|
---|
585 | interrupted = thread->interrupted;
|
---|
586 | irq_spinlock_unlock(&thread->lock, true);
|
---|
587 |
|
---|
588 | return interrupted;
|
---|
589 | }
|
---|
590 |
|
---|
591 | /** Prevent the current thread from being migrated to another processor. */
|
---|
592 | void thread_migration_disable(void)
|
---|
593 | {
|
---|
594 | assert(THREAD);
|
---|
595 |
|
---|
596 | THREAD->nomigrate++;
|
---|
597 | }
|
---|
598 |
|
---|
599 | /** Allow the current thread to be migrated to another processor. */
|
---|
600 | void thread_migration_enable(void)
|
---|
601 | {
|
---|
602 | assert(THREAD);
|
---|
603 | assert(THREAD->nomigrate > 0);
|
---|
604 |
|
---|
605 | if (THREAD->nomigrate > 0)
|
---|
606 | THREAD->nomigrate--;
|
---|
607 | }
|
---|
608 |
|
---|
609 | /** Thread sleep
|
---|
610 | *
|
---|
611 | * Suspend execution of the current thread.
|
---|
612 | *
|
---|
613 | * @param sec Number of seconds to sleep.
|
---|
614 | *
|
---|
615 | */
|
---|
616 | void thread_sleep(uint32_t sec)
|
---|
617 | {
|
---|
618 | /*
|
---|
619 | * Sleep in 1000 second steps to support
|
---|
620 | * full argument range
|
---|
621 | */
|
---|
622 | while (sec > 0) {
|
---|
623 | uint32_t period = (sec > 1000) ? 1000 : sec;
|
---|
624 |
|
---|
625 | thread_usleep(period * 1000000);
|
---|
626 | sec -= period;
|
---|
627 | }
|
---|
628 | }
|
---|
629 |
|
---|
630 | /** Wait for another thread to exit.
|
---|
631 | *
|
---|
632 | * @param thread Thread to join on exit.
|
---|
633 | * @param usec Timeout in microseconds.
|
---|
634 | * @param flags Mode of operation.
|
---|
635 | *
|
---|
636 | * @return An error code from errno.h or an error code from synch.h.
|
---|
637 | *
|
---|
638 | */
|
---|
639 | errno_t thread_join_timeout(thread_t *thread, uint32_t usec, unsigned int flags)
|
---|
640 | {
|
---|
641 | if (thread == THREAD)
|
---|
642 | return EINVAL;
|
---|
643 |
|
---|
644 | /*
|
---|
645 | * Since thread join can only be called once on an undetached thread,
|
---|
646 | * the thread pointer is guaranteed to be still valid.
|
---|
647 | */
|
---|
648 |
|
---|
649 | irq_spinlock_lock(&thread->lock, true);
|
---|
650 | assert(!thread->detached);
|
---|
651 | irq_spinlock_unlock(&thread->lock, true);
|
---|
652 |
|
---|
653 | return waitq_sleep_timeout(&thread->join_wq, usec, flags, NULL);
|
---|
654 | }
|
---|
655 |
|
---|
656 | /** Detach thread.
|
---|
657 | *
|
---|
658 | * Mark the thread as detached. If the thread is already
|
---|
659 | * in the Lingering state, deallocate its resources.
|
---|
660 | *
|
---|
661 | * @param thread Thread to be detached.
|
---|
662 | *
|
---|
663 | */
|
---|
664 | void thread_detach(thread_t *thread)
|
---|
665 | {
|
---|
666 | /*
|
---|
667 | * Since the thread is expected not to be already detached,
|
---|
668 | * pointer to it must be still valid.
|
---|
669 | */
|
---|
670 | irq_spinlock_lock(&thread->lock, true);
|
---|
671 | assert(!thread->detached);
|
---|
672 |
|
---|
673 | if (thread->state == Lingering) {
|
---|
674 | /*
|
---|
675 | * Unlock &thread->lock and restore
|
---|
676 | * interrupts in thread_destroy().
|
---|
677 | */
|
---|
678 | thread_destroy(thread, true);
|
---|
679 | return;
|
---|
680 | } else {
|
---|
681 | thread->detached = true;
|
---|
682 | }
|
---|
683 |
|
---|
684 | irq_spinlock_unlock(&thread->lock, true);
|
---|
685 | }
|
---|
686 |
|
---|
687 | /** Thread usleep
|
---|
688 | *
|
---|
689 | * Suspend execution of the current thread.
|
---|
690 | *
|
---|
691 | * @param usec Number of microseconds to sleep.
|
---|
692 | *
|
---|
693 | */
|
---|
694 | void thread_usleep(uint32_t usec)
|
---|
695 | {
|
---|
696 | waitq_t wq;
|
---|
697 |
|
---|
698 | waitq_initialize(&wq);
|
---|
699 |
|
---|
700 | (void) waitq_sleep_timeout(&wq, usec, SYNCH_FLAGS_NON_BLOCKING, NULL);
|
---|
701 | }
|
---|
702 |
|
---|
703 | static void thread_print(thread_t *thread, bool additional)
|
---|
704 | {
|
---|
705 | uint64_t ucycles, kcycles;
|
---|
706 | char usuffix, ksuffix;
|
---|
707 | order_suffix(thread->ucycles, &ucycles, &usuffix);
|
---|
708 | order_suffix(thread->kcycles, &kcycles, &ksuffix);
|
---|
709 |
|
---|
710 | char *name;
|
---|
711 | if (str_cmp(thread->name, "uinit") == 0)
|
---|
712 | name = thread->task->name;
|
---|
713 | else
|
---|
714 | name = thread->name;
|
---|
715 |
|
---|
716 | #ifdef __32_BITS__
|
---|
717 | if (additional)
|
---|
718 | printf("%-8" PRIu64 " %10p %10p %9" PRIu64 "%c %9" PRIu64 "%c ",
|
---|
719 | thread->tid, thread->thread_code, thread->kstack,
|
---|
720 | ucycles, usuffix, kcycles, ksuffix);
|
---|
721 | else
|
---|
722 | printf("%-8" PRIu64 " %-14s %10p %-8s %10p %-5" PRIu32 "\n",
|
---|
723 | thread->tid, name, thread, thread_states[thread->state],
|
---|
724 | thread->task, thread->task->container);
|
---|
725 | #endif
|
---|
726 |
|
---|
727 | #ifdef __64_BITS__
|
---|
728 | if (additional)
|
---|
729 | printf("%-8" PRIu64 " %18p %18p\n"
|
---|
730 | " %9" PRIu64 "%c %9" PRIu64 "%c ",
|
---|
731 | thread->tid, thread->thread_code, thread->kstack,
|
---|
732 | ucycles, usuffix, kcycles, ksuffix);
|
---|
733 | else
|
---|
734 | printf("%-8" PRIu64 " %-14s %18p %-8s %18p %-5" PRIu32 "\n",
|
---|
735 | thread->tid, name, thread, thread_states[thread->state],
|
---|
736 | thread->task, thread->task->container);
|
---|
737 | #endif
|
---|
738 |
|
---|
739 | if (additional) {
|
---|
740 | if (thread->cpu)
|
---|
741 | printf("%-5u", thread->cpu->id);
|
---|
742 | else
|
---|
743 | printf("none ");
|
---|
744 |
|
---|
745 | if (thread->state == Sleeping) {
|
---|
746 | #ifdef __32_BITS__
|
---|
747 | printf(" %10p", thread->sleep_queue);
|
---|
748 | #endif
|
---|
749 |
|
---|
750 | #ifdef __64_BITS__
|
---|
751 | printf(" %18p", thread->sleep_queue);
|
---|
752 | #endif
|
---|
753 | }
|
---|
754 |
|
---|
755 | printf("\n");
|
---|
756 | }
|
---|
757 | }
|
---|
758 |
|
---|
759 | /** Print list of threads debug info
|
---|
760 | *
|
---|
761 | * @param additional Print additional information.
|
---|
762 | *
|
---|
763 | */
|
---|
764 | void thread_print_list(bool additional)
|
---|
765 | {
|
---|
766 | thread_t *thread;
|
---|
767 |
|
---|
768 | /* Messing with thread structures, avoid deadlock */
|
---|
769 | irq_spinlock_lock(&threads_lock, true);
|
---|
770 |
|
---|
771 | #ifdef __32_BITS__
|
---|
772 | if (additional)
|
---|
773 | printf("[id ] [code ] [stack ] [ucycles ] [kcycles ]"
|
---|
774 | " [cpu] [waitqueue]\n");
|
---|
775 | else
|
---|
776 | printf("[id ] [name ] [address ] [state ] [task ]"
|
---|
777 | " [ctn]\n");
|
---|
778 | #endif
|
---|
779 |
|
---|
780 | #ifdef __64_BITS__
|
---|
781 | if (additional) {
|
---|
782 | printf("[id ] [code ] [stack ]\n"
|
---|
783 | " [ucycles ] [kcycles ] [cpu] [waitqueue ]\n");
|
---|
784 | } else
|
---|
785 | printf("[id ] [name ] [address ] [state ]"
|
---|
786 | " [task ] [ctn]\n");
|
---|
787 | #endif
|
---|
788 |
|
---|
789 | thread = thread_first();
|
---|
790 | while (thread != NULL) {
|
---|
791 | thread_print(thread, additional);
|
---|
792 | thread = thread_next(thread);
|
---|
793 | }
|
---|
794 |
|
---|
795 | irq_spinlock_unlock(&threads_lock, true);
|
---|
796 | }
|
---|
797 |
|
---|
798 | /** Check whether thread exists.
|
---|
799 | *
|
---|
800 | * Note that threads_lock must be already held and
|
---|
801 | * interrupts must be already disabled.
|
---|
802 | *
|
---|
803 | * @param thread Pointer to thread.
|
---|
804 | *
|
---|
805 | * @return True if thread t is known to the system, false otherwise.
|
---|
806 | *
|
---|
807 | */
|
---|
808 | bool thread_exists(thread_t *thread)
|
---|
809 | {
|
---|
810 | assert(interrupts_disabled());
|
---|
811 | assert(irq_spinlock_locked(&threads_lock));
|
---|
812 |
|
---|
813 | odlink_t *odlink = odict_find_eq(&threads, thread, NULL);
|
---|
814 | return odlink != NULL;
|
---|
815 | }
|
---|
816 |
|
---|
817 | /** Update accounting of current thread.
|
---|
818 | *
|
---|
819 | * Note that thread_lock on THREAD must be already held and
|
---|
820 | * interrupts must be already disabled.
|
---|
821 | *
|
---|
822 | * @param user True to update user accounting, false for kernel.
|
---|
823 | *
|
---|
824 | */
|
---|
825 | void thread_update_accounting(bool user)
|
---|
826 | {
|
---|
827 | uint64_t time = get_cycle();
|
---|
828 |
|
---|
829 | assert(interrupts_disabled());
|
---|
830 | assert(irq_spinlock_locked(&THREAD->lock));
|
---|
831 |
|
---|
832 | if (user)
|
---|
833 | THREAD->ucycles += time - THREAD->last_cycle;
|
---|
834 | else
|
---|
835 | THREAD->kcycles += time - THREAD->last_cycle;
|
---|
836 |
|
---|
837 | THREAD->last_cycle = time;
|
---|
838 | }
|
---|
839 |
|
---|
840 | /** Find thread structure corresponding to thread ID.
|
---|
841 | *
|
---|
842 | * The threads_lock must be already held by the caller of this function and
|
---|
843 | * interrupts must be disabled.
|
---|
844 | *
|
---|
845 | * @param id Thread ID.
|
---|
846 | *
|
---|
847 | * @return Thread structure address or NULL if there is no such thread ID.
|
---|
848 | *
|
---|
849 | */
|
---|
850 | thread_t *thread_find_by_id(thread_id_t thread_id)
|
---|
851 | {
|
---|
852 | thread_t *thread;
|
---|
853 |
|
---|
854 | assert(interrupts_disabled());
|
---|
855 | assert(irq_spinlock_locked(&threads_lock));
|
---|
856 |
|
---|
857 | thread = thread_first();
|
---|
858 | while (thread != NULL) {
|
---|
859 | if (thread->tid == thread_id)
|
---|
860 | return thread;
|
---|
861 |
|
---|
862 | thread = thread_next(thread);
|
---|
863 | }
|
---|
864 |
|
---|
865 | return NULL;
|
---|
866 | }
|
---|
867 |
|
---|
868 | /** Get count of threads.
|
---|
869 | *
|
---|
870 | * @return Number of threads in the system
|
---|
871 | */
|
---|
872 | size_t thread_count(void)
|
---|
873 | {
|
---|
874 | assert(interrupts_disabled());
|
---|
875 | assert(irq_spinlock_locked(&threads_lock));
|
---|
876 |
|
---|
877 | return odict_count(&threads);
|
---|
878 | }
|
---|
879 |
|
---|
880 | /** Get first thread.
|
---|
881 | *
|
---|
882 | * @return Pointer to first thread or @c NULL if there are none.
|
---|
883 | */
|
---|
884 | thread_t *thread_first(void)
|
---|
885 | {
|
---|
886 | odlink_t *odlink;
|
---|
887 |
|
---|
888 | assert(interrupts_disabled());
|
---|
889 | assert(irq_spinlock_locked(&threads_lock));
|
---|
890 |
|
---|
891 | odlink = odict_first(&threads);
|
---|
892 | if (odlink == NULL)
|
---|
893 | return NULL;
|
---|
894 |
|
---|
895 | return odict_get_instance(odlink, thread_t, lthreads);
|
---|
896 | }
|
---|
897 |
|
---|
898 | /** Get next thread.
|
---|
899 | *
|
---|
900 | * @param cur Current thread
|
---|
901 | * @return Pointer to next thread or @c NULL if there are no more threads.
|
---|
902 | */
|
---|
903 | thread_t *thread_next(thread_t *cur)
|
---|
904 | {
|
---|
905 | odlink_t *odlink;
|
---|
906 |
|
---|
907 | assert(interrupts_disabled());
|
---|
908 | assert(irq_spinlock_locked(&threads_lock));
|
---|
909 |
|
---|
910 | odlink = odict_next(&cur->lthreads, &threads);
|
---|
911 | if (odlink == NULL)
|
---|
912 | return NULL;
|
---|
913 |
|
---|
914 | return odict_get_instance(odlink, thread_t, lthreads);
|
---|
915 | }
|
---|
916 |
|
---|
917 | #ifdef CONFIG_UDEBUG
|
---|
918 |
|
---|
919 | void thread_stack_trace(thread_id_t thread_id)
|
---|
920 | {
|
---|
921 | irq_spinlock_lock(&threads_lock, true);
|
---|
922 |
|
---|
923 | thread_t *thread = thread_find_by_id(thread_id);
|
---|
924 | if (thread == NULL) {
|
---|
925 | printf("No such thread.\n");
|
---|
926 | irq_spinlock_unlock(&threads_lock, true);
|
---|
927 | return;
|
---|
928 | }
|
---|
929 |
|
---|
930 | irq_spinlock_lock(&thread->lock, false);
|
---|
931 |
|
---|
932 | /*
|
---|
933 | * Schedule a stack trace to be printed
|
---|
934 | * just before the thread is scheduled next.
|
---|
935 | *
|
---|
936 | * If the thread is sleeping then try to interrupt
|
---|
937 | * the sleep. Any request for printing an uspace stack
|
---|
938 | * trace from within the kernel should be always
|
---|
939 | * considered a last resort debugging means, therefore
|
---|
940 | * forcing the thread's sleep to be interrupted
|
---|
941 | * is probably justifiable.
|
---|
942 | */
|
---|
943 |
|
---|
944 | bool sleeping = false;
|
---|
945 | istate_t *istate = thread->udebug.uspace_state;
|
---|
946 | if (istate != NULL) {
|
---|
947 | printf("Scheduling thread stack trace.\n");
|
---|
948 | thread->btrace = true;
|
---|
949 | if (thread->state == Sleeping)
|
---|
950 | sleeping = true;
|
---|
951 | } else
|
---|
952 | printf("Thread interrupt state not available.\n");
|
---|
953 |
|
---|
954 | irq_spinlock_unlock(&thread->lock, false);
|
---|
955 |
|
---|
956 | if (sleeping)
|
---|
957 | waitq_interrupt_sleep(thread);
|
---|
958 |
|
---|
959 | irq_spinlock_unlock(&threads_lock, true);
|
---|
960 | }
|
---|
961 |
|
---|
962 | #endif /* CONFIG_UDEBUG */
|
---|
963 |
|
---|
964 | /** Get key function for the @c threads ordered dictionary.
|
---|
965 | *
|
---|
966 | * @param odlink Link
|
---|
967 | * @return Pointer to thread structure cast as 'void *'
|
---|
968 | */
|
---|
969 | static void *threads_getkey(odlink_t *odlink)
|
---|
970 | {
|
---|
971 | thread_t *thread = odict_get_instance(odlink, thread_t, lthreads);
|
---|
972 | return (void *) thread;
|
---|
973 | }
|
---|
974 |
|
---|
975 | /** Key comparison function for the @c threads ordered dictionary.
|
---|
976 | *
|
---|
977 | * @param a Pointer to thread A
|
---|
978 | * @param b Pointer to thread B
|
---|
979 | * @return -1, 0, 1 iff pointer to A is less than, equal to, greater than B
|
---|
980 | */
|
---|
981 | static int threads_cmp(void *a, void *b)
|
---|
982 | {
|
---|
983 | if (a > b)
|
---|
984 | return -1;
|
---|
985 | else if (a == b)
|
---|
986 | return 0;
|
---|
987 | else
|
---|
988 | return +1;
|
---|
989 | }
|
---|
990 |
|
---|
991 | /** Process syscall to create new thread.
|
---|
992 | *
|
---|
993 | */
|
---|
994 | sys_errno_t sys_thread_create(uspace_arg_t *uspace_uarg, char *uspace_name,
|
---|
995 | size_t name_len, thread_id_t *uspace_thread_id)
|
---|
996 | {
|
---|
997 | if (name_len > THREAD_NAME_BUFLEN - 1)
|
---|
998 | name_len = THREAD_NAME_BUFLEN - 1;
|
---|
999 |
|
---|
1000 | char namebuf[THREAD_NAME_BUFLEN];
|
---|
1001 | errno_t rc = copy_from_uspace(namebuf, uspace_name, name_len);
|
---|
1002 | if (rc != EOK)
|
---|
1003 | return (sys_errno_t) rc;
|
---|
1004 |
|
---|
1005 | namebuf[name_len] = 0;
|
---|
1006 |
|
---|
1007 | /*
|
---|
1008 | * In case of failure, kernel_uarg will be deallocated in this function.
|
---|
1009 | * In case of success, kernel_uarg will be freed in uinit().
|
---|
1010 | */
|
---|
1011 | uspace_arg_t *kernel_uarg =
|
---|
1012 | (uspace_arg_t *) malloc(sizeof(uspace_arg_t));
|
---|
1013 | if (!kernel_uarg)
|
---|
1014 | return (sys_errno_t) ENOMEM;
|
---|
1015 |
|
---|
1016 | rc = copy_from_uspace(kernel_uarg, uspace_uarg, sizeof(uspace_arg_t));
|
---|
1017 | if (rc != EOK) {
|
---|
1018 | free(kernel_uarg);
|
---|
1019 | return (sys_errno_t) rc;
|
---|
1020 | }
|
---|
1021 |
|
---|
1022 | thread_t *thread = thread_create(uinit, kernel_uarg, TASK,
|
---|
1023 | THREAD_FLAG_USPACE | THREAD_FLAG_NOATTACH, namebuf);
|
---|
1024 | if (thread) {
|
---|
1025 | if (uspace_thread_id != NULL) {
|
---|
1026 | rc = copy_to_uspace(uspace_thread_id, &thread->tid,
|
---|
1027 | sizeof(thread->tid));
|
---|
1028 | if (rc != EOK) {
|
---|
1029 | /*
|
---|
1030 | * We have encountered a failure, but the thread
|
---|
1031 | * has already been created. We need to undo its
|
---|
1032 | * creation now.
|
---|
1033 | */
|
---|
1034 |
|
---|
1035 | /*
|
---|
1036 | * The new thread structure is initialized, but
|
---|
1037 | * is still not visible to the system.
|
---|
1038 | * We can safely deallocate it.
|
---|
1039 | */
|
---|
1040 | slab_free(thread_cache, thread);
|
---|
1041 | free(kernel_uarg);
|
---|
1042 |
|
---|
1043 | return (sys_errno_t) rc;
|
---|
1044 | }
|
---|
1045 | }
|
---|
1046 |
|
---|
1047 | #ifdef CONFIG_UDEBUG
|
---|
1048 | /*
|
---|
1049 | * Generate udebug THREAD_B event and attach the thread.
|
---|
1050 | * This must be done atomically (with the debug locks held),
|
---|
1051 | * otherwise we would either miss some thread or receive
|
---|
1052 | * THREAD_B events for threads that already existed
|
---|
1053 | * and could be detected with THREAD_READ before.
|
---|
1054 | */
|
---|
1055 | udebug_thread_b_event_attach(thread, TASK);
|
---|
1056 | #else
|
---|
1057 | thread_attach(thread, TASK);
|
---|
1058 | #endif
|
---|
1059 | thread_ready(thread);
|
---|
1060 |
|
---|
1061 | return 0;
|
---|
1062 | } else
|
---|
1063 | free(kernel_uarg);
|
---|
1064 |
|
---|
1065 | return (sys_errno_t) ENOMEM;
|
---|
1066 | }
|
---|
1067 |
|
---|
1068 | /** Process syscall to terminate thread.
|
---|
1069 | *
|
---|
1070 | */
|
---|
1071 | sys_errno_t sys_thread_exit(int uspace_status)
|
---|
1072 | {
|
---|
1073 | thread_exit();
|
---|
1074 | }
|
---|
1075 |
|
---|
1076 | /** Syscall for getting TID.
|
---|
1077 | *
|
---|
1078 | * @param uspace_thread_id Userspace address of 8-byte buffer where to store
|
---|
1079 | * current thread ID.
|
---|
1080 | *
|
---|
1081 | * @return 0 on success or an error code from @ref errno.h.
|
---|
1082 | *
|
---|
1083 | */
|
---|
1084 | sys_errno_t sys_thread_get_id(thread_id_t *uspace_thread_id)
|
---|
1085 | {
|
---|
1086 | /*
|
---|
1087 | * No need to acquire lock on THREAD because tid
|
---|
1088 | * remains constant for the lifespan of the thread.
|
---|
1089 | *
|
---|
1090 | */
|
---|
1091 | return (sys_errno_t) copy_to_uspace(uspace_thread_id, &THREAD->tid,
|
---|
1092 | sizeof(THREAD->tid));
|
---|
1093 | }
|
---|
1094 |
|
---|
1095 | /** Syscall wrapper for sleeping. */
|
---|
1096 | sys_errno_t sys_thread_usleep(uint32_t usec)
|
---|
1097 | {
|
---|
1098 | thread_usleep(usec);
|
---|
1099 | return 0;
|
---|
1100 | }
|
---|
1101 |
|
---|
1102 | sys_errno_t sys_thread_udelay(uint32_t usec)
|
---|
1103 | {
|
---|
1104 | delay(usec);
|
---|
1105 | return 0;
|
---|
1106 | }
|
---|
1107 |
|
---|
1108 | /** @}
|
---|
1109 | */
|
---|