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

Last change on this file since 669 was 651, checked in by alain, 5 years ago

1) Improve the VMM MMAP allocator: implement the "buddy" algorithm
to allocate only aligned blocks.
2) fix a bug in the pthread_join() / pthread_exit() mmechanism.

File size: 7.1 KB
RevLine 
[1]1/*
[651]2 * sys_thread_join.c - wait termination of of an attached thread.
[1]3 *
[637]4 * Authors    Alain Greiner (2016,2017,2018,2019)
[23]5 *
[637]6 * Copyright (c) UPMC Sorbonne Universites
[1]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>
[651]28#include <hal_uspace.h>
[1]29#include <thread.h>
[23]30#include <vmm.h>
[1]31#include <scheduler.h>
32#include <errno.h>
[23]33#include <printk.h>
[566]34#include <remote_busylock.h>
[1]35
[506]36#include <syscalls.h>
37
[23]38///////////////////////////////////////
39int sys_thread_join ( trdid_t    trdid,
[651]40                      void    ** exit_status )
[1]41{
[651]42    error_t       error;
[436]43    reg_t         save_sr;
[23]44    xptr_t        target_xp;
45    thread_t    * target_ptr;
46    cxy_t         target_cxy;
47    ltid_t        target_ltid;
[436]48    xptr_t        target_join_lock_xp;
49    xptr_t        target_flags_xp;
50    xptr_t        target_join_xp_xp;
[651]51    xptr_t        target_exit_status_xp;
[436]52    xptr_t        killer_xp;
53    xptr_t        joining_xp;
54    thread_t    * joining_ptr;
55    process_t   * process;
[651]56    vseg_t      * vseg;
57    void        * status;
[1]58
[436]59    // get joining thread pointers
60        joining_ptr = CURRENT_THREAD;
61    joining_xp  = XPTR( local_cxy , joining_ptr );
62    process     = joining_ptr->process;
[1]63
[23]64    // get target thread ltid and cxy
65    target_ltid = LTID_FROM_TRDID( trdid );
66    target_cxy  = CXY_FROM_TRDID( trdid );
67
[438]68#if DEBUG_SYS_THREAD_JOIN
[651]69uint64_t  tm_start = hal_get_cycles();
[438]70if( DEBUG_SYS_THREAD_JOIN < tm_start )
[651]71printk("\n[%s] joining thread[%x,%x] enter / target thread[%x,%x] / cycle %d\n",
[584]72__FUNCTION__ , process->pid, joining_ptr->trdid,
73process->pid, trdid, (uint32_t)tm_start );
[436]74#endif
75
[23]76    // check trdid argument
[637]77        if( (target_ltid >= CONFIG_THREADS_MAX_PER_CLUSTER) || 
78        (cluster_is_active(target_cxy) == false) )
[1]79        {
[436]80
[438]81#if DEBUG_SYSCALLS_ERROR
[651]82printk("\n[ERROR] in %s : illegal trdid argument %x / joining thread[%x,%x]\n",
83__FUNCTION__, trdid, joining_ptr->process->pid, joining_ptr-trdid );
[436]84#endif
85                joining_ptr->errno = EINVAL;
[23]86                return -1;
[1]87        }
88
[651]89    // check exit_value argument in user space
90    error = vmm_get_vseg( process , (intptr_t)exit_status  , &vseg );
91        if( error )
[1]92        {
[436]93
[438]94#if DEBUG_SYSCALLS_ERROR
[651]95printk("\n[ERROR] in %s : exit_status argument %x not mapped / joining thread[%x,%x]\n",
96__FUNCTION__, exit_status, joining_ptr->process->pid, joining_ptr-trdid );
[436]97#endif
98                joining_ptr->errno = EINVAL;
[23]99                return -1;
[1]100        }
101
[23]102    // check target thread != this thread
[436]103    if( joining_ptr->trdid == trdid )
[23]104    {
[436]105
[438]106#if DEBUG_SYSCALLS_ERROR
[651]107printk("\n[ERROR] in %s : joinig thread[%x,%x] == target thread\n",
108__FUNCTION__, joining_ptr->process->pid, joining_ptr->trdid );
[436]109#endif
110        joining_ptr->errno = EDEADLK;
[23]111        return -1;
112    }
[1]113
[436]114    // get pointers on target thread
[23]115        target_xp  = thread_get_xptr( process->pid , trdid );
[436]116    target_ptr = GET_PTR( target_xp );
[1]117
[23]118    if( target_xp == XPTR_NULL )
119    {
[436]120
[438]121#if DEBUG_SYSCALLS_ERROR
[651]122printk("\n[ERROR] in %s : target thread[%x,%x] not found / joining_thread[%x,%x]\n",
123__FUNCTION__, process->pid, trdid, joining_ptr->process->pid, joining_ptr-trdid );
[436]124#endif
125        joining_ptr->errno = ESRCH;
[23]126        return -1;
127    }
[1]128
[436]129    // get extended pointers on various fields in target thread
[651]130    target_join_lock_xp   = XPTR( target_cxy , &target_ptr->join_lock );
131    target_flags_xp       = XPTR( target_cxy , &target_ptr->flags );
132    target_join_xp_xp     = XPTR( target_cxy , &target_ptr->join_xp );
133    target_exit_status_xp = XPTR( target_cxy , &target_ptr->exit_status );
[1]134
[23]135    // check target thread joinable
[566]136    if( (hal_remote_l32( target_flags_xp ) & THREAD_FLAG_DETACHED) != 0 )
[23]137    {
[436]138
[438]139#if DEBUG_SYSCALLS_ERROR
[651]140printk("\n[ERROR] in %s : target thread[%x,‰x] not attached / joining thread[%x,%x]\n",
141__FUNCTION__, process->pid, trdid, joining_ptr->process->pid, joining_ptr-trdid );
[436]142#endif
143        joining_ptr->errno = EINVAL; 
[23]144        return -1;
145    }
[1]146
[436]147    // mask IRQs
148    hal_disable_irq( &save_sr );
[1]149
[409]150    // get the lock protecting the join in target thread
[566]151    remote_busylock_acquire( target_join_lock_xp );
[409]152
[436]153    // test the kill_done flag from the target thread
[651]154    if( hal_remote_l32( target_flags_xp ) & THREAD_FLAG_KILL_DONE )  // killer is first
[23]155    {
[651]156        // get exit_status from target thread
157        status = (void*)hal_remote_lpt( target_exit_status_xp );
158
[436]159        // get pointers on killer thread
[566]160        killer_xp  = (xptr_t)hal_remote_l64( target_join_xp_xp );
[1]161
[436]162        // reset the kill_done flag in target thread
163        hal_remote_atomic_and( target_flags_xp , ~THREAD_FLAG_KILL_DONE );
[1]164
[436]165        // unblock the killer thread
166        thread_unblock( killer_xp , THREAD_BLOCKED_JOIN );
167
168        // release the lock protecting join     
[566]169        remote_busylock_release( target_join_lock_xp );
[409]170    }
[651]171    else                                                          // joining 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 )
[651]187printk("\n[%s] joining thread[%x,%x] deschedules / target thread[%x,%x] not completed\n",
[584]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" );
[651]192
193        // returns exit_status from joining thread
194        status = joining_ptr->exit_status;
195    }
[409]196   
[651]197    // returns exit_status to user space
198    hal_copy_to_uspace( exit_status,
199                        XPTR( local_cxy , &status ),
200                        sizeof( void* ) );
201
202    // restore IRQs
203    hal_restore_irq( save_sr );
204
205    hal_fence();
206
207#if (DEBUG_SYS_THREAD_JOIN || CONFIG_INSTRUMENTATION_SYSCALLS)
208uint64_t     tm_end = hal_get_cycles();
209#endif
210
211#if CONFIG_INSTRUMENTATION_SYSCALLS
212hal_atomic_add( &syscalls_cumul_cost[SYS_THREAD_JOIN] , tm_end - tm_start );
213hal_atomic_add( &syscalls_occurences[SYS_THREAD_JOIN] , 1 );
214#endif
215
[584]216#if DEBUG_SYS_THREAD_JOIN
[436]217tm_end = hal_get_cycles();
[438]218if( DEBUG_SYS_THREAD_JOIN < tm_end )
[651]219printk("\n[%s] joining thread[%x,%x] exit / target thread[%x,%x] completed / cycle %d\n",
[584]220__FUNCTION__ , process->pid, joining_ptr->trdid, process->pid, trdid, (uint32_t)tm_end );
[436]221#endif
222
[23]223    return 0;
224
225}  // end sys_thread_join()
Note: See TracBrowser for help on using the repository browser.