source: mainline/kernel/generic/src/proc/thread.c@ bbda5ab

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since bbda5ab was 1ba37fa, checked in by Stanislav Kozina <stanislav.kozina@…>, 15 years ago

Removed useless cycles sum, using ucycles + kcycles instead.

  • Property mode set to 100644
File size: 19.3 KB
Line 
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
29/** @addtogroup genericproc
30 * @{
31 */
32
33/**
34 * @file
35 * @brief Thread management functions.
36 */
37
38#include <proc/scheduler.h>
39#include <proc/thread.h>
40#include <proc/task.h>
41#include <proc/uarg.h>
42#include <mm/frame.h>
43#include <mm/page.h>
44#include <arch/asm.h>
45#include <arch/cycle.h>
46#include <arch.h>
47#include <synch/synch.h>
48#include <synch/spinlock.h>
49#include <synch/waitq.h>
50#include <synch/rwlock.h>
51#include <cpu.h>
52#include <func.h>
53#include <context.h>
54#include <adt/avl.h>
55#include <adt/list.h>
56#include <time/clock.h>
57#include <time/timeout.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 <memstr.h>
64#include <print.h>
65#include <mm/slab.h>
66#include <debug.h>
67#include <main/uinit.h>
68#include <syscall/copy.h>
69#include <errno.h>
70
71
72#ifndef LOADED_PROG_STACK_PAGES_NO
73#define LOADED_PROG_STACK_PAGES_NO 1
74#endif
75
76
77/** Thread states */
78const char *thread_states[] = {
79 "Invalid",
80 "Running",
81 "Sleeping",
82 "Ready",
83 "Entering",
84 "Exiting",
85 "Lingering"
86};
87
88/** Lock protecting the threads_tree AVL tree.
89 *
90 * For locking rules, see declaration thereof.
91 */
92SPINLOCK_INITIALIZE(threads_lock);
93
94/** AVL tree of all threads.
95 *
96 * When a thread is found in the threads_tree AVL tree, it is guaranteed to
97 * exist as long as the threads_lock is held.
98 */
99avltree_t threads_tree;
100
101SPINLOCK_INITIALIZE(tidlock);
102thread_id_t last_tid = 0;
103
104static slab_cache_t *thread_slab;
105#ifdef CONFIG_FPU
106slab_cache_t *fpu_context_slab;
107#endif
108
109/** Thread wrapper.
110 *
111 * This wrapper is provided to ensure that every thread makes a call to
112 * thread_exit() when its implementing function returns.
113 *
114 * interrupts_disable() is assumed.
115 *
116 */
117static void cushion(void)
118{
119 void (*f)(void *) = THREAD->thread_code;
120 void *arg = THREAD->thread_arg;
121 THREAD->last_cycle = get_cycle();
122
123 /* This is where each thread wakes up after its creation */
124 spinlock_unlock(&THREAD->lock);
125 interrupts_enable();
126
127 f(arg);
128
129 /* Accumulate accounting to the task */
130 ipl_t ipl = interrupts_disable();
131
132 spinlock_lock(&THREAD->lock);
133 if (!THREAD->uncounted) {
134 thread_update_accounting(true);
135 uint64_t ucycles = THREAD->ucycles;
136 THREAD->ucycles = 0;
137 uint64_t kcycles = THREAD->kcycles;
138 THREAD->kcycles = 0;
139
140 spinlock_unlock(&THREAD->lock);
141
142 spinlock_lock(&TASK->lock);
143 TASK->ucycles += ucycles;
144 TASK->kcycles += kcycles;
145 spinlock_unlock(&TASK->lock);
146 } else
147 spinlock_unlock(&THREAD->lock);
148
149 interrupts_restore(ipl);
150
151 thread_exit();
152 /* not reached */
153}
154
155/** Initialization and allocation for thread_t structure */
156static int thr_constructor(void *obj, int kmflags)
157{
158 thread_t *t = (thread_t *) obj;
159
160 spinlock_initialize(&t->lock, "thread_t_lock");
161 link_initialize(&t->rq_link);
162 link_initialize(&t->wq_link);
163 link_initialize(&t->th_link);
164
165 /* call the architecture-specific part of the constructor */
166 thr_constructor_arch(t);
167
168#ifdef CONFIG_FPU
169#ifdef CONFIG_FPU_LAZY
170 t->saved_fpu_context = NULL;
171#else
172 t->saved_fpu_context = slab_alloc(fpu_context_slab, kmflags);
173 if (!t->saved_fpu_context)
174 return -1;
175#endif
176#endif
177
178 t->kstack = (uint8_t *) frame_alloc(STACK_FRAMES, FRAME_KA | kmflags);
179 if (!t->kstack) {
180#ifdef CONFIG_FPU
181 if (t->saved_fpu_context)
182 slab_free(fpu_context_slab, t->saved_fpu_context);
183#endif
184 return -1;
185 }
186
187#ifdef CONFIG_UDEBUG
188 mutex_initialize(&t->udebug.lock, MUTEX_PASSIVE);
189#endif
190
191 return 0;
192}
193
194/** Destruction of thread_t object */
195static int thr_destructor(void *obj)
196{
197 thread_t *t = (thread_t *) obj;
198
199 /* call the architecture-specific part of the destructor */
200 thr_destructor_arch(t);
201
202 frame_free(KA2PA(t->kstack));
203#ifdef CONFIG_FPU
204 if (t->saved_fpu_context)
205 slab_free(fpu_context_slab, t->saved_fpu_context);
206#endif
207 return 1; /* One page freed */
208}
209
210/** Initialize threads
211 *
212 * Initialize kernel threads support.
213 *
214 */
215void thread_init(void)
216{
217 THREAD = NULL;
218 atomic_set(&nrdy, 0);
219 thread_slab = slab_cache_create("thread_slab", sizeof(thread_t), 0,
220 thr_constructor, thr_destructor, 0);
221
222#ifdef CONFIG_FPU
223 fpu_context_slab = slab_cache_create("fpu_slab", sizeof(fpu_context_t),
224 FPU_CONTEXT_ALIGN, NULL, NULL, 0);
225#endif
226
227 avltree_create(&threads_tree);
228}
229
230/** Make thread ready
231 *
232 * Switch thread t to the ready state.
233 *
234 * @param t Thread to make ready.
235 *
236 */
237void thread_ready(thread_t *t)
238{
239 cpu_t *cpu;
240 runq_t *r;
241 ipl_t ipl;
242 int i, avg;
243
244 ipl = interrupts_disable();
245
246 spinlock_lock(&t->lock);
247
248 ASSERT(!(t->state == Ready));
249
250 i = (t->priority < RQ_COUNT - 1) ? ++t->priority : t->priority;
251
252 cpu = CPU;
253 if (t->flags & THREAD_FLAG_WIRED) {
254 ASSERT(t->cpu != NULL);
255 cpu = t->cpu;
256 }
257 t->state = Ready;
258 spinlock_unlock(&t->lock);
259
260 /*
261 * Append t to respective ready queue on respective processor.
262 */
263 r = &cpu->rq[i];
264 spinlock_lock(&r->lock);
265 list_append(&t->rq_link, &r->rq_head);
266 r->n++;
267 spinlock_unlock(&r->lock);
268
269 atomic_inc(&nrdy);
270 // FIXME: Why is the avg value never read?
271 avg = atomic_get(&nrdy) / config.cpu_active;
272 atomic_inc(&cpu->nrdy);
273
274 interrupts_restore(ipl);
275}
276
277/** Create new thread
278 *
279 * Create a new thread.
280 *
281 * @param func Thread's implementing function.
282 * @param arg Thread's implementing function argument.
283 * @param task Task to which the thread belongs. The caller must
284 * guarantee that the task won't cease to exist during the
285 * call. The task's lock may not be held.
286 * @param flags Thread flags.
287 * @param name Symbolic name (a copy is made).
288 * @param uncounted Thread's accounting doesn't affect accumulated task
289 * accounting.
290 *
291 * @return New thread's structure on success, NULL on failure.
292 *
293 */
294thread_t *thread_create(void (* func)(void *), void *arg, task_t *task,
295 int flags, const char *name, bool uncounted)
296{
297 thread_t *t;
298 ipl_t ipl;
299
300 t = (thread_t *) slab_alloc(thread_slab, 0);
301 if (!t)
302 return NULL;
303
304 /* Not needed, but good for debugging */
305 memsetb(t->kstack, THREAD_STACK_SIZE * 1 << STACK_FRAMES, 0);
306
307 ipl = interrupts_disable();
308 spinlock_lock(&tidlock);
309 t->tid = ++last_tid;
310 spinlock_unlock(&tidlock);
311 interrupts_restore(ipl);
312
313 context_save(&t->saved_context);
314 context_set(&t->saved_context, FADDR(cushion), (uintptr_t) t->kstack,
315 THREAD_STACK_SIZE);
316
317 the_initialize((the_t *) t->kstack);
318
319 ipl = interrupts_disable();
320 t->saved_context.ipl = interrupts_read();
321 interrupts_restore(ipl);
322
323 memcpy(t->name, name, THREAD_NAME_BUFLEN);
324 t->name[THREAD_NAME_BUFLEN - 1] = 0;
325
326 t->thread_code = func;
327 t->thread_arg = arg;
328 t->ticks = -1;
329 t->ucycles = 0;
330 t->kcycles = 0;
331 t->uncounted = uncounted;
332 t->priority = -1; /* start in rq[0] */
333 t->cpu = NULL;
334 t->flags = flags;
335 t->state = Entering;
336 t->call_me = NULL;
337 t->call_me_with = NULL;
338
339 timeout_initialize(&t->sleep_timeout);
340 t->sleep_interruptible = false;
341 t->sleep_queue = NULL;
342 t->timeout_pending = 0;
343
344 t->in_copy_from_uspace = false;
345 t->in_copy_to_uspace = false;
346
347 t->interrupted = false;
348 t->detached = false;
349 waitq_initialize(&t->join_wq);
350
351 t->rwlock_holder_type = RWLOCK_NONE;
352
353 t->task = task;
354
355 t->fpu_context_exists = 0;
356 t->fpu_context_engaged = 0;
357
358 avltree_node_initialize(&t->threads_tree_node);
359 t->threads_tree_node.key = (uintptr_t) t;
360
361#ifdef CONFIG_UDEBUG
362 /* Init debugging stuff */
363 udebug_thread_initialize(&t->udebug);
364#endif
365
366 /* might depend on previous initialization */
367 thread_create_arch(t);
368
369 if (!(flags & THREAD_FLAG_NOATTACH))
370 thread_attach(t, task);
371
372 return t;
373}
374
375/** Destroy thread memory structure
376 *
377 * Detach thread from all queues, cpus etc. and destroy it.
378 *
379 * Assume thread->lock is held!!
380 */
381void thread_destroy(thread_t *t)
382{
383 ASSERT(t->state == Exiting || t->state == Lingering);
384 ASSERT(t->task);
385 ASSERT(t->cpu);
386
387 spinlock_lock(&t->cpu->lock);
388 if (t->cpu->fpu_owner == t)
389 t->cpu->fpu_owner = NULL;
390 spinlock_unlock(&t->cpu->lock);
391
392 spinlock_unlock(&t->lock);
393
394 spinlock_lock(&threads_lock);
395 avltree_delete(&threads_tree, &t->threads_tree_node);
396 spinlock_unlock(&threads_lock);
397
398 /*
399 * Detach from the containing task.
400 */
401 spinlock_lock(&t->task->lock);
402 list_remove(&t->th_link);
403 spinlock_unlock(&t->task->lock);
404
405 /*
406 * t is guaranteed to be the very last thread of its task.
407 * It is safe to destroy the task.
408 */
409 if (atomic_predec(&t->task->refcount) == 0)
410 task_destroy(t->task);
411
412 slab_free(thread_slab, t);
413}
414
415/** Make the thread visible to the system.
416 *
417 * Attach the thread structure to the current task and make it visible in the
418 * threads_tree.
419 *
420 * @param t Thread to be attached to the task.
421 * @param task Task to which the thread is to be attached.
422 */
423void thread_attach(thread_t *t, task_t *task)
424{
425 ipl_t ipl;
426
427 /*
428 * Attach to the specified task.
429 */
430 ipl = interrupts_disable();
431 spinlock_lock(&task->lock);
432
433 atomic_inc(&task->refcount);
434
435 /* Must not count kbox thread into lifecount */
436 if (t->flags & THREAD_FLAG_USPACE)
437 atomic_inc(&task->lifecount);
438
439 list_append(&t->th_link, &task->th_head);
440 spinlock_unlock(&task->lock);
441
442 /*
443 * Register this thread in the system-wide list.
444 */
445 spinlock_lock(&threads_lock);
446 avltree_insert(&threads_tree, &t->threads_tree_node);
447 spinlock_unlock(&threads_lock);
448
449 interrupts_restore(ipl);
450}
451
452/** Terminate thread.
453 *
454 * End current thread execution and switch it to the exiting state. All pending
455 * timeouts are executed.
456 */
457void thread_exit(void)
458{
459 ipl_t ipl;
460
461 if (THREAD->flags & THREAD_FLAG_USPACE) {
462#ifdef CONFIG_UDEBUG
463 /* Generate udebug THREAD_E event */
464 udebug_thread_e_event();
465#endif
466 if (atomic_predec(&TASK->lifecount) == 0) {
467 /*
468 * We are the last userspace thread in the task that
469 * still has not exited. With the exception of the
470 * moment the task was created, new userspace threads
471 * can only be created by threads of the same task.
472 * We are safe to perform cleanup.
473 */
474 ipc_cleanup();
475 futex_cleanup();
476 LOG("Cleanup of task %" PRIu64" completed.", TASK->taskid);
477 }
478 }
479
480restart:
481 ipl = interrupts_disable();
482 spinlock_lock(&THREAD->lock);
483 if (THREAD->timeout_pending) {
484 /* busy waiting for timeouts in progress */
485 spinlock_unlock(&THREAD->lock);
486 interrupts_restore(ipl);
487 goto restart;
488 }
489
490 THREAD->state = Exiting;
491 spinlock_unlock(&THREAD->lock);
492 scheduler();
493
494 /* Not reached */
495 while (1)
496 ;
497}
498
499
500/** Thread sleep
501 *
502 * Suspend execution of the current thread.
503 *
504 * @param sec Number of seconds to sleep.
505 *
506 */
507void thread_sleep(uint32_t sec)
508{
509 /* Sleep in 1000 second steps to support
510 full argument range */
511 while (sec > 0) {
512 uint32_t period = (sec > 1000) ? 1000 : sec;
513
514 thread_usleep(period * 1000000);
515 sec -= period;
516 }
517}
518
519/** Wait for another thread to exit.
520 *
521 * @param t Thread to join on exit.
522 * @param usec Timeout in microseconds.
523 * @param flags Mode of operation.
524 *
525 * @return An error code from errno.h or an error code from synch.h.
526 */
527int thread_join_timeout(thread_t *t, uint32_t usec, int flags)
528{
529 ipl_t ipl;
530 int rc;
531
532 if (t == THREAD)
533 return EINVAL;
534
535 /*
536 * Since thread join can only be called once on an undetached thread,
537 * the thread pointer is guaranteed to be still valid.
538 */
539
540 ipl = interrupts_disable();
541 spinlock_lock(&t->lock);
542 ASSERT(!t->detached);
543 spinlock_unlock(&t->lock);
544 interrupts_restore(ipl);
545
546 rc = waitq_sleep_timeout(&t->join_wq, usec, flags);
547
548 return rc;
549}
550
551/** Detach thread.
552 *
553 * Mark the thread as detached, if the thread is already in the Lingering
554 * state, deallocate its resources.
555 *
556 * @param t Thread to be detached.
557 */
558void thread_detach(thread_t *t)
559{
560 ipl_t ipl;
561
562 /*
563 * Since the thread is expected not to be already detached,
564 * pointer to it must be still valid.
565 */
566 ipl = interrupts_disable();
567 spinlock_lock(&t->lock);
568 ASSERT(!t->detached);
569 if (t->state == Lingering) {
570 thread_destroy(t); /* unlocks &t->lock */
571 interrupts_restore(ipl);
572 return;
573 } else {
574 t->detached = true;
575 }
576 spinlock_unlock(&t->lock);
577 interrupts_restore(ipl);
578}
579
580/** Thread usleep
581 *
582 * Suspend execution of the current thread.
583 *
584 * @param usec Number of microseconds to sleep.
585 *
586 */
587void thread_usleep(uint32_t usec)
588{
589 waitq_t wq;
590
591 waitq_initialize(&wq);
592
593 (void) waitq_sleep_timeout(&wq, usec, SYNCH_FLAGS_NON_BLOCKING);
594}
595
596/** Register thread out-of-context invocation
597 *
598 * Register a function and its argument to be executed
599 * on next context switch to the current thread.
600 *
601 * @param call_me Out-of-context function.
602 * @param call_me_with Out-of-context function argument.
603 *
604 */
605void thread_register_call_me(void (* call_me)(void *), void *call_me_with)
606{
607 ipl_t ipl;
608
609 ipl = interrupts_disable();
610 spinlock_lock(&THREAD->lock);
611 THREAD->call_me = call_me;
612 THREAD->call_me_with = call_me_with;
613 spinlock_unlock(&THREAD->lock);
614 interrupts_restore(ipl);
615}
616
617static bool thread_walker(avltree_node_t *node, void *arg)
618{
619 thread_t *t = avltree_get_instance(node, thread_t, threads_tree_node);
620
621 uint64_t ucycles, kcycles;
622 char usuffix, ksuffix;
623 order(t->ucycles, &ucycles, &usuffix);
624 order(t->kcycles, &kcycles, &ksuffix);
625
626#ifdef __32_BITS__
627 printf("%-6" PRIu64" %-10s %10p %-8s %10p %-3" PRIu32 " %10p %10p %9"
628 PRIu64 "%c %9" PRIu64 "%c ", t->tid, t->name, t,
629 thread_states[t->state], t->task, t->task->context, t->thread_code,
630 t->kstack, ucycles, usuffix, kcycles, ksuffix);
631#endif
632
633#ifdef __64_BITS__
634 printf("%-6" PRIu64" %-10s %18p %-8s %18p %-3" PRIu32 " %18p %18p %9"
635 PRIu64 "%c %9" PRIu64 "%c ", t->tid, t->name, t,
636 thread_states[t->state], t->task, t->task->context, t->thread_code,
637 t->kstack, ucycles, usuffix, kcycles, ksuffix);
638#endif
639
640 if (t->cpu)
641 printf("%-4u", t->cpu->id);
642 else
643 printf("none");
644
645 if (t->state == Sleeping) {
646#ifdef __32_BITS__
647 printf(" %10p", t->sleep_queue);
648#endif
649
650#ifdef __64_BITS__
651 printf(" %18p", t->sleep_queue);
652#endif
653 }
654
655 printf("\n");
656
657 return true;
658}
659
660/** Print list of threads debug info */
661void thread_print_list(void)
662{
663 ipl_t ipl;
664
665 /* Messing with thread structures, avoid deadlock */
666 ipl = interrupts_disable();
667 spinlock_lock(&threads_lock);
668
669#ifdef __32_BITS__
670 printf("tid name address state task "
671 "ctx code stack ucycles kcycles cpu "
672 "waitqueue\n");
673 printf("------ ---------- ---------- -------- ---------- "
674 "--- ---------- ---------- ---------- ---------- ---- "
675 "----------\n");
676#endif
677
678#ifdef __64_BITS__
679 printf("tid name address state task "
680 "ctx code stack ucycles kcycles cpu "
681 "waitqueue\n");
682 printf("------ ---------- ------------------ -------- ------------------ "
683 "--- ------------------ ------------------ ---------- ---------- ---- "
684 "------------------\n");
685#endif
686
687 avltree_walk(&threads_tree, thread_walker, NULL);
688
689 spinlock_unlock(&threads_lock);
690 interrupts_restore(ipl);
691}
692
693/** Check whether thread exists.
694 *
695 * Note that threads_lock must be already held and
696 * interrupts must be already disabled.
697 *
698 * @param t Pointer to thread.
699 *
700 * @return True if thread t is known to the system, false otherwise.
701 */
702bool thread_exists(thread_t *t)
703{
704 avltree_node_t *node;
705
706 node = avltree_search(&threads_tree, (avltree_key_t) ((uintptr_t) t));
707
708 return node != NULL;
709}
710
711/** Update accounting of current thread.
712 *
713 * Note that thread_lock on THREAD must be already held and
714 * interrupts must be already disabled.
715 *
716 * @param user True to update user accounting, false for kernel.
717 */
718void thread_update_accounting(bool user)
719{
720 uint64_t time = get_cycle();
721 if (user) {
722 THREAD->ucycles += time - THREAD->last_cycle;
723 } else {
724 THREAD->kcycles += time - THREAD->last_cycle;
725 }
726 THREAD->last_cycle = time;
727}
728
729/** Process syscall to create new thread.
730 *
731 */
732unative_t sys_thread_create(uspace_arg_t *uspace_uarg, char *uspace_name,
733 size_t name_len, thread_id_t *uspace_thread_id)
734{
735 thread_t *t;
736 char namebuf[THREAD_NAME_BUFLEN];
737 uspace_arg_t *kernel_uarg;
738 int rc;
739
740 if (name_len > THREAD_NAME_BUFLEN - 1)
741 name_len = THREAD_NAME_BUFLEN - 1;
742
743 rc = copy_from_uspace(namebuf, uspace_name, name_len);
744 if (rc != 0)
745 return (unative_t) rc;
746
747 namebuf[name_len] = 0;
748
749 /*
750 * In case of failure, kernel_uarg will be deallocated in this function.
751 * In case of success, kernel_uarg will be freed in uinit().
752 */
753 kernel_uarg = (uspace_arg_t *) malloc(sizeof(uspace_arg_t), 0);
754
755 rc = copy_from_uspace(kernel_uarg, uspace_uarg, sizeof(uspace_arg_t));
756 if (rc != 0) {
757 free(kernel_uarg);
758 return (unative_t) rc;
759 }
760
761 t = thread_create(uinit, kernel_uarg, TASK,
762 THREAD_FLAG_USPACE | THREAD_FLAG_NOATTACH, namebuf, false);
763 if (t) {
764 if (uspace_thread_id != NULL) {
765 int rc;
766
767 rc = copy_to_uspace(uspace_thread_id, &t->tid,
768 sizeof(t->tid));
769 if (rc != 0) {
770 /*
771 * We have encountered a failure, but the thread
772 * has already been created. We need to undo its
773 * creation now.
774 */
775
776 /*
777 * The new thread structure is initialized, but
778 * is still not visible to the system.
779 * We can safely deallocate it.
780 */
781 slab_free(thread_slab, t);
782 free(kernel_uarg);
783
784 return (unative_t) rc;
785 }
786 }
787#ifdef CONFIG_UDEBUG
788 /*
789 * Generate udebug THREAD_B event and attach the thread.
790 * This must be done atomically (with the debug locks held),
791 * otherwise we would either miss some thread or receive
792 * THREAD_B events for threads that already existed
793 * and could be detected with THREAD_READ before.
794 */
795 udebug_thread_b_event_attach(t, TASK);
796#else
797 thread_attach(t, TASK);
798#endif
799 thread_ready(t);
800
801 return 0;
802 } else
803 free(kernel_uarg);
804
805 return (unative_t) ENOMEM;
806}
807
808/** Process syscall to terminate thread.
809 *
810 */
811unative_t sys_thread_exit(int uspace_status)
812{
813 thread_exit();
814 /* Unreachable */
815 return 0;
816}
817
818/** Syscall for getting TID.
819 *
820 * @param uspace_thread_id Userspace address of 8-byte buffer where to store
821 * current thread ID.
822 *
823 * @return 0 on success or an error code from @ref errno.h.
824 */
825unative_t sys_thread_get_id(thread_id_t *uspace_thread_id)
826{
827 /*
828 * No need to acquire lock on THREAD because tid
829 * remains constant for the lifespan of the thread.
830 */
831 return (unative_t) copy_to_uspace(uspace_thread_id, &THREAD->tid,
832 sizeof(THREAD->tid));
833}
834
835/** Syscall wrapper for sleeping. */
836unative_t sys_thread_usleep(uint32_t usec)
837{
838 thread_usleep(usec);
839 return 0;
840}
841
842/** @}
843 */
Note: See TracBrowser for help on using the repository browser.