Ignore:
Timestamp:
Nov 14, 2019, 11:50:09 AM (5 years ago)
Author:
alain
Message:

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:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/kernel/syscalls/sys_thread_join.c

    r637 r651  
    11/*
    2  * sys_thread_join.c - passive wait on the end of a given thread.
     2 * sys_thread_join.c - wait termination of of an attached thread.
    33 *
    44 * Authors    Alain Greiner (2016,2017,2018,2019)
     
    2626#include <hal_special.h>
    2727#include <hal_irqmask.h>
     28#include <hal_uspace.h>
    2829#include <thread.h>
    2930#include <vmm.h>
     
    3738///////////////////////////////////////
    3839int sys_thread_join ( trdid_t    trdid,
    39                       void    ** exit_value )
     40                      void    ** exit_status )
    4041{
     42    error_t       error;
    4143    reg_t         save_sr;
    4244    xptr_t        target_xp;
     
    4749    xptr_t        target_flags_xp;
    4850    xptr_t        target_join_xp_xp;
     51    xptr_t        target_exit_status_xp;
    4952    xptr_t        killer_xp;
    5053    xptr_t        joining_xp;
    5154    thread_t    * joining_ptr;
    5255    process_t   * process;
     56    vseg_t      * vseg;
     57    void        * status;
    5358
    5459    // get joining thread pointers
     
    6267
    6368#if DEBUG_SYS_THREAD_JOIN
    64 uint64_t     tm_start;
    65 uint64_t     tm_end;
    66 tm_start = hal_get_cycles();
     69uint64_t  tm_start = hal_get_cycles();
    6770if( DEBUG_SYS_THREAD_JOIN < tm_start )
    68 printk("\n[DBG] %s : joining thread[%x,%x] enter / target thread[%x,%x] / cycle %d\n",
     71printk("\n[%s] joining thread[%x,%x] enter / target thread[%x,%x] / cycle %d\n",
    6972__FUNCTION__ , process->pid, joining_ptr->trdid,
    7073process->pid, trdid, (uint32_t)tm_start );
     
    7780
    7881#if DEBUG_SYSCALLS_ERROR
    79 printk("\n[ERROR] in %s : illegal trdid argument %x\n",
    80 __FUNCTION__, trdid );
     82printk("\n[ERROR] in %s : illegal trdid argument %x / joining thread[%x,%x]\n",
     83__FUNCTION__, trdid, joining_ptr->process->pid, joining_ptr-trdid );
    8184#endif
    8285                joining_ptr->errno = EINVAL;
     
    8487        }
    8588
    86     // check exit_value argument
    87         if( exit_value != NULL )
     89    // check exit_value argument in user space
     90    error = vmm_get_vseg( process , (intptr_t)exit_status  , &vseg );
     91        if( error )
    8892        {
    8993
    9094#if DEBUG_SYSCALLS_ERROR
    91 printk("\n[ERROR] in %s : exit_value argument must be NULL\n",
    92 __FUNCTION__ );
     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 );
    9397#endif
    9498                joining_ptr->errno = EINVAL;
     
    101105
    102106#if DEBUG_SYSCALLS_ERROR
    103 printk("\n[ERROR] in %s : this thread (%x) == target thread(%x)\n",
    104 __FUNCTION__, joining_ptr->trdid, trdid );
     107printk("\n[ERROR] in %s : joinig thread[%x,%x] == target thread\n",
     108__FUNCTION__, joining_ptr->process->pid, joining_ptr->trdid );
    105109#endif
    106110        joining_ptr->errno = EDEADLK;
     
    116120
    117121#if DEBUG_SYSCALLS_ERROR
    118 printk("\n[ERROR] in %s : target thread %x not found\n",
    119 __FUNCTION__, trdid );
     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 );
    120124#endif
    121125        joining_ptr->errno = ESRCH;
     
    124128
    125129    // 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_join_xp_xp   = XPTR( target_cxy , &target_ptr->join_xp );
     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 );
    129134
    130135    // check target thread joinable
     
    133138
    134139#if DEBUG_SYSCALLS_ERROR
    135 printk("\n[ERROR] in %s : target thread %x not joinable\n", __FUNCTION__, trdid );
     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 );
    136142#endif
    137143        joining_ptr->errno = EINVAL;
     
    146152
    147153    // test the kill_done flag from the target thread
    148     if( hal_remote_l32( target_flags_xp ) & THREAD_FLAG_KILL_DONE )  // killer thread is first
    149     {
     154    if( hal_remote_l32( target_flags_xp ) & THREAD_FLAG_KILL_DONE )  // killer is first
     155    {
     156        // get exit_status from target thread
     157        status = (void*)hal_remote_lpt( target_exit_status_xp );
     158
    150159        // get pointers on killer thread
    151160        killer_xp  = (xptr_t)hal_remote_l64( target_join_xp_xp );
     
    159168        // release the lock protecting join     
    160169        remote_busylock_release( target_join_lock_xp );
    161 
    162 #if DEBUG_SYS_THREAD_JOIN
    163 tm_end = hal_get_cycles();
    164 if( DEBUG_SYS_THREAD_JOIN < tm_end )
    165 printk("\n[DBG] %s : joining thread[%x,%x] exit / target thread[%x,%x] completed / cycle %d\n",
    166 __FUNCTION__, process->pid, joining_ptr->trdid, process->pid, trdid, (uint32_t)tm_end );
    167 #endif
    168 
    169     }
    170     else                                                          // joining thread is first
     170    }
     171    else                                                          // joining is first
    171172    {
    172173        // set the join_done flag in target thread
     
    184185#if DEBUG_SYS_THREAD_JOIN
    185186if( DEBUG_SYS_THREAD_JOIN < tm_start )
    186 printk("\n[DBG] %s : joining thread[%x,%x] deschedules / target thread[%x,%x] not completed\n",
     187printk("\n[%s] joining thread[%x,%x] deschedules / target thread[%x,%x] not completed\n",
    187188__FUNCTION__ , process->pid, joining_ptr->trdid, process->pid, trdid );
    188189#endif
    189190        // deschedule
    190191        sched_yield( "joining thread waiting killer thread" );
     192
     193        // returns exit_status from joining thread
     194        status = joining_ptr->exit_status;
     195    }
    191196   
     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
    192216#if DEBUG_SYS_THREAD_JOIN
    193217tm_end = hal_get_cycles();
    194218if( DEBUG_SYS_THREAD_JOIN < tm_end )
    195 printk("\n[DBG] %s : joining thread[%x,%x] exit / target thread[%x,%x] completed / cycle %d\n",
     219printk("\n[%s] joining thread[%x,%x] exit / target thread[%x,%x] completed / cycle %d\n",
    196220__FUNCTION__ , process->pid, joining_ptr->trdid, process->pid, trdid, (uint32_t)tm_end );
    197221#endif
    198222
    199     }
    200 
    201     // restore IRQs
    202     hal_restore_irq( save_sr );
    203 
    204223    return 0;
    205224
Note: See TracChangeset for help on using the changeset viewer.