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

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

Accounting separated to kernel and user time.

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