Changeset 583 for trunk/kernel/kern/thread.c
- Timestamp:
- Nov 1, 2018, 12:10:42 PM (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/kernel/kern/thread.c
r581 r583 224 224 225 225 // update DQDT 226 dqdt_ update_threads( 1);226 dqdt_increment_threads(); 227 227 228 228 #if DEBUG_THREAD_INIT … … 768 768 hal_cpu_context_init( thread ); 769 769 770 // set THREAD_BLOCKED_IDLE for DEV threads 771 if( type == THREAD_DEV ) thread->blocked |= THREAD_BLOCKED_IDLE; 770 772 771 773 #if DEBUG_THREAD_KERNEL_CREATE … … 815 817 /////////////////////////////////////////////////////////////////////////////////////// 816 818 // 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] 818 820 /////////////////////////////////////////////////////////////////////////////////////// 819 bool_tthread_destroy( thread_t * thread )821 void thread_destroy( thread_t * thread ) 820 822 { 821 823 reg_t save_sr; 822 bool_t last_thread;823 824 824 825 process_t * process = thread->process; … … 826 827 827 828 #if DEBUG_THREAD_DESTROY 828 uint32_t cycle = (uint32_t)hal_get_cycles(); 829 uint32_t cycle = (uint32_t)hal_get_cycles(); 830 thread_t * this = CURRENT_THREAD; 829 831 if( 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 ); 832 printk("\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__ ); 837 838 838 839 // update intrumentation values … … 852 853 hal_restore_irq( save_sr ); 853 854 854 // remove thread from process th_tbl[]855 last_thread = process_remove_thread( thread );856 857 // update DQDT858 dqdt_update_threads( -1 );859 860 855 // invalidate thread descriptor 861 856 thread->signature = 0; … … 867 862 cycle = (uint32_t)hal_get_cycles(); 868 863 if( 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; 864 printk("\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 874 867 875 868 } // end thread_destroy() … … 1023 1016 uint32_t cycle = (uint32_t)hal_get_cycles(); 1024 1017 if( 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 ); 1018 printk("\n[DBG] %s : killer[%x,%x] enters / target[%x,%x] / cycle %d\n", 1019 __FUNCTION__, killer_ptr->process->pid, killer_ptr->trdid, 1020 target_ptr->process->pid, target_ptr->trdid, cycle ); 1021 #endif 1032 1022 1033 1023 // check target thread is not the main thread, because the main thread … … 1036 1026 "tharget thread cannot be the main thread\n" ); 1037 1027 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 { 1050 1036 // build extended pointers on target thread join fields 1051 1037 target_join_lock_xp = XPTR( target_cxy , &target_ptr->join_lock ); … … 1061 1047 target_join_done = ((hal_remote_l32( target_flags_xp ) & THREAD_FLAG_JOIN_DONE) != 0); 1062 1048 1063 if( target_join_done ) // joining thread arrived first => unblock the joining thread1049 if( target_join_done ) // joining thread arrived first 1064 1050 { 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 #endif1071 1051 // get extended pointer on joining thread 1072 1052 joining_xp = (xptr_t)hal_remote_l64( target_join_xp_xp ); … … 1083 1063 remote_busylock_release( target_join_lock_xp ); 1084 1064 1065 // block the target thread 1066 thread_block( target_xp , THREAD_BLOCKED_GLOBAL ); 1067 1085 1068 // set the REQ_DELETE flag in target thread descriptor 1086 1069 hal_remote_atomic_or( target_flags_xp , THREAD_FLAG_REQ_DELETE ); 1087 1070 1088 // restore IRQs1071 // exit critical section 1089 1072 hal_restore_irq( save_sr ); 1090 }1091 else // killer thread arrived first => register flags and deschedule1092 {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 #endif1099 // set the kill_done flag in target thread1100 hal_remote_atomic_or( target_flags_xp , THREAD_FLAG_KILL_DONE );1101 1102 // block this thread on BLOCKED_JOIN1103 thread_block( killer_xp , THREAD_BLOCKED_JOIN );1104 1105 // set extended pointer on killer thread in target thread1106 hal_remote_s64( target_join_xp_xp , killer_xp );1107 1108 // release the join_lock in target thread descriptor1109 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 #endif1116 // deschedule1117 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 #endif1124 // set the REQ_DELETE flag in target thread descriptor1125 hal_remote_atomic_or( target_flags_xp , THREAD_FLAG_REQ_DELETE );1126 1127 // restore IRQs1128 hal_restore_irq( save_sr );1129 }1130 }1131 else // target thread not attached1132 {1133 // set the REQ_DELETE flag in target thread descriptor1134 hal_remote_atomic_or( target_flags_xp , THREAD_FLAG_REQ_DELETE );1135 }1136 1073 1137 1074 #if DEBUG_THREAD_DELETE 1138 1075 cycle = (uint32_t)hal_get_cycles; 1139 1076 if( 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 1077 printk("\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, 1079 target_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 1098 cycle = (uint32_t)hal_get_cycles; 1099 if( DEBUG_THREAD_DELETE < cycle ) 1100 printk("\n[DBG] %s : killer[%x,%x] deschedules / target[%x,%x] not completed / cycle %d\n", 1101 __FUNCTION__, killer_ptr->process->pid, killer_ptr->trdid, 1102 target_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 1117 cycle = (uint32_t)hal_get_cycles; 1118 if( DEBUG_THREAD_DELETE < cycle ) 1119 printk("\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, 1121 target_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 1135 cycle = (uint32_t)hal_get_cycles; 1136 if( DEBUG_THREAD_DELETE < cycle ) 1137 printk("\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, 1139 target_ptr->process->pid, target_ptr->trdid, cycle ); 1140 #endif 1141 1142 } 1143 1143 1144 1144 } // end thread_delete() … … 1155 1155 1156 1156 // force core to low-power mode (optional) 1157 if( CONFIG_ THREAD_IDLE_MODE_SLEEP )1157 if( CONFIG_SCHED_IDLE_MODE_SLEEP ) 1158 1158 { 1159 1159 … … 1354 1354 #if DEBUG_BUSYLOCK 1355 1355 1356 // get root of list of taken busylocks 1356 // scan list of busylocks 1357 xptr_t iter_xp; 1357 1358 xptr_t root_xp = XPTR( local_cxy , &thread->busylocks_root ); 1358 xptr_t iter_xp;1359 1360 // scan list of busylocks1361 1359 XLIST_FOREACH( root_xp , iter_xp ) 1362 1360 {
Note: See TracChangeset
for help on using the changeset viewer.