[1] | 1 | /* |
---|
[23] | 2 | * sys_thread_join.c - passive wait on the end of a given thread. |
---|
[1] | 3 | * |
---|
[23] | 4 | * Authors Alain Greiner (2016,2017) |
---|
| 5 | * |
---|
[1] | 6 | * Copyright (c) 2011,2012 UPMC Sorbonne Universites |
---|
| 7 | * |
---|
[23] | 8 | * This file is part of ALMOS-MKH. |
---|
[1] | 9 | * |
---|
[23] | 10 | * ALMOS-MKH is free software; you can redistribute it and/or modify it |
---|
[1] | 11 | * under the terms of the GNU General Public License as published by |
---|
| 12 | * the Free Software Foundation; version 2.0 of the License. |
---|
| 13 | * |
---|
[23] | 14 | * ALMOS-MKH is distributed in the hope that it will be useful, but |
---|
[1] | 15 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
| 17 | * General Public License for more details. |
---|
| 18 | * |
---|
| 19 | * You should have received a copy of the GNU General Public License |
---|
[23] | 20 | * along with ALMOS-MKH; if not, write to the Free Software Foundation, |
---|
[1] | 21 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
| 22 | */ |
---|
| 23 | |
---|
[457] | 24 | #include <hal_kernel_types.h> |
---|
[23] | 25 | #include <hal_remote.h> |
---|
| 26 | #include <hal_special.h> |
---|
[436] | 27 | #include <hal_irqmask.h> |
---|
[1] | 28 | #include <thread.h> |
---|
[23] | 29 | #include <vmm.h> |
---|
[1] | 30 | #include <scheduler.h> |
---|
| 31 | #include <errno.h> |
---|
[23] | 32 | #include <printk.h> |
---|
| 33 | #include <remote_spinlock.h> |
---|
[1] | 34 | |
---|
[23] | 35 | /////////////////////////////////////// |
---|
| 36 | int sys_thread_join ( trdid_t trdid, |
---|
| 37 | void ** exit_value ) |
---|
[1] | 38 | { |
---|
[436] | 39 | reg_t save_sr; |
---|
[23] | 40 | xptr_t target_xp; |
---|
| 41 | thread_t * target_ptr; |
---|
| 42 | cxy_t target_cxy; |
---|
| 43 | ltid_t target_ltid; |
---|
[436] | 44 | xptr_t target_join_lock_xp; |
---|
| 45 | xptr_t target_flags_xp; |
---|
| 46 | xptr_t target_blocked_xp; |
---|
| 47 | xptr_t target_join_xp_xp; |
---|
| 48 | xptr_t killer_xp; |
---|
| 49 | xptr_t joining_xp; |
---|
| 50 | thread_t * joining_ptr; |
---|
| 51 | process_t * process; |
---|
[1] | 52 | |
---|
[436] | 53 | // get joining thread pointers |
---|
| 54 | joining_ptr = CURRENT_THREAD; |
---|
| 55 | joining_xp = XPTR( local_cxy , joining_ptr ); |
---|
| 56 | process = joining_ptr->process; |
---|
[1] | 57 | |
---|
[23] | 58 | // get target thread ltid and cxy |
---|
| 59 | target_ltid = LTID_FROM_TRDID( trdid ); |
---|
| 60 | target_cxy = CXY_FROM_TRDID( trdid ); |
---|
| 61 | |
---|
[438] | 62 | #if DEBUG_SYS_THREAD_JOIN |
---|
[436] | 63 | uint64_t tm_start; |
---|
| 64 | uint64_t tm_end; |
---|
| 65 | tm_start = hal_get_cycles(); |
---|
[438] | 66 | if( DEBUG_SYS_THREAD_JOIN < tm_start ) |
---|
[436] | 67 | printk("\n[DBG] %s : parent thread %x enter / process %x / target trdid %x / cycle %d\n", |
---|
| 68 | __FUNCTION__ , joining_ptr , process->pid , trdid , (uint32_t)tm_start ); |
---|
| 69 | #endif |
---|
| 70 | |
---|
[23] | 71 | // check trdid argument |
---|
| 72 | if( (target_ltid >= CONFIG_THREAD_MAX_PER_CLUSTER) || cluster_is_undefined( target_cxy ) ) |
---|
[1] | 73 | { |
---|
[436] | 74 | |
---|
[438] | 75 | #if DEBUG_SYSCALLS_ERROR |
---|
[436] | 76 | printk("\n[ERROR] in %s : illegal trdid argument %x\n", __FUNCTION__, trdid ); |
---|
| 77 | #endif |
---|
| 78 | joining_ptr->errno = EINVAL; |
---|
[23] | 79 | return -1; |
---|
[1] | 80 | } |
---|
| 81 | |
---|
[23] | 82 | // check exit_value argument |
---|
[436] | 83 | if( exit_value != NULL ) |
---|
[1] | 84 | { |
---|
[436] | 85 | |
---|
[438] | 86 | #if DEBUG_SYSCALLS_ERROR |
---|
[436] | 87 | printk("\n[ERROR] in %s : exit_value argument must be NULL\n", __FUNCTION__ ); |
---|
| 88 | #endif |
---|
| 89 | joining_ptr->errno = EINVAL; |
---|
[23] | 90 | return -1; |
---|
[1] | 91 | } |
---|
| 92 | |
---|
[23] | 93 | // check target thread != this thread |
---|
[436] | 94 | if( joining_ptr->trdid == trdid ) |
---|
[23] | 95 | { |
---|
[436] | 96 | |
---|
[438] | 97 | #if DEBUG_SYSCALLS_ERROR |
---|
[473] | 98 | printk("\n[ERROR] in %s : this thread (%x) == target thread(%x)\n", |
---|
| 99 | __FUNCTION__, joining_ptr->trdid, trdid ); |
---|
[436] | 100 | #endif |
---|
| 101 | joining_ptr->errno = EDEADLK; |
---|
[23] | 102 | return -1; |
---|
| 103 | } |
---|
[1] | 104 | |
---|
[436] | 105 | // get pointers on target thread |
---|
[23] | 106 | target_xp = thread_get_xptr( process->pid , trdid ); |
---|
[436] | 107 | target_ptr = GET_PTR( target_xp ); |
---|
[1] | 108 | |
---|
[23] | 109 | if( target_xp == XPTR_NULL ) |
---|
| 110 | { |
---|
[436] | 111 | |
---|
[438] | 112 | #if DEBUG_SYSCALLS_ERROR |
---|
[436] | 113 | printk("\n[ERROR] in %s : target thread %x not found\n", __FUNCTION__, trdid ); |
---|
| 114 | #endif |
---|
| 115 | joining_ptr->errno = ESRCH; |
---|
[23] | 116 | return -1; |
---|
| 117 | } |
---|
[1] | 118 | |
---|
[436] | 119 | // get extended pointers on various fields in target thread |
---|
| 120 | target_join_lock_xp = XPTR( target_cxy , &target_ptr->join_lock ); |
---|
| 121 | target_flags_xp = XPTR( target_cxy , &target_ptr->flags ); |
---|
| 122 | target_blocked_xp = XPTR( target_cxy , &target_ptr->blocked ); |
---|
| 123 | target_join_xp_xp = XPTR( target_cxy , &target_ptr->join_xp ); |
---|
[1] | 124 | |
---|
[23] | 125 | // check target thread joinable |
---|
[436] | 126 | if( (hal_remote_lw( target_flags_xp ) & THREAD_FLAG_DETACHED) == 0 ) |
---|
[23] | 127 | { |
---|
[436] | 128 | |
---|
[438] | 129 | #if DEBUG_SYSCALLS_ERROR |
---|
[436] | 130 | printk("\n[ERROR] in %s : target thread %x not joinable\n", __FUNCTION__, trdid ); |
---|
| 131 | #endif |
---|
| 132 | joining_ptr->errno = EINVAL; |
---|
[23] | 133 | return -1; |
---|
| 134 | } |
---|
[1] | 135 | |
---|
[436] | 136 | // mask IRQs |
---|
| 137 | hal_disable_irq( &save_sr ); |
---|
[1] | 138 | |
---|
[409] | 139 | // get the lock protecting the join in target thread |
---|
[436] | 140 | remote_spinlock_lock( target_join_lock_xp ); |
---|
[409] | 141 | |
---|
[436] | 142 | // test the kill_done flag from the target thread |
---|
| 143 | if( hal_remote_lw( target_flags_xp ) & THREAD_FLAG_KILL_DONE ) // killer thread is first |
---|
[23] | 144 | { |
---|
[436] | 145 | // get pointers on killer thread |
---|
| 146 | killer_xp = (xptr_t)hal_remote_lwd( target_join_xp_xp ); |
---|
[1] | 147 | |
---|
[436] | 148 | // reset the kill_done flag in target thread |
---|
| 149 | hal_remote_atomic_and( target_flags_xp , ~THREAD_FLAG_KILL_DONE ); |
---|
[1] | 150 | |
---|
[436] | 151 | // unblock the killer thread |
---|
| 152 | thread_unblock( killer_xp , THREAD_BLOCKED_JOIN ); |
---|
| 153 | |
---|
| 154 | // release the lock protecting join |
---|
| 155 | remote_spinlock_unlock( target_join_lock_xp ); |
---|
| 156 | |
---|
| 157 | // restore IRQs |
---|
| 158 | hal_restore_irq( save_sr ); |
---|
[409] | 159 | } |
---|
[436] | 160 | else // joining thread is first |
---|
[409] | 161 | { |
---|
[436] | 162 | // set the join_done flag in target thread |
---|
| 163 | hal_remote_atomic_or( target_flags_xp , THREAD_FLAG_JOIN_DONE ); |
---|
[23] | 164 | |
---|
[436] | 165 | // block joining thread on BLOCKED_JOIN |
---|
| 166 | thread_block( joining_xp , THREAD_BLOCKED_JOIN ); |
---|
[23] | 167 | |
---|
[436] | 168 | // register the joining thread extended pointer in target thread |
---|
| 169 | hal_remote_swd( target_join_xp_xp , joining_xp ); |
---|
[23] | 170 | |
---|
[436] | 171 | // release the lock protecting the join |
---|
| 172 | remote_spinlock_unlock( target_join_lock_xp ); |
---|
[23] | 173 | |
---|
[409] | 174 | // deschedule |
---|
[436] | 175 | sched_yield( "joining thread waiting killer thread" ); |
---|
[409] | 176 | |
---|
[436] | 177 | // restore IRQs |
---|
| 178 | hal_restore_irq( save_sr ); |
---|
[23] | 179 | } |
---|
| 180 | |
---|
[438] | 181 | #if DEBUG_SYS_THREAD_JOIN |
---|
[436] | 182 | tm_end = hal_get_cycles(); |
---|
[438] | 183 | if( DEBUG_SYS_THREAD_JOIN < tm_end ) |
---|
[436] | 184 | printk("\n[DBG] %s : parent thread %x exit / process %x / target trdid %x / cycle %d\n", |
---|
| 185 | __FUNCTION__, joining_ptr, process->pid, trdid, (uint32_t)tm_end ); |
---|
| 186 | #endif |
---|
| 187 | |
---|
[23] | 188 | return 0; |
---|
| 189 | |
---|
| 190 | } // end sys_thread_join() |
---|