source: trunk/kernel/syscalls/sys_thread_join.c@ 617

Last change on this file since 617 was 584, checked in by alain, 8 years ago

Introduce sys_place_fork() function.

File size: 6.2 KB
RevLine 
[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///////////////////////////////////////
38int 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]65uint64_t tm_start;
66uint64_t tm_end;
67tm_start = hal_get_cycles();
[438]68if( DEBUG_SYS_THREAD_JOIN < tm_start )
[584]69printk("\n[DBG] %s : joining thread[%x,%x] enter / target thread[%x,%x] / cycle %d\n",
70__FUNCTION__ , process->pid, joining_ptr->trdid,
71process->pid, trdid, (uint32_t)tm_start );
[436]72#endif
73
[23]74 // check trdid argument
[584]75 if( (target_ltid >= CONFIG_THREADS_MAX_PER_CLUSTER) || cluster_is_undefined(target_cxy) )
[1]76 {
[436]77
[438]78#if DEBUG_SYSCALLS_ERROR
[584]79printk("\n[ERROR] in %s : illegal trdid argument %x\n",
80__FUNCTION__, trdid );
[436]81#endif
82 joining_ptr->errno = EINVAL;
[23]83 return -1;
[1]84 }
85
[23]86 // check exit_value argument
[436]87 if( exit_value != NULL )
[1]88 {
[436]89
[438]90#if DEBUG_SYSCALLS_ERROR
[584]91printk("\n[ERROR] in %s : exit_value argument must be NULL\n",
92__FUNCTION__ );
[436]93#endif
94 joining_ptr->errno = EINVAL;
[23]95 return -1;
[1]96 }
97
[23]98 // check target thread != this thread
[436]99 if( joining_ptr->trdid == trdid )
[23]100 {
[436]101
[438]102#if DEBUG_SYSCALLS_ERROR
[473]103printk("\n[ERROR] in %s : this thread (%x) == target thread(%x)\n",
104__FUNCTION__, joining_ptr->trdid, trdid );
[436]105#endif
106 joining_ptr->errno = EDEADLK;
[23]107 return -1;
108 }
[1]109
[436]110 // get pointers on target thread
[23]111 target_xp = thread_get_xptr( process->pid , trdid );
[436]112 target_ptr = GET_PTR( target_xp );
[1]113
[23]114 if( target_xp == XPTR_NULL )
115 {
[436]116
[438]117#if DEBUG_SYSCALLS_ERROR
[584]118printk("\n[ERROR] in %s : target thread %x not found\n",
119__FUNCTION__, trdid );
[436]120#endif
121 joining_ptr->errno = ESRCH;
[23]122 return -1;
123 }
[1]124
[436]125 // get extended pointers on various fields in target thread
126 target_join_lock_xp = XPTR( target_cxy , &target_ptr->join_lock );
127 target_flags_xp = XPTR( target_cxy , &target_ptr->flags );
128 target_blocked_xp = XPTR( target_cxy , &target_ptr->blocked );
129 target_join_xp_xp = XPTR( target_cxy , &target_ptr->join_xp );
[1]130
[23]131 // check target thread joinable
[566]132 if( (hal_remote_l32( target_flags_xp ) & THREAD_FLAG_DETACHED) != 0 )
[23]133 {
[436]134
[438]135#if DEBUG_SYSCALLS_ERROR
[436]136printk("\n[ERROR] in %s : target thread %x not joinable\n", __FUNCTION__, trdid );
137#endif
138 joining_ptr->errno = EINVAL;
[23]139 return -1;
140 }
[1]141
[436]142 // mask IRQs
143 hal_disable_irq( &save_sr );
[1]144
[409]145 // get the lock protecting the join in target thread
[566]146 remote_busylock_acquire( target_join_lock_xp );
[409]147
[436]148 // test the kill_done flag from the target thread
[566]149 if( hal_remote_l32( target_flags_xp ) & THREAD_FLAG_KILL_DONE ) // killer thread is first
[23]150 {
[436]151 // get pointers on killer thread
[566]152 killer_xp = (xptr_t)hal_remote_l64( target_join_xp_xp );
[1]153
[436]154 // reset the kill_done flag in target thread
155 hal_remote_atomic_and( target_flags_xp , ~THREAD_FLAG_KILL_DONE );
[1]156
[436]157 // unblock the killer thread
158 thread_unblock( killer_xp , THREAD_BLOCKED_JOIN );
159
160 // release the lock protecting join
[566]161 remote_busylock_release( target_join_lock_xp );
[436]162
[584]163#if DEBUG_SYS_THREAD_JOIN
164tm_end = hal_get_cycles();
165if( DEBUG_SYS_THREAD_JOIN < tm_end )
166printk("\n[DBG] %s : joining thread[%x,%x] exit / target thread[%x,%x] completed / cycle %d\n",
167__FUNCTION__, process->pid, joining_ptr->trdid, process->pid, trdid, (uint32_t)tm_end );
168#endif
169
[409]170 }
[436]171 else // joining thread is first
[409]172 {
[436]173 // set the join_done flag in target thread
174 hal_remote_atomic_or( target_flags_xp , THREAD_FLAG_JOIN_DONE );
[23]175
[436]176 // block joining thread on BLOCKED_JOIN
177 thread_block( joining_xp , THREAD_BLOCKED_JOIN );
[23]178
[436]179 // register the joining thread extended pointer in target thread
[566]180 hal_remote_s64( target_join_xp_xp , joining_xp );
[23]181
[436]182 // release the lock protecting the join
[566]183 remote_busylock_release( target_join_lock_xp );
[23]184
[584]185#if DEBUG_SYS_THREAD_JOIN
[566]186if( DEBUG_SYS_THREAD_JOIN < tm_start )
[584]187printk("\n[DBG] %s : joining thread[%x,%x] deschedules / target thread[%x,%x] not completed\n",
188__FUNCTION__ , process->pid, joining_ptr->trdid, process->pid, trdid );
[566]189#endif
[409]190 // deschedule
[436]191 sched_yield( "joining thread waiting killer thread" );
[409]192
[584]193#if DEBUG_SYS_THREAD_JOIN
[436]194tm_end = hal_get_cycles();
[438]195if( DEBUG_SYS_THREAD_JOIN < tm_end )
[584]196printk("\n[DBG] %s : joining thread[%x,%x] exit / target thread[%x,%x] completed / cycle %d\n",
197__FUNCTION__ , process->pid, joining_ptr->trdid, process->pid, trdid, (uint32_t)tm_end );
[436]198#endif
199
[584]200 }
201
202 // restore IRQs
203 hal_restore_irq( save_sr );
204
[23]205 return 0;
206
207} // end sys_thread_join()
Note: See TracBrowser for help on using the repository browser.