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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 42bbbe2 was 7ed8530, checked in by Jakub Jermar <jakub@…>, 15 years ago

Deploy task_hold() and task_release().

  • Property mode set to 100644
File size: 20.2 KB
Line 
1/*
2 * Copyright (c) 2010 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 * Drop the reference to the containing task.
412 */
413 task_release(t->task);
414
415 slab_free(thread_slab, t);
416}
417
418/** Make the thread visible to the system.
419 *
420 * Attach the thread structure to the current task and make it visible in the
421 * threads_tree.
422 *
423 * @param t Thread to be attached to the task.
424 * @param task Task to which the thread is to be attached.
425 */
426void thread_attach(thread_t *t, task_t *task)
427{
428 ipl_t ipl;
429
430 /*
431 * Attach to the specified task.
432 */
433 ipl = interrupts_disable();
434 spinlock_lock(&task->lock);
435
436 /* Hold a reference to the task. */
437 task_hold(task);
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 ucycles, kcycles;
626 char usuffix, ksuffix;
627 order_suffix(t->ucycles, &ucycles, &usuffix);
628 order_suffix(t->kcycles, &kcycles, &ksuffix);
629
630#ifdef __32_BITS__
631 printf("%-6" PRIu64" %-10s %10p %-8s %10p %-3" PRIu32 " %10p %10p %9"
632 PRIu64 "%c %9" PRIu64 "%c ", t->tid, t->name, t,
633 thread_states[t->state], t->task, t->task->context, t->thread_code,
634 t->kstack, ucycles, usuffix, kcycles, ksuffix);
635#endif
636
637#ifdef __64_BITS__
638 printf("%-6" PRIu64" %-10s %18p %-8s %18p %-3" PRIu32 " %18p %18p %9"
639 PRIu64 "%c %9" PRIu64 "%c ", t->tid, t->name, t,
640 thread_states[t->state], t->task, t->task->context, t->thread_code,
641 t->kstack, ucycles, usuffix, kcycles, ksuffix);
642#endif
643
644 if (t->cpu)
645 printf("%-4u", t->cpu->id);
646 else
647 printf("none");
648
649 if (t->state == Sleeping) {
650#ifdef __32_BITS__
651 printf(" %10p", t->sleep_queue);
652#endif
653
654#ifdef __64_BITS__
655 printf(" %18p", t->sleep_queue);
656#endif
657 }
658
659 printf("\n");
660
661 return true;
662}
663
664/** Print list of threads debug info */
665void thread_print_list(void)
666{
667 ipl_t ipl;
668
669 /* Messing with thread structures, avoid deadlock */
670 ipl = interrupts_disable();
671 spinlock_lock(&threads_lock);
672
673#ifdef __32_BITS__
674 printf("tid name address state task "
675 "ctx code stack ucycles kcycles cpu "
676 "waitqueue\n");
677 printf("------ ---------- ---------- -------- ---------- "
678 "--- ---------- ---------- ---------- ---------- ---- "
679 "----------\n");
680#endif
681
682#ifdef __64_BITS__
683 printf("tid name address state task "
684 "ctx code stack ucycles kcycles cpu "
685 "waitqueue\n");
686 printf("------ ---------- ------------------ -------- ------------------ "
687 "--- ------------------ ------------------ ---------- ---------- ---- "
688 "------------------\n");
689#endif
690
691 avltree_walk(&threads_tree, thread_walker, NULL);
692
693 spinlock_unlock(&threads_lock);
694 interrupts_restore(ipl);
695}
696
697/** Check whether thread exists.
698 *
699 * Note that threads_lock must be already held and
700 * interrupts must be already disabled.
701 *
702 * @param t Pointer to thread.
703 *
704 * @return True if thread t is known to the system, false otherwise.
705 */
706bool thread_exists(thread_t *t)
707{
708 avltree_node_t *node;
709
710 node = avltree_search(&threads_tree, (avltree_key_t) ((uintptr_t) t));
711
712 return node != NULL;
713}
714
715/** Update accounting of current thread.
716 *
717 * Note that thread_lock on THREAD must be already held and
718 * interrupts must be already disabled.
719 *
720 * @param user True to update user accounting, false for kernel.
721 */
722void thread_update_accounting(bool user)
723{
724 uint64_t time = get_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
733static bool thread_search_walker(avltree_node_t *node, void *arg)
734{
735 thread_t *thread =
736 (thread_t *) avltree_get_instance(node, thread_t, threads_tree_node);
737 thread_iterator_t *iterator = (thread_iterator_t *) arg;
738
739 if (thread->tid == iterator->thread_id) {
740 iterator->thread = thread;
741 return false;
742 }
743
744 return true;
745}
746
747/** Find thread structure corresponding to thread ID.
748 *
749 * The threads_lock must be already held by the caller of this function and
750 * interrupts must be disabled.
751 *
752 * @param id Thread ID.
753 *
754 * @return Thread structure address or NULL if there is no such thread ID.
755 *
756 */
757thread_t *thread_find_by_id(thread_id_t thread_id)
758{
759 thread_iterator_t iterator;
760
761 iterator.thread_id = thread_id;
762 iterator.thread = NULL;
763
764 avltree_walk(&threads_tree, thread_search_walker, (void *) &iterator);
765
766 return iterator.thread;
767}
768
769
770/** Process syscall to create new thread.
771 *
772 */
773unative_t sys_thread_create(uspace_arg_t *uspace_uarg, char *uspace_name,
774 size_t name_len, thread_id_t *uspace_thread_id)
775{
776 thread_t *t;
777 char namebuf[THREAD_NAME_BUFLEN];
778 uspace_arg_t *kernel_uarg;
779 int rc;
780
781 if (name_len > THREAD_NAME_BUFLEN - 1)
782 name_len = THREAD_NAME_BUFLEN - 1;
783
784 rc = copy_from_uspace(namebuf, uspace_name, name_len);
785 if (rc != 0)
786 return (unative_t) rc;
787
788 namebuf[name_len] = 0;
789
790 /*
791 * In case of failure, kernel_uarg will be deallocated in this function.
792 * In case of success, kernel_uarg will be freed in uinit().
793 */
794 kernel_uarg = (uspace_arg_t *) malloc(sizeof(uspace_arg_t), 0);
795
796 rc = copy_from_uspace(kernel_uarg, uspace_uarg, sizeof(uspace_arg_t));
797 if (rc != 0) {
798 free(kernel_uarg);
799 return (unative_t) rc;
800 }
801
802 t = thread_create(uinit, kernel_uarg, TASK,
803 THREAD_FLAG_USPACE | THREAD_FLAG_NOATTACH, namebuf, false);
804 if (t) {
805 if (uspace_thread_id != NULL) {
806 int rc;
807
808 rc = copy_to_uspace(uspace_thread_id, &t->tid,
809 sizeof(t->tid));
810 if (rc != 0) {
811 /*
812 * We have encountered a failure, but the thread
813 * has already been created. We need to undo its
814 * creation now.
815 */
816
817 /*
818 * The new thread structure is initialized, but
819 * is still not visible to the system.
820 * We can safely deallocate it.
821 */
822 slab_free(thread_slab, t);
823 free(kernel_uarg);
824
825 return (unative_t) rc;
826 }
827 }
828#ifdef CONFIG_UDEBUG
829 /*
830 * Generate udebug THREAD_B event and attach the thread.
831 * This must be done atomically (with the debug locks held),
832 * otherwise we would either miss some thread or receive
833 * THREAD_B events for threads that already existed
834 * and could be detected with THREAD_READ before.
835 */
836 udebug_thread_b_event_attach(t, TASK);
837#else
838 thread_attach(t, TASK);
839#endif
840 thread_ready(t);
841
842 return 0;
843 } else
844 free(kernel_uarg);
845
846 return (unative_t) ENOMEM;
847}
848
849/** Process syscall to terminate thread.
850 *
851 */
852unative_t sys_thread_exit(int uspace_status)
853{
854 thread_exit();
855 /* Unreachable */
856 return 0;
857}
858
859/** Syscall for getting TID.
860 *
861 * @param uspace_thread_id Userspace address of 8-byte buffer where to store
862 * current thread ID.
863 *
864 * @return 0 on success or an error code from @ref errno.h.
865 */
866unative_t sys_thread_get_id(thread_id_t *uspace_thread_id)
867{
868 /*
869 * No need to acquire lock on THREAD because tid
870 * remains constant for the lifespan of the thread.
871 */
872 return (unative_t) copy_to_uspace(uspace_thread_id, &THREAD->tid,
873 sizeof(THREAD->tid));
874}
875
876/** Syscall wrapper for sleeping. */
877unative_t sys_thread_usleep(uint32_t usec)
878{
879 thread_usleep(usec);
880 return 0;
881}
882
883/** @}
884 */
Note: See TracBrowser for help on using the repository browser.