Ignore:
Timestamp:
May 3, 2018, 5:51:22 PM (6 years ago)
Author:
alain
Message:

1/ Fix a bug in the Multithreaded "sort" applicationr:
The pthread_create() arguments must be declared as global variables.
2/ The exit syscall can be called by any thread of a process..

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/kernel/kern/thread.c

    r438 r440  
    184184    thread->blocked         = THREAD_BLOCKED_GLOBAL;
    185185
    186     // reset children list
    187     xlist_root_init( XPTR( local_cxy , &thread->children_root ) );
    188     thread->children_nr = 0;
    189 
    190     // reset sched list and brothers list
     186    // reset sched list
    191187    list_entry_init( &thread->sched_list );
    192     xlist_entry_init( XPTR( local_cxy , &thread->brothers_list ) );
    193188
    194189    // reset thread info
     
    238233    // get process descriptor local copy
    239234    process = process_get_local_copy( pid );
     235
    240236    if( process == NULL )
    241237    {
     
    604600///////////////////////////////////////////////////////////////////////////////////////
    605601// TODO: check that all memory dynamically allocated during thread execution
    606 // has been released, using a cache of mmap and malloc requests. [AG]
     602// has been released, using a cache of mmap requests. [AG]
    607603///////////////////////////////////////////////////////////////////////////////////////
    608604void thread_destroy( thread_t * thread )
     
    619615__FUNCTION__, CURRENT_THREAD, thread, process->pid, cycle );
    620616#endif
    621 
    622     assert( (thread->children_nr == 0) , __FUNCTION__ , "still attached children" );
    623617
    624618    assert( (thread->local_locks == 0) , __FUNCTION__ , "all local locks not released" );
     
    663657}   // end thread_destroy()
    664658
    665 /////////////////////////////////////////////////
    666 void thread_child_parent_link( xptr_t  xp_parent,
    667                                xptr_t  xp_child )
    668 {
    669     // get extended pointers on children list root
    670     cxy_t      parent_cxy = GET_CXY( xp_parent );
    671     thread_t * parent_ptr = (thread_t *)GET_PTR( xp_parent );
    672     xptr_t     root       = XPTR( parent_cxy , &parent_ptr->children_root );
    673 
    674     // get extended pointer on children list entry
    675     cxy_t      child_cxy  = GET_CXY( xp_child );
    676     thread_t * child_ptr  = (thread_t *)GET_PTR( xp_child );
    677     xptr_t     entry      = XPTR( child_cxy , &child_ptr->brothers_list );
    678 
    679     // set the link
    680     xlist_add_first( root , entry );
    681     hal_remote_atomic_add( XPTR( parent_cxy , &parent_ptr->children_nr ) , 1 );
    682 
    683 }  // end thread_child_parent_link()
    684 
    685 ///////////////////////////////////////////////////
    686 void thread_child_parent_unlink( xptr_t  xp_parent,
    687                                  xptr_t  xp_child )
    688 {
    689     // get extended pointer on children list lock
    690     cxy_t      parent_cxy = GET_CXY( xp_parent );
    691     thread_t * parent_ptr = (thread_t *)GET_PTR( xp_parent );
    692     xptr_t     lock       = XPTR( parent_cxy , &parent_ptr->children_lock );
    693 
    694     // get extended pointer on children list entry
    695     cxy_t      child_cxy  = GET_CXY( xp_child );
    696     thread_t * child_ptr  = (thread_t *)GET_PTR( xp_child );
    697     xptr_t     entry      = XPTR( child_cxy , &child_ptr->brothers_list );
    698 
    699     // get the lock
    700     remote_spinlock_lock( lock );
    701 
    702     // remove the link
    703     xlist_unlink( entry );
    704     hal_remote_atomic_add( XPTR( parent_cxy , &parent_ptr->children_nr ) , -1 );
    705 
    706     // release the lock
    707     remote_spinlock_unlock( lock );
    708 
    709 }  // thread_child_parent_unlink()
    710 
    711659//////////////////////////////////////////////////
    712660inline void thread_set_req_ack( thread_t * target,
     
    846794
    847795}  // end thread_unblock()
     796
     797/*
    848798
    849799////////////////////////////////////
     
    875825    process_t * target_process;         // pointer on target thread process
    876826
    877     // get target thread cluster and pointer
     827    // get target thread pointer and cluster
    878828    target_cxy = GET_CXY( target_xp );
    879829    target_ptr = GET_PTR( target_xp );
     
    883833    killer_xp  = XPTR( local_cxy , killer_ptr );
    884834
    885 #if DEBUG_THREAD_KILL
     835#if DEBUG_THREAD_DELETE
    886836uint32_t cycle  = (uint32_t)hal_get_cycles;
    887 if( DEBUG_THREAD_KILL < cycle )
     837if( DEBUG_THREAD_DELETE < cycle )
    888838printk("\n[DBG] %s : thread %x enter for target thread %x / cycle %d\n",
    889839__FUNCTION__, killer_ptr, target_ptr, cycle );
     
    982932        else          hal_remote_atomic_or( process_state_xp , PROCESS_TERM_KILL );
    983933
    984 #if DEBUG_THREAD_KILL
     934#if DEBUG_THREAD_DELETE
    985935cycle  = (uint32_t)hal_get_cycles;
    986 if( DEBUG_THREAD_KILL < cycle )
     936if( DEBUG_THREAD_DELETE < cycle )
    987937printk("\n[DBG] %s : thread %x exit for thread %x / main thread / cycle %d\n",
    988938__FUNCTION__, killer_ptr, target_ptr, cycle );
     
    995945        hal_remote_atomic_or( target_flags_xp , THREAD_FLAG_REQ_DELETE );
    996946
    997 #if DEBUG_THREAD_KILL
     947#if DEBUG_THREAD_DELETE
    998948cycle  = (uint32_t)hal_get_cycles;
    999 if( DEBUG_THREAD_KILL < cycle )
     949if( DEBUG_THREAD_DELETE < cycle )
    1000950printk("\n[DBG] %s : thread %x exit for thread %x / not the main thread / cycle %d\n",
    1001951__FUNCTION__, killer_ptr, target_ptr, cycle );
     
    1005955
    1006956}  // end thread_kill()
     957
     958*/
     959
     960//////////////////////////////////////
     961void thread_delete( xptr_t  target_xp,
     962                    pid_t   pid,
     963                    bool_t  is_forced )
     964{
     965    reg_t       save_sr;                // for critical section
     966    bool_t      target_join_done;       // joining thread arrived first
     967    bool_t      target_attached;        // target thread attached
     968    xptr_t      killer_xp;              // extended pointer on killer thread (this)
     969    thread_t  * killer_ptr;             // pointer on killer thread (this)
     970    cxy_t       target_cxy;             // target thread cluster     
     971    thread_t  * target_ptr;             // pointer on target thread
     972    xptr_t      target_flags_xp;        // extended pointer on target thread <flags>
     973    uint32_t    target_flags;           // target thread <flags> value
     974    xptr_t      target_join_lock_xp;    // extended pointer on target thread <join_lock>
     975    xptr_t      target_join_xp_xp;      // extended pointer on target thread <join_xp>
     976    trdid_t     target_trdid;           // target thread identifier
     977    ltid_t      target_ltid;            // target thread local index
     978    xptr_t      joining_xp;             // extended pointer on joining thread
     979    thread_t  * joining_ptr;            // pointer on joining thread
     980    cxy_t       joining_cxy;            // joining thread cluster
     981    cxy_t       owner_cxy;              // process owner cluster
     982
     983
     984    // get target thread pointers, identifiers, and flags
     985    target_cxy      = GET_CXY( target_xp );
     986    target_ptr      = GET_PTR( target_xp );
     987    target_trdid    = hal_remote_lw( XPTR( target_cxy , &target_ptr->trdid ) );
     988    target_ltid     = LTID_FROM_TRDID( target_trdid );
     989    target_flags_xp = XPTR( target_cxy , &target_ptr->flags );
     990    target_flags    = hal_remote_lw( target_flags_xp );
     991
     992    // get killer thread pointers
     993    killer_ptr = CURRENT_THREAD;
     994    killer_xp  = XPTR( local_cxy , killer_ptr );
     995
     996#if DEBUG_THREAD_DELETE
     997uint32_t cycle  = (uint32_t)hal_get_cycles;
     998if( DEBUG_THREAD_DELETE < cycle )
     999printk("\n[DBG] %s : killer thread %x enter for target thread %x / cycle %d\n",
     1000__FUNCTION__, killer_ptr, target_ptr, cycle );
     1001#endif
     1002
     1003    // target thread cannot be the main thread, because the main thread
     1004    // must be deleted by the parent process sys_wait() function
     1005    owner_cxy = CXY_FROM_PID( pid );
     1006    assert( ((owner_cxy != target_cxy) || (target_ltid != 0)), __FUNCTION__,
     1007    "tharget thread cannot be the main thread\n" );
     1008
     1009    // block the target thread
     1010    thread_block( target_xp , THREAD_BLOCKED_GLOBAL );
     1011
     1012    // get attached from target flag descriptor
     1013    target_attached = ((hal_remote_lw( target_flags_xp ) & THREAD_FLAG_DETACHED) != 0);
     1014
     1015    // synchronize with the joining thread if the target thread is attached
     1016    if( target_attached && (is_forced == false) )
     1017    {
     1018        // build extended pointers on target thread join fields
     1019        target_join_lock_xp  = XPTR( target_cxy , &target_ptr->join_lock );
     1020        target_join_xp_xp    = XPTR( target_cxy , &target_ptr->join_xp );
     1021
     1022        // enter critical section
     1023        hal_disable_irq( &save_sr );
     1024
     1025        // take the join_lock in target thread descriptor
     1026        remote_spinlock_lock( target_join_lock_xp );
     1027
     1028        // get join_done from target thread descriptor
     1029        target_join_done = ((hal_remote_lw( target_flags_xp ) & THREAD_FLAG_JOIN_DONE) != 0);
     1030   
     1031        if( target_join_done )  // joining thread arrived first => unblock the joining thread
     1032        {
     1033            // get extended pointer on joining thread
     1034            joining_xp  = (xptr_t)hal_remote_lwd( target_join_xp_xp );
     1035            joining_ptr = GET_PTR( joining_xp );
     1036            joining_cxy = GET_CXY( joining_xp );
     1037           
     1038            // reset the join_done flag in target thread
     1039            hal_remote_atomic_and( target_flags_xp , ~THREAD_FLAG_JOIN_DONE );
     1040
     1041            // unblock the joining thread
     1042            thread_unblock( joining_xp , THREAD_BLOCKED_JOIN );
     1043
     1044            // release the join_lock in target thread descriptor
     1045            remote_spinlock_unlock( target_join_lock_xp );
     1046
     1047            // restore IRQs
     1048            hal_restore_irq( save_sr );
     1049        }
     1050        else                // this thread arrived first => register flags and deschedule
     1051        {
     1052            // set the kill_done flag in target thread
     1053            hal_remote_atomic_or( target_flags_xp , THREAD_FLAG_KILL_DONE );
     1054
     1055            // block this thread on BLOCKED_JOIN
     1056            thread_block( killer_xp , THREAD_BLOCKED_JOIN );
     1057
     1058            // set extended pointer on killer thread in target thread
     1059            hal_remote_swd( target_join_xp_xp , killer_xp );
     1060
     1061            // release the join_lock in target thread descriptor
     1062            remote_spinlock_unlock( target_join_lock_xp );
     1063
     1064            // deschedule
     1065            sched_yield( "killer thread wait joining thread" );
     1066
     1067            // restore IRQs
     1068            hal_restore_irq( save_sr );
     1069        }
     1070    }  // end if attached
     1071
     1072    // set the REQ_DELETE flag in target thread descriptor
     1073    hal_remote_atomic_or( target_flags_xp , THREAD_FLAG_REQ_DELETE );
     1074
     1075#if DEBUG_THREAD_DELETE
     1076cycle  = (uint32_t)hal_get_cycles;
     1077if( DEBUG_THREAD_DELETE < cycle )
     1078printk("\n[DBG] %s : killer thread %x exit for target thread %x / cycle %d\n",
     1079__FUNCTION__, killer_ptr, target_ptr, cycle );
     1080#endif
     1081
     1082}  // end thread_delete()
     1083
     1084
    10071085
    10081086///////////////////////
Note: See TracChangeset for help on using the changeset viewer.