| 1 | /*
|
|---|
| 2 | * kern/sys_thread_join.c - passive wait on the end of a given thread
|
|---|
| 3 | *
|
|---|
| 4 | * Copyright (c) 2008,2009,2010,2011,2012 Ghassan Almaless
|
|---|
| 5 | * Copyright (c) 2011,2012 UPMC Sorbonne Universites
|
|---|
| 6 | *
|
|---|
| 7 | * This file is part of ALMOS-kernel.
|
|---|
| 8 | *
|
|---|
| 9 | * ALMOS-kernel is free software; you can redistribute it and/or modify it
|
|---|
| 10 | * under the terms of the GNU General Public License as published by
|
|---|
| 11 | * the Free Software Foundation; version 2.0 of the License.
|
|---|
| 12 | *
|
|---|
| 13 | * ALMOS-kernel is distributed in the hope that it will be useful, but
|
|---|
| 14 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|---|
| 16 | * General Public License for more details.
|
|---|
| 17 | *
|
|---|
| 18 | * You should have received a copy of the GNU General Public License
|
|---|
| 19 | * along with ALMOS-kernel; if not, write to the Free Software Foundation,
|
|---|
| 20 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|---|
| 21 | */
|
|---|
| 22 |
|
|---|
| 23 | #include <list.h>
|
|---|
| 24 | #include <thread.h>
|
|---|
| 25 | #include <scheduler.h>
|
|---|
| 26 | #include <kmem.h>
|
|---|
| 27 | #include <errno.h>
|
|---|
| 28 | #include <task.h>
|
|---|
| 29 | #include <spinlock.h>
|
|---|
| 30 |
|
|---|
| 31 | int sys_thread_join (pthread_t tid, void **thread_return)
|
|---|
| 32 | {
|
|---|
| 33 | register struct task_s *task;
|
|---|
| 34 | register struct thread_s *this;
|
|---|
| 35 | register struct thread_s *target_th;
|
|---|
| 36 | register uint_t state = 0;
|
|---|
| 37 | void *retval;
|
|---|
| 38 | int err;
|
|---|
| 39 |
|
|---|
| 40 | this = current_thread;
|
|---|
| 41 | task = this->task;
|
|---|
| 42 | retval = 0;
|
|---|
| 43 |
|
|---|
| 44 | if((tid > task->max_order) ||
|
|---|
| 45 | ((thread_return != NULL) &&
|
|---|
| 46 | (NOT_IN_USPACE((uint_t)thread_return + sizeof(void*)))))
|
|---|
| 47 | {
|
|---|
| 48 | err = EINVAL;
|
|---|
| 49 | goto fail_arg;
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | /* try to write to userland address */
|
|---|
| 53 | if(thread_return)
|
|---|
| 54 | {
|
|---|
| 55 | if((err = cpu_copy_to_uspace(thread_return, &retval, sizeof(void *))))
|
|---|
| 56 | goto fail_uspace_ret;
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | spinlock_lock(&task->th_lock);
|
|---|
| 60 | target_th = task->th_tbl[tid];
|
|---|
| 61 |
|
|---|
| 62 | if((target_th == NULL) ||
|
|---|
| 63 | (target_th->signature != THREAD_ID) ||
|
|---|
| 64 | (target_th->info.attr.key != tid))
|
|---|
| 65 | {
|
|---|
| 66 | err = ESRCH;
|
|---|
| 67 | goto fail_srch;
|
|---|
| 68 | }
|
|---|
| 69 |
|
|---|
| 70 | if(target_th == this)
|
|---|
| 71 | {
|
|---|
| 72 | err = EDEADLK;
|
|---|
| 73 | goto fail_deadlock;
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | if(!(thread_isJoinable(target_th)))
|
|---|
| 77 | {
|
|---|
| 78 | err = EINVAL;
|
|---|
| 79 | goto fail_joinable;
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | spinlock_lock(&target_th->lock);
|
|---|
| 83 |
|
|---|
| 84 | if(target_th->info.join != NULL)
|
|---|
| 85 | {
|
|---|
| 86 | spinlock_unlock(&target_th->lock);
|
|---|
| 87 | err = EINVAL;
|
|---|
| 88 | goto fail_joined;
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | // Get the exit code of the target thread
|
|---|
| 92 | if ((state=wait_queue_isEmpty(&target_th->info.wait_queue)))
|
|---|
| 93 | {
|
|---|
| 94 | target_th->info.join = this;
|
|---|
| 95 | wait_on(&target_th->info.wait_queue, WAIT_ANY);
|
|---|
| 96 | spinlock_unlock(&target_th->lock);
|
|---|
| 97 | spinlock_unlock_nosched(&task->th_lock);
|
|---|
| 98 | sched_sleep(this);
|
|---|
| 99 | retval = this->info.exit_value;
|
|---|
| 100 | }
|
|---|
| 101 | else
|
|---|
| 102 | {
|
|---|
| 103 | retval = target_th->info.exit_value;
|
|---|
| 104 | wakeup_one(&target_th->info.wait_queue, WAIT_ANY);
|
|---|
| 105 | spinlock_unlock(&target_th->lock);
|
|---|
| 106 | spinlock_unlock(&task->th_lock);
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | /* Probably will not fail */
|
|---|
| 110 | if(thread_return)
|
|---|
| 111 | {
|
|---|
| 112 | if((err = cpu_copy_to_uspace(thread_return, &retval, sizeof(void *))))
|
|---|
| 113 | goto fail_uspace;
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | return 0;
|
|---|
| 117 |
|
|---|
| 118 | fail_joined:
|
|---|
| 119 | fail_joinable:
|
|---|
| 120 | fail_deadlock:
|
|---|
| 121 | fail_srch:
|
|---|
| 122 | spinlock_unlock(&task->th_lock);
|
|---|
| 123 |
|
|---|
| 124 | fail_uspace:
|
|---|
| 125 | fail_uspace_ret:
|
|---|
| 126 | fail_arg:
|
|---|
| 127 | this->info.errno = err;
|
|---|
| 128 | return err;
|
|---|
| 129 | }
|
|---|