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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since dec16a2 was e535eeb, checked in by Martin Decky <martin@…>, 15 years ago

rename order() to order_suffix(), make it a generic libc string function in user space

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