Changeset 440 for trunk/kernel/kern/thread.c
- Timestamp:
- May 3, 2018, 5:51:22 PM (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/kernel/kern/thread.c
r438 r440 184 184 thread->blocked = THREAD_BLOCKED_GLOBAL; 185 185 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 191 187 list_entry_init( &thread->sched_list ); 192 xlist_entry_init( XPTR( local_cxy , &thread->brothers_list ) );193 188 194 189 // reset thread info … … 238 233 // get process descriptor local copy 239 234 process = process_get_local_copy( pid ); 235 240 236 if( process == NULL ) 241 237 { … … 604 600 /////////////////////////////////////////////////////////////////////////////////////// 605 601 // TODO: check that all memory dynamically allocated during thread execution 606 // has been released, using a cache of mmap and mallocrequests. [AG]602 // has been released, using a cache of mmap requests. [AG] 607 603 /////////////////////////////////////////////////////////////////////////////////////// 608 604 void thread_destroy( thread_t * thread ) … … 619 615 __FUNCTION__, CURRENT_THREAD, thread, process->pid, cycle ); 620 616 #endif 621 622 assert( (thread->children_nr == 0) , __FUNCTION__ , "still attached children" );623 617 624 618 assert( (thread->local_locks == 0) , __FUNCTION__ , "all local locks not released" ); … … 663 657 } // end thread_destroy() 664 658 665 /////////////////////////////////////////////////666 void thread_child_parent_link( xptr_t xp_parent,667 xptr_t xp_child )668 {669 // get extended pointers on children list root670 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 entry675 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 link680 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 lock690 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 entry695 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 lock700 remote_spinlock_lock( lock );701 702 // remove the link703 xlist_unlink( entry );704 hal_remote_atomic_add( XPTR( parent_cxy , &parent_ptr->children_nr ) , -1 );705 706 // release the lock707 remote_spinlock_unlock( lock );708 709 } // thread_child_parent_unlink()710 711 659 ////////////////////////////////////////////////// 712 660 inline void thread_set_req_ack( thread_t * target, … … 846 794 847 795 } // end thread_unblock() 796 797 /* 848 798 849 799 //////////////////////////////////// … … 875 825 process_t * target_process; // pointer on target thread process 876 826 877 // get target thread cluster and pointer827 // get target thread pointer and cluster 878 828 target_cxy = GET_CXY( target_xp ); 879 829 target_ptr = GET_PTR( target_xp ); … … 883 833 killer_xp = XPTR( local_cxy , killer_ptr ); 884 834 885 #if DEBUG_THREAD_ KILL835 #if DEBUG_THREAD_DELETE 886 836 uint32_t cycle = (uint32_t)hal_get_cycles; 887 if( DEBUG_THREAD_ KILL< cycle )837 if( DEBUG_THREAD_DELETE < cycle ) 888 838 printk("\n[DBG] %s : thread %x enter for target thread %x / cycle %d\n", 889 839 __FUNCTION__, killer_ptr, target_ptr, cycle ); … … 982 932 else hal_remote_atomic_or( process_state_xp , PROCESS_TERM_KILL ); 983 933 984 #if DEBUG_THREAD_ KILL934 #if DEBUG_THREAD_DELETE 985 935 cycle = (uint32_t)hal_get_cycles; 986 if( DEBUG_THREAD_ KILL< cycle )936 if( DEBUG_THREAD_DELETE < cycle ) 987 937 printk("\n[DBG] %s : thread %x exit for thread %x / main thread / cycle %d\n", 988 938 __FUNCTION__, killer_ptr, target_ptr, cycle ); … … 995 945 hal_remote_atomic_or( target_flags_xp , THREAD_FLAG_REQ_DELETE ); 996 946 997 #if DEBUG_THREAD_ KILL947 #if DEBUG_THREAD_DELETE 998 948 cycle = (uint32_t)hal_get_cycles; 999 if( DEBUG_THREAD_ KILL< cycle )949 if( DEBUG_THREAD_DELETE < cycle ) 1000 950 printk("\n[DBG] %s : thread %x exit for thread %x / not the main thread / cycle %d\n", 1001 951 __FUNCTION__, killer_ptr, target_ptr, cycle ); … … 1005 955 1006 956 } // end thread_kill() 957 958 */ 959 960 ////////////////////////////////////// 961 void 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 997 uint32_t cycle = (uint32_t)hal_get_cycles; 998 if( DEBUG_THREAD_DELETE < cycle ) 999 printk("\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 1076 cycle = (uint32_t)hal_get_cycles; 1077 if( DEBUG_THREAD_DELETE < cycle ) 1078 printk("\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 1007 1085 1008 1086 ///////////////////////
Note: See TracChangeset
for help on using the changeset viewer.