[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> |
---|
[566] | 33 | #include <remote_busylock.h> |
---|
[1] | 34 | |
---|
[506] | 35 | #include <syscalls.h> |
---|
| 36 | |
---|
[23] | 37 | /////////////////////////////////////// |
---|
| 38 | int sys_thread_join ( trdid_t trdid, |
---|
| 39 | void ** exit_value ) |
---|
[1] | 40 | { |
---|
[436] | 41 | reg_t save_sr; |
---|
[23] | 42 | xptr_t target_xp; |
---|
| 43 | thread_t * target_ptr; |
---|
| 44 | cxy_t target_cxy; |
---|
| 45 | ltid_t target_ltid; |
---|
[436] | 46 | xptr_t target_join_lock_xp; |
---|
| 47 | xptr_t target_flags_xp; |
---|
| 48 | xptr_t target_blocked_xp; |
---|
| 49 | xptr_t target_join_xp_xp; |
---|
| 50 | xptr_t killer_xp; |
---|
| 51 | xptr_t joining_xp; |
---|
| 52 | thread_t * joining_ptr; |
---|
| 53 | process_t * process; |
---|
[1] | 54 | |
---|
[436] | 55 | // get joining thread pointers |
---|
| 56 | joining_ptr = CURRENT_THREAD; |
---|
| 57 | joining_xp = XPTR( local_cxy , joining_ptr ); |
---|
| 58 | process = joining_ptr->process; |
---|
[1] | 59 | |
---|
[23] | 60 | // get target thread ltid and cxy |
---|
| 61 | target_ltid = LTID_FROM_TRDID( trdid ); |
---|
| 62 | target_cxy = CXY_FROM_TRDID( trdid ); |
---|
| 63 | |
---|
[438] | 64 | #if DEBUG_SYS_THREAD_JOIN |
---|
[436] | 65 | uint64_t tm_start; |
---|
| 66 | uint64_t tm_end; |
---|
| 67 | tm_start = hal_get_cycles(); |
---|
[438] | 68 | if( DEBUG_SYS_THREAD_JOIN < tm_start ) |
---|
[566] | 69 | printk("\n[DBG] %s : thread %x in process %x enter / target thread %x / cycle %d\n", |
---|
| 70 | __FUNCTION__ , joining_ptr->trdid, process->pid, trdid , (uint32_t)tm_start ); |
---|
[436] | 71 | #endif |
---|
| 72 | |
---|
[23] | 73 | // check trdid argument |
---|
[566] | 74 | if( (target_ltid >= CONFIG_THREADS_MAX_PER_CLUSTER) || cluster_is_undefined( target_cxy ) ) |
---|
[1] | 75 | { |
---|
[436] | 76 | |
---|
[438] | 77 | #if DEBUG_SYSCALLS_ERROR |
---|
[436] | 78 | printk("\n[ERROR] in %s : illegal trdid argument %x\n", __FUNCTION__, trdid ); |
---|
| 79 | #endif |
---|
| 80 | joining_ptr->errno = EINVAL; |
---|
[23] | 81 | return -1; |
---|
[1] | 82 | } |
---|
| 83 | |
---|
[23] | 84 | // check exit_value argument |
---|
[436] | 85 | if( exit_value != NULL ) |
---|
[1] | 86 | { |
---|
[436] | 87 | |
---|
[438] | 88 | #if DEBUG_SYSCALLS_ERROR |
---|
[436] | 89 | printk("\n[ERROR] in %s : exit_value argument must be NULL\n", __FUNCTION__ ); |
---|
| 90 | #endif |
---|
| 91 | joining_ptr->errno = EINVAL; |
---|
[23] | 92 | return -1; |
---|
[1] | 93 | } |
---|
| 94 | |
---|
[23] | 95 | // check target thread != this thread |
---|
[436] | 96 | if( joining_ptr->trdid == trdid ) |
---|
[23] | 97 | { |
---|
[436] | 98 | |
---|
[438] | 99 | #if DEBUG_SYSCALLS_ERROR |
---|
[473] | 100 | printk("\n[ERROR] in %s : this thread (%x) == target thread(%x)\n", |
---|
| 101 | __FUNCTION__, joining_ptr->trdid, trdid ); |
---|
[436] | 102 | #endif |
---|
| 103 | joining_ptr->errno = EDEADLK; |
---|
[23] | 104 | return -1; |
---|
| 105 | } |
---|
[1] | 106 | |
---|
[436] | 107 | // get pointers on target thread |
---|
[23] | 108 | target_xp = thread_get_xptr( process->pid , trdid ); |
---|
[436] | 109 | target_ptr = GET_PTR( target_xp ); |
---|
[1] | 110 | |
---|
[23] | 111 | if( target_xp == XPTR_NULL ) |
---|
| 112 | { |
---|
[436] | 113 | |
---|
[438] | 114 | #if DEBUG_SYSCALLS_ERROR |
---|
[436] | 115 | printk("\n[ERROR] in %s : target thread %x not found\n", __FUNCTION__, trdid ); |
---|
| 116 | #endif |
---|
| 117 | joining_ptr->errno = ESRCH; |
---|
[23] | 118 | return -1; |
---|
| 119 | } |
---|
[1] | 120 | |
---|
[436] | 121 | // get extended pointers on various fields in target thread |
---|
| 122 | target_join_lock_xp = XPTR( target_cxy , &target_ptr->join_lock ); |
---|
| 123 | target_flags_xp = XPTR( target_cxy , &target_ptr->flags ); |
---|
| 124 | target_blocked_xp = XPTR( target_cxy , &target_ptr->blocked ); |
---|
| 125 | target_join_xp_xp = XPTR( target_cxy , &target_ptr->join_xp ); |
---|
[1] | 126 | |
---|
[23] | 127 | // check target thread joinable |
---|
[566] | 128 | if( (hal_remote_l32( target_flags_xp ) & THREAD_FLAG_DETACHED) != 0 ) |
---|
[23] | 129 | { |
---|
[436] | 130 | |
---|
[438] | 131 | #if DEBUG_SYSCALLS_ERROR |
---|
[436] | 132 | printk("\n[ERROR] in %s : target thread %x not joinable\n", __FUNCTION__, trdid ); |
---|
| 133 | #endif |
---|
| 134 | joining_ptr->errno = EINVAL; |
---|
[23] | 135 | return -1; |
---|
| 136 | } |
---|
[1] | 137 | |
---|
[436] | 138 | // mask IRQs |
---|
| 139 | hal_disable_irq( &save_sr ); |
---|
[1] | 140 | |
---|
[409] | 141 | // get the lock protecting the join in target thread |
---|
[566] | 142 | remote_busylock_acquire( target_join_lock_xp ); |
---|
[409] | 143 | |
---|
[436] | 144 | // test the kill_done flag from the target thread |
---|
[566] | 145 | if( hal_remote_l32( target_flags_xp ) & THREAD_FLAG_KILL_DONE ) // killer thread is first |
---|
[23] | 146 | { |
---|
[566] | 147 | |
---|
| 148 | #if (DEBUG_SYS_THREAD_JOIN & 1) |
---|
| 149 | if( DEBUG_SYS_THREAD_JOIN < tm_start ) |
---|
| 150 | printk("\n[DBG] %s : thread %x in process %x / killer thread arrived first\n", |
---|
| 151 | __FUNCTION__ , joining_ptr->trdid, process->pid ); |
---|
| 152 | #endif |
---|
[436] | 153 | // get pointers on killer thread |
---|
[566] | 154 | killer_xp = (xptr_t)hal_remote_l64( target_join_xp_xp ); |
---|
[1] | 155 | |
---|
[436] | 156 | // reset the kill_done flag in target thread |
---|
| 157 | hal_remote_atomic_and( target_flags_xp , ~THREAD_FLAG_KILL_DONE ); |
---|
[1] | 158 | |
---|
[436] | 159 | // unblock the killer thread |
---|
| 160 | thread_unblock( killer_xp , THREAD_BLOCKED_JOIN ); |
---|
| 161 | |
---|
| 162 | // release the lock protecting join |
---|
[566] | 163 | remote_busylock_release( target_join_lock_xp ); |
---|
[436] | 164 | |
---|
| 165 | // restore IRQs |
---|
| 166 | hal_restore_irq( save_sr ); |
---|
[409] | 167 | } |
---|
[436] | 168 | else // joining thread is first |
---|
[409] | 169 | { |
---|
[566] | 170 | |
---|
| 171 | #if (DEBUG_SYS_THREAD_JOIN & 1) |
---|
| 172 | if( DEBUG_SYS_THREAD_JOIN < tm_start ) |
---|
| 173 | printk("\n[DBG] %s : thread %x in process %x / joining thread arrived first\n", |
---|
| 174 | __FUNCTION__ , joining_ptr->trdid, process->pid ); |
---|
| 175 | #endif |
---|
[436] | 176 | // set the join_done flag in target thread |
---|
| 177 | hal_remote_atomic_or( target_flags_xp , THREAD_FLAG_JOIN_DONE ); |
---|
[23] | 178 | |
---|
[436] | 179 | // block joining thread on BLOCKED_JOIN |
---|
| 180 | thread_block( joining_xp , THREAD_BLOCKED_JOIN ); |
---|
[23] | 181 | |
---|
[436] | 182 | // register the joining thread extended pointer in target thread |
---|
[566] | 183 | hal_remote_s64( target_join_xp_xp , joining_xp ); |
---|
[23] | 184 | |
---|
[436] | 185 | // release the lock protecting the join |
---|
[566] | 186 | remote_busylock_release( target_join_lock_xp ); |
---|
[23] | 187 | |
---|
[566] | 188 | #if (DEBUG_SYS_THREAD_JOIN & 1) |
---|
| 189 | if( DEBUG_SYS_THREAD_JOIN < tm_start ) |
---|
| 190 | printk("\n[DBG] %s : thread %x in process %x / joining thread deschedule\n", |
---|
| 191 | __FUNCTION__ , joining_ptr->trdid, process->pid ); |
---|
| 192 | #endif |
---|
[409] | 193 | // deschedule |
---|
[436] | 194 | sched_yield( "joining thread waiting killer thread" ); |
---|
[409] | 195 | |
---|
[566] | 196 | #if (DEBUG_SYS_THREAD_JOIN & 1) |
---|
| 197 | if( DEBUG_SYS_THREAD_JOIN < tm_start ) |
---|
| 198 | printk("\n[DBG] %s : thread %x in process %x / joining thread resume\n", |
---|
| 199 | __FUNCTION__ , joining_ptr->trdid, process->pid ); |
---|
| 200 | #endif |
---|
[436] | 201 | // restore IRQs |
---|
| 202 | hal_restore_irq( save_sr ); |
---|
[23] | 203 | } |
---|
| 204 | |
---|
[438] | 205 | #if DEBUG_SYS_THREAD_JOIN |
---|
[436] | 206 | tm_end = hal_get_cycles(); |
---|
[438] | 207 | if( DEBUG_SYS_THREAD_JOIN < tm_end ) |
---|
[566] | 208 | printk("\n[DBG] %s : thread %x in process %x exit / target thread %x / cycle %d\n", |
---|
| 209 | __FUNCTION__, joining_ptr->trdid, process->pid, (uint32_t)tm_end ); |
---|
[436] | 210 | #endif |
---|
| 211 | |
---|
[23] | 212 | return 0; |
---|
| 213 | |
---|
| 214 | } // end sys_thread_join() |
---|