Ignore:
Timestamp:
Nov 1, 2018, 12:10:42 PM (6 years ago)
Author:
alain
Message:

Improve signals.

File:
1 edited

Legend:

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

    r581 r583  
    224224
    225225        // update DQDT
    226     dqdt_update_threads( 1 );
     226    dqdt_increment_threads();
    227227
    228228#if DEBUG_THREAD_INIT
     
    768768    hal_cpu_context_init( thread );
    769769
     770    // set THREAD_BLOCKED_IDLE for DEV threads
     771    if( type == THREAD_DEV ) thread->blocked |= THREAD_BLOCKED_IDLE;
    770772
    771773#if DEBUG_THREAD_KERNEL_CREATE
     
    815817///////////////////////////////////////////////////////////////////////////////////////
    816818// TODO: check that all memory dynamically allocated during thread execution
    817 // has been released, using a cache of mmap requests. [AG]
     819// has been released => check vmm destroy for MMAP vsegs [AG]
    818820///////////////////////////////////////////////////////////////////////////////////////
    819 bool_t thread_destroy( thread_t * thread )
     821void thread_destroy( thread_t * thread )
    820822{
    821823    reg_t        save_sr;
    822     bool_t       last_thread;
    823824
    824825    process_t  * process    = thread->process;
     
    826827
    827828#if DEBUG_THREAD_DESTROY
    828 uint32_t cycle = (uint32_t)hal_get_cycles();
     829uint32_t   cycle = (uint32_t)hal_get_cycles();
     830thread_t * this  = CURRENT_THREAD;
    829831if( DEBUG_THREAD_DESTROY < cycle )
    830 printk("\n[DBG] %s : thread %x enter to destroy thread %x in process %x / cycle %d\n",
    831 __FUNCTION__, CURRENT_THREAD, thread->trdid, process->pid, cycle );
    832 #endif
    833 
    834 // check busylocks counter
    835 assert( (thread->busylocks == 0) ,
    836 "busylock not released for thread %x in process %x", thread->trdid, process->pid );
     832printk("\n[DBG] %s : thread[%x,%x] enter to destroy thread[%x,%x] / cycle %d\n",
     833__FUNCTION__, this->process->pid, this->trdid, process->pid, thread->trdid, cycle );
     834#endif
     835
     836    // check busylocks counter
     837    thread_assert_can_yield( thread , __FUNCTION__ );
    837838
    838839    // update intrumentation values
     
    852853        hal_restore_irq( save_sr );
    853854
    854     // remove thread from process th_tbl[]
    855     last_thread = process_remove_thread( thread );
    856        
    857     // update DQDT
    858     dqdt_update_threads( -1 );
    859 
    860855    // invalidate thread descriptor
    861856        thread->signature = 0;
     
    867862cycle = (uint32_t)hal_get_cycles();
    868863if( DEBUG_THREAD_DESTROY < cycle )
    869 printk("\n[DBG] %s : thread %x exit / destroyed thread %x in process %x / last %d / cycle %d\n",
    870 __FUNCTION__, CURRENT_THREAD, thread->trdid, process->pid, last_thread / cycle );
    871 #endif
    872 
    873     return last_thread;
     864printk("\n[DBG] %s : thread[%x,%x] exit / destroyed thread[%x,%x] / cycle %d\n",
     865__FUNCTION__, this->process->pid, this->trdid, process->pid, thread->trdid, cycle );
     866#endif
    874867
    875868}   // end thread_destroy()
     
    10231016uint32_t cycle  = (uint32_t)hal_get_cycles();
    10241017if( DEBUG_THREAD_DELETE < cycle )
    1025 printk("\n[DBG] %s : thread %x in process %x enters / target thread %x / cycle %d\n",
    1026 __FUNCTION__, killer_ptr->trdid, killer_ptr->process->pid, target_ptr->trdid, cycle );
    1027 #endif
    1028 
    1029 // check killer thread can yield
    1030 assert( (killer_ptr->busylocks == 0),
    1031 "cannot yield : busylocks = %d\n", killer_ptr->busylocks );
     1018printk("\n[DBG] %s : killer[%x,%x] enters / target[%x,%x] / cycle %d\n",
     1019__FUNCTION__, killer_ptr->process->pid, killer_ptr->trdid, 
     1020target_ptr->process->pid, target_ptr->trdid, cycle );
     1021#endif
    10321022
    10331023// check target thread is not the main thread, because the main thread
     
    10361026"tharget thread cannot be the main thread\n" );
    10371027
    1038     // block the target thread
    1039     thread_block( target_xp , THREAD_BLOCKED_GLOBAL );
    1040 
    1041     // synchronize with the joining thread if attached
    1042     if( target_attached && (is_forced == false) )
    1043     {
    1044 
    1045 #if (DEBUG_THREAD_DELETE & 1)
    1046 if( DEBUG_THREAD_DELETE < cycle )
    1047 printk("\n[DBG] %s : thread %x in process %x / target thread is attached\n",
    1048 __FUNCTION__, killer_ptr->trdid, killer_ptr->process->pid );
    1049 #endif
     1028    // check killer thread can yield
     1029    thread_assert_can_yield( killer_ptr , __FUNCTION__ );
     1030
     1031    // if the target thread is attached, we must synchonize with the joining thread
     1032    // before blocking and marking the target thead for delete.
     1033
     1034    if( target_attached && (is_forced == false) ) // synchronize with joining thread
     1035    {
    10501036        // build extended pointers on target thread join fields
    10511037        target_join_lock_xp  = XPTR( target_cxy , &target_ptr->join_lock );
     
    10611047        target_join_done = ((hal_remote_l32( target_flags_xp ) & THREAD_FLAG_JOIN_DONE) != 0);
    10621048   
    1063         if( target_join_done )  // joining thread arrived first => unblock the joining thread
     1049        if( target_join_done )                     // joining thread arrived first
    10641050        {
    1065 
    1066 #if (DEBUG_THREAD_DELETE & 1)
    1067 if( DEBUG_THREAD_DELETE < cycle )
    1068 printk("\n[DBG] %s : thread %x in process %x / joining thread arrived first\n",
    1069 __FUNCTION__, killer_ptr->trdid, killer_ptr->process->pid );
    1070 #endif
    10711051            // get extended pointer on joining thread
    10721052            joining_xp  = (xptr_t)hal_remote_l64( target_join_xp_xp );
     
    10831063            remote_busylock_release( target_join_lock_xp );
    10841064
     1065            // block the target thread
     1066            thread_block( target_xp , THREAD_BLOCKED_GLOBAL );
     1067
    10851068            // set the REQ_DELETE flag in target thread descriptor
    10861069            hal_remote_atomic_or( target_flags_xp , THREAD_FLAG_REQ_DELETE );
    10871070
    1088             // restore IRQs
     1071            // exit critical section
    10891072            hal_restore_irq( save_sr );
    1090         }
    1091         else                // killer thread arrived first => register flags and deschedule
    1092         {
    1093 
    1094 #if (DEBUG_THREAD_DELETE & 1)
    1095 if( DEBUG_THREAD_DELETE < cycle )
    1096 printk("\n[DBG] %s : thread %x in process %x / killer thread arrived first\n",
    1097 __FUNCTION__, killer_ptr->trdid, killer_ptr->process->pid );
    1098 #endif
    1099             // set the kill_done flag in target thread
    1100             hal_remote_atomic_or( target_flags_xp , THREAD_FLAG_KILL_DONE );
    1101 
    1102             // block this thread on BLOCKED_JOIN
    1103             thread_block( killer_xp , THREAD_BLOCKED_JOIN );
    1104 
    1105             // set extended pointer on killer thread in target thread
    1106             hal_remote_s64( target_join_xp_xp , killer_xp );
    1107 
    1108             // release the join_lock in target thread descriptor
    1109             remote_busylock_release( target_join_lock_xp );
    1110 
    1111 #if (DEBUG_THREAD_DELETE & 1)
    1112 if( DEBUG_THREAD_DELETE < cycle )
    1113 printk("\n[DBG] %s : thread %x in process %x / killer thread deschedule\n",
    1114 __FUNCTION__, killer_ptr->trdid, killer_ptr->process->pid );
    1115 #endif
    1116             // deschedule
    1117             sched_yield( "killer thread wait joining thread" );
    1118 
    1119 #if (DEBUG_THREAD_DELETE & 1)
    1120 if( DEBUG_THREAD_DELETE < cycle )
    1121 printk("\n[DBG] %s : thread %x in process %x / killer thread resume\n",
    1122 __FUNCTION__, killer_ptr->trdid, killer_ptr->process->pid );
    1123 #endif
    1124             // set the REQ_DELETE flag in target thread descriptor
    1125             hal_remote_atomic_or( target_flags_xp , THREAD_FLAG_REQ_DELETE );
    1126 
    1127             // restore IRQs
    1128             hal_restore_irq( save_sr );
    1129         }
    1130     }
    1131     else                                                   // target thread not attached
    1132     {
    1133         // set the REQ_DELETE flag in target thread descriptor
    1134         hal_remote_atomic_or( target_flags_xp , THREAD_FLAG_REQ_DELETE );
    1135     }
    11361073
    11371074#if DEBUG_THREAD_DELETE
    11381075cycle  = (uint32_t)hal_get_cycles;
    11391076if( DEBUG_THREAD_DELETE < cycle )
    1140 printk("\n[DBG] %s : thread %x in process %x exit / target thread %x / cycle %d\n",
    1141 __FUNCTION__, killer_ptr->trdid, killer_ptr->process->pid, target_ptr->trdid, cycle );
    1142 #endif
     1077printk("\n[DBG] %s : killer[%x,%x] exit / target[%x,%x] marked after join / cycle %d\n",
     1078__FUNCTION__, killer_ptr->process->pid, killer_ptr->trdid,
     1079target_ptr->process->pid, target_ptr->trdid, cycle );
     1080#endif
     1081
     1082        }
     1083        else                                      // killer thread arrived first
     1084        {
     1085            // set the kill_done flag in target thread
     1086            hal_remote_atomic_or( target_flags_xp , THREAD_FLAG_KILL_DONE );
     1087
     1088            // block this thread on BLOCKED_JOIN
     1089            thread_block( killer_xp , THREAD_BLOCKED_JOIN );
     1090
     1091            // set extended pointer on killer thread in target thread
     1092            hal_remote_s64( target_join_xp_xp , killer_xp );
     1093
     1094            // release the join_lock in target thread descriptor
     1095            remote_busylock_release( target_join_lock_xp );
     1096
     1097#if DEBUG_THREAD_DELETE
     1098cycle  = (uint32_t)hal_get_cycles;
     1099if( DEBUG_THREAD_DELETE < cycle )
     1100printk("\n[DBG] %s : killer[%x,%x] deschedules / target[%x,%x] not completed / cycle %d\n",
     1101__FUNCTION__, killer_ptr->process->pid, killer_ptr->trdid,
     1102target_ptr->process->pid, target_ptr->trdid, cycle );
     1103#endif
     1104            // deschedule
     1105            sched_yield( "killer thread wait joining thread" );
     1106
     1107            // block the target thread
     1108            thread_block( target_xp , THREAD_BLOCKED_GLOBAL );
     1109
     1110            // set the REQ_DELETE flag in target thread descriptor
     1111            hal_remote_atomic_or( target_flags_xp , THREAD_FLAG_REQ_DELETE );
     1112
     1113            // exit critical section
     1114            hal_restore_irq( save_sr );
     1115
     1116#if DEBUG_THREAD_DELETE
     1117cycle  = (uint32_t)hal_get_cycles;
     1118if( DEBUG_THREAD_DELETE < cycle )
     1119printk("\n[DBG] %s : killer[%x,%x] exit / target[%x,%x] marked after join / cycle %d\n",
     1120__FUNCTION__, killer_ptr->process->pid, killer_ptr->trdid,
     1121target_ptr->process->pid, target_ptr->trdid, cycle );
     1122#endif
     1123
     1124        }
     1125    }
     1126    else                     // no synchronization with joining thread required
     1127    {
     1128        // block the target thread
     1129        thread_block( target_xp , THREAD_BLOCKED_GLOBAL );
     1130
     1131        // set the REQ_DELETE flag in target thread descriptor
     1132        hal_remote_atomic_or( target_flags_xp , THREAD_FLAG_REQ_DELETE );
     1133
     1134#if DEBUG_THREAD_DELETE
     1135cycle  = (uint32_t)hal_get_cycles;
     1136if( DEBUG_THREAD_DELETE < cycle )
     1137printk("\n[DBG] %s : killer[%x,%x] exit / target [%x,%x] marked / no join / cycle %d\n",
     1138__FUNCTION__, killer_ptr->process->pid, killer_ptr->trdid,
     1139target_ptr->process->pid, target_ptr->trdid, cycle );
     1140#endif
     1141
     1142    }
    11431143
    11441144}  // end thread_delete()
     
    11551155
    11561156        // force core to low-power mode (optional)
    1157         if( CONFIG_THREAD_IDLE_MODE_SLEEP )
     1157        if( CONFIG_SCHED_IDLE_MODE_SLEEP )
    11581158        {
    11591159
     
    13541354#if DEBUG_BUSYLOCK
    13551355
    1356 // get root of list of taken busylocks
     1356// scan list of busylocks
     1357xptr_t    iter_xp;
    13571358xptr_t    root_xp  = XPTR( local_cxy , &thread->busylocks_root );
    1358 xptr_t    iter_xp;
    1359 
    1360 // scan list of busylocks
    13611359XLIST_FOREACH( root_xp , iter_xp )
    13621360{
Note: See TracChangeset for help on using the changeset viewer.