Changeset 6b33e89 for libcfa/src/concurrency/monitor.cfa
- Timestamp:
- Apr 25, 2025, 7:39:09 AM (5 months ago)
- Branches:
- master
- Children:
- 65bd3c2
- Parents:
- b195498
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
libcfa/src/concurrency/monitor.cfa
rb195498 r6b33e89 9 9 // Author : Thierry Delisle 10 10 // Created On : Thd Feb 23 12:27:26 2017 11 // Last Modified By : Kyoung Seo12 // Last Modified On : Thd Jan 16 12:59:00202513 // Update Count : 7311 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Fri Apr 25 07:20:22 2025 13 // Update Count : 80 14 14 // 15 15 … … 78 78 __spinlock_t * locks[count]; /* We need to pass-in an array of locks to BlockInternal */ 79 79 80 #define monitor_save save( monitors, count, locks, recursions, masks )80 #define monitor_save save ( monitors, count, locks, recursions, masks ) 81 81 #define monitor_restore restore( monitors, count, locks, recursions, masks ) 82 82 … … 95 95 if ( unlikely(0 != (0x1 & (uintptr_t)this->owner)) ) { 96 96 abort( "Attempt by thread \"%.256s\" (%p) to access joined monitor %p.", thrd->self_cor.name, thrd, this ); 97 } else if ( ! this->owner ) {97 } else if ( ! this->owner ) { 98 98 // No one has the monitor, just take it 99 99 __set_owner( this, thrd ); 100 100 101 __cfaabi_dbg_print_safe( "Kernel : 101 __cfaabi_dbg_print_safe( "Kernel : mon is free \n" ); 102 102 } else if ( this->owner == thrd) { 103 103 // We already have the monitor, just note how many times we took it 104 104 this->recursion += 1; 105 105 106 __cfaabi_dbg_print_safe( "Kernel : 106 __cfaabi_dbg_print_safe( "Kernel : mon already owned \n" ); 107 107 } else if ( is_accepted( this, group) ) { 108 108 // Some one was waiting for us, enter … … 112 112 reset_mask( this ); 113 113 114 __cfaabi_dbg_print_safe( "Kernel : 114 __cfaabi_dbg_print_safe( "Kernel : mon accepts \n" ); 115 115 } else { 116 __cfaabi_dbg_print_safe( "Kernel : 116 __cfaabi_dbg_print_safe( "Kernel : blocking \n" ); 117 117 118 118 // Some one else has the monitor, wait in line for it … … 124 124 park(); 125 125 126 __cfaabi_dbg_print_safe( "Kernel : %10p Entered 126 __cfaabi_dbg_print_safe( "Kernel : %10p Entered mon %p\n", thrd, this); 127 127 128 128 /* paranoid */ verifyf( active_thread() == this->owner, "Expected owner to be %p, got %p (r: %i, m: %p)", active_thread(), this->owner, this->recursion, this ); … … 130 130 } 131 131 132 __cfaabi_dbg_print_safe( "Kernel : %10p Entered 132 __cfaabi_dbg_print_safe( "Kernel : %10p Entered mon %p\n", thrd, this); 133 133 134 134 /* paranoid */ verifyf( active_thread() == this->owner, "Expected owner to be %p, got %p (r: %i, m: %p)", active_thread(), this->owner, this->recursion, this ); … … 152 152 153 153 154 if ( ! this->owner ) {154 if ( ! this->owner ) { 155 155 __cfaabi_dbg_print_safe( "Kernel : Destroying free mon %p\n", this); 156 156 … … 159 159 160 160 /* paranoid */ verifyf( active_thread() == this->owner, "Expected owner to be %p, got %p (r: %i, m: %p)", active_thread(), this->owner, this->recursion, this ); 161 /* paranoid */ verify( ! is_thrd || thrd->state == Halted || thrd->state == Cancelled );161 /* paranoid */ verify( ! is_thrd || thrd->state == Halted || thrd->state == Cancelled ); 162 162 163 163 unlock( this->lock ); 164 164 return; 165 } else if ( this->owner == thrd && ! join) {165 } else if ( this->owner == thrd && ! join) { 166 166 // We already have the monitor... but where about to destroy it so the nesting will fail 167 167 // Abort! … … 179 179 180 180 /* paranoid */ verifyf( active_thread() == this->owner, "Expected owner to be %p, got %p (r: %i, m: %p)", active_thread(), this->owner, this->recursion, this ); 181 /* paranoid */ verify( ! is_thrd || thrd->state == Halted || thrd->state == Cancelled );181 /* paranoid */ verify( ! is_thrd || thrd->state == Halted || thrd->state == Cancelled ); 182 182 183 183 unlock( this->lock ); … … 186 186 187 187 // The monitor is busy, if this is a thread and the thread owns itself, it better be active 188 /* paranoid */ verify( ! is_thrd || this->owner != thrd || (thrd->state != Halted && thrd->state != Cancelled) );188 /* paranoid */ verify( ! is_thrd || this->owner != thrd || (thrd->state != Halted && thrd->state != Cancelled) ); 189 189 190 190 __lock_size_t count = 1; … … 192 192 __monitor_group_t group = { &this, 1, func }; 193 193 if ( is_accepted( this, group) ) { 194 __cfaabi_dbg_print_safe( "Kernel : 194 __cfaabi_dbg_print_safe( "Kernel : mon accepts dtor, block and signal it \n" ); 195 195 196 196 // Wake the thread that is waiting for this … … 220 220 return; 221 221 } else { 222 __cfaabi_dbg_print_safe( "Kernel : 222 __cfaabi_dbg_print_safe( "Kernel : blocking \n" ); 223 223 224 224 wait_ctx( thrd, 0 ) … … 254 254 // it means we don't need to do anything 255 255 if ( this->recursion != 0) { 256 __cfaabi_dbg_print_safe( "Kernel : 256 __cfaabi_dbg_print_safe( "Kernel : recursion still %d\n", this->recursion); 257 257 unlock( this->lock ); 258 258 return; … … 264 264 // Check the new owner is consistent with who we wake-up 265 265 // new_owner might be null even if someone owns the monitor when the owner is still waiting for another monitor 266 /* paranoid */ verifyf( ! new_owner || new_owner == this->owner, "Expected owner to be %p, got %p (m: %p)", new_owner, this->owner, this );266 /* paranoid */ verifyf( ! new_owner || new_owner == this->owner, "Expected owner to be %p, got %p (m: %p)", new_owner, this->owner, this ); 267 267 268 268 // We can now let other threads in safely … … 270 270 271 271 //We need to wake-up the thread 272 /* paranoid */ verifyf( ! new_owner || new_owner == this->owner, "Expected owner to be %p, got %p (m: %p)", new_owner, this->owner, this );272 /* paranoid */ verifyf( ! new_owner || new_owner == this->owner, "Expected owner to be %p, got %p (m: %p)", new_owner, this->owner, this ); 273 273 unpark( new_owner ); 274 274 } … … 280 280 abort( "Destroyed monitor %p has inconsistent owner, expected %p got %p.\n", this, active_thread(), this->owner); 281 281 } 282 if ( this->recursion != 1 && !join ) {282 if ( this->recursion != 1 && ! join ) { 283 283 abort( "Destroyed monitor %p has %d outstanding nested calls.\n", this, this->recursion - 1); 284 284 } … … 317 317 318 318 // Unpark the next owner if needed 319 /* paranoid */ verifyf( ! new_owner || new_owner == this->owner, "Expected owner to be %p, got %p (m: %p)", new_owner, this->owner, this );319 /* paranoid */ verifyf( ! new_owner || new_owner == this->owner, "Expected owner to be %p, got %p (m: %p)", new_owner, this->owner, this ); 320 320 /* paranoid */ verify( ! __preemption_enabled() ); 321 321 /* paranoid */ verify( thrd->state == Halted ); … … 424 424 425 425 static void ?{}(__condition_criterion_t & this ) with( this ) { 426 ready 426 ready = false; 427 427 target = 0p; 428 owner 429 next= 0p;428 owner = 0p; 429 this.next = 0p; 430 430 } 431 431 432 432 static void ?{}(__condition_criterion_t & this, monitor$ * target, __condition_node_t & owner ) { 433 this.ready 433 this.ready = false; 434 434 this.target = target; 435 this.owner 436 this.next 435 this.owner = &owner; 436 this.next = 0p; 437 437 } 438 438 … … 525 525 for ( i; count ) { 526 526 __condition_criterion_t * crit = &node->criteria[i]; 527 assert( ! crit->ready );527 assert( ! crit->ready ); 528 528 push( crit->target->signal_stack, crit ); 529 529 } … … 536 536 537 537 bool signal_block( condition & this ) libcfa_public { 538 if ( ! this.blocked.head ) { return false; }538 if ( ! this.blocked.head ) { return false; } 539 539 540 540 //Check that everything is as expected … … 571 571 // WE WOKE UP 572 572 573 __cfaabi_dbg_print_buffer_local( "Kernel : 573 __cfaabi_dbg_print_buffer_local( "Kernel : signal_block returned\n" ); 574 574 575 575 //We are back, restore the masks and recursions … … 581 581 // Access the user_info of the thread waiting at the front of the queue 582 582 uintptr_t front( condition & this ) libcfa_public { 583 verifyf( ! is_empty(this),583 verifyf( ! is_empty(this), 584 584 "Attempt to access user data on an empty condition.\n" 585 585 "Possible cause is not checking if the condition is empty before reading stored data." … … 624 624 { 625 625 // Check if the entry queue 626 thread$ * n ext; int index;627 [n ext, index] = search_entry_queue( mask, monitors, count );628 629 if ( n ext ) {626 thread$ * nxt; int index; 627 [nxt, index] = search_entry_queue( mask, monitors, count ); 628 629 if ( nxt ) { 630 630 *mask.accepted = index; 631 631 __acceptable_t& accepted = mask[index]; 632 632 if ( accepted.is_dtor ) { 633 633 __cfaabi_dbg_print_buffer_local( "Kernel : dtor already there\n" ); 634 verifyf( accepted.size == 1, 634 verifyf( accepted.size == 1, "ERROR: Accepted dtor has more than 1 mutex parameter." ); 635 635 636 636 monitor$ * mon2dtor = accepted[0]; … … 651 651 monitor_save; 652 652 653 __cfaabi_dbg_print_buffer_local( "Kernel : 653 __cfaabi_dbg_print_buffer_local( "Kernel : baton of %"PRIdFAST16" monitors : ", count ); 654 654 #ifdef __CFA_DEBUG_PRINT__ 655 655 for ( i; count ) { … … 660 660 661 661 // Set the owners to be the next thread 662 __set_owner( monitors, count, n ext );662 __set_owner( monitors, count, nxt ); 663 663 664 664 // unlock all the monitors … … 666 666 667 667 // unpark the thread we signalled 668 unpark( n ext );668 unpark( nxt ); 669 669 670 670 //Everything is ready to go to sleep … … 741 741 /* paranoid */ verify ( monitors[0]->lock.lock ); 742 742 /* paranoid */ verifyf( monitors[0]->owner == active_thread(), "Expected owner to be %p, got %p (r: %i, m: %p)", active_thread(), monitors[0]->owner, monitors[0]->recursion, monitors[0] ); 743 monitors[0]->owner 744 monitors[0]->recursion 743 monitors[0]->owner = owner; 744 monitors[0]->recursion = 1; 745 745 for ( i; 1~count ) { 746 746 /* paranoid */ verify ( monitors[i]->lock.lock ); 747 747 /* paranoid */ verifyf( monitors[i]->owner == active_thread(), "Expected owner to be %p, got %p (r: %i, m: %p)", active_thread(), monitors[i]->owner, monitors[i]->recursion, monitors[i] ); 748 monitors[i]->owner 749 monitors[i]->recursion 748 monitors[i]->owner = owner; 749 monitors[i]->recursion = 0; 750 750 } 751 751 } … … 765 765 static inline thread$ * next_thread( monitor$ * this ) { 766 766 //Check the signaller stack 767 __cfaabi_dbg_print_safe( "Kernel : 767 __cfaabi_dbg_print_safe( "Kernel : mon %p AS-stack top %p\n", this, this->signal_stack.top); 768 768 __condition_criterion_t * urgent = pop( this->signal_stack ); 769 769 if ( urgent ) { … … 771 771 //regardless of if we are ready to baton pass, 772 772 //we need to set the monitor as in use 773 /* paranoid */ verifyf( ! this->owner || active_thread() == this->owner, "Expected owner to be %p, got %p (r: %i, m: %p)", active_thread(), this->owner, this->recursion, this );774 __set_owner( this, 773 /* paranoid */ verifyf( ! this->owner || active_thread() == this->owner, "Expected owner to be %p, got %p (r: %i, m: %p)", active_thread(), this->owner, this->recursion, this ); 774 __set_owner( this, urgent->owner->waiting_thread ); 775 775 776 776 return check_condition( urgent ); … … 780 780 // Get the next thread in the entry_queue 781 781 thread$ * new_owner = pop_head( this->entry_queue ); 782 /* paranoid */ verifyf( ! this->owner || active_thread() == this->owner, "Expected owner to be %p, got %p (r: %i, m: %p)", active_thread(), this->owner, this->recursion, this );783 /* paranoid */ verify( ! new_owner || new_owner->user_link.next == 0p );782 /* paranoid */ verifyf( ! this->owner || active_thread() == this->owner, "Expected owner to be %p, got %p (r: %i, m: %p)", active_thread(), this->owner, this->recursion, this ); 783 /* paranoid */ verify( ! new_owner || new_owner->user_link.next == 0p ); 784 784 __set_owner( this, new_owner ); 785 785 … … 792 792 793 793 // Check if there are any acceptable functions 794 if ( ! it ) return false;794 if ( ! it ) return false; 795 795 796 796 // If this isn't the first monitor to test this, there is no reason to repeat the test. … … 820 820 for ( i; count ) { 821 821 (criteria[i]){ monitors[i], waiter }; 822 __cfaabi_dbg_print_safe( "Kernel : 822 __cfaabi_dbg_print_safe( "Kernel : target %p = %p\n", criteria[i].target, &criteria[i] ); 823 823 push( criteria[i].target->signal_stack, &criteria[i] ); 824 824 } … … 902 902 } 903 903 904 __cfaabi_dbg_print_safe( "Kernel : 904 __cfaabi_dbg_print_safe( "Kernel : Runing %i (%p)\n", ready2run, ready2run ? (thread*)node->waiting_thread : (thread*)0p ); 905 905 return ready2run ? node->waiting_thread : 0p; 906 906 } … … 908 908 static inline void brand_condition( condition & this ) { 909 909 thread$ * thrd = active_thread(); 910 if ( ! this.monitors ) {910 if ( ! this.monitors ) { 911 911 // __cfaabi_dbg_print_safe( "Branding\n" ); 912 912 assertf( thrd->monitors.data != 0p, "No current monitor to brand condition %p", thrd->monitors.data ); … … 928 928 for ( __acceptable_t * it = begin; it != end; it++, i++ ) { 929 929 #if defined( __CFA_WITH_VERIFY__ ) 930 thread$ * last= 0p;930 thread$ * prior = 0p; 931 931 #endif // __CFA_WITH_VERIFY__ 932 932 … … 934 934 thread$ * curr = *thrd_it; 935 935 936 /* paranoid */ verifyf( ! last || last->user_link.next == curr, "search not making progress, from %p (%p) to %p",937 last, last->user_link.next, curr );938 /* paranoid */ verifyf( curr != last, "search not making progress, from %p to %p", last, curr );936 /* paranoid */ verifyf( ! prior || prior->user_link.next == curr, "search not making progress, from %p (%p) to %p", 937 prior, prior->user_link.next, curr ); 938 /* paranoid */ verifyf( curr != prior, "search not making progress, from %p to %p", prior, curr ); 939 939 940 940 // For each thread in the entry-queue check for a match … … 945 945 946 946 #if defined( __CFA_WITH_VERIFY__ ) 947 last= curr;947 prior = curr; 948 948 #endif 949 949 } // for … … 1001 1001 if ( unlikely(0 != (0x1 & (uintptr_t)this->owner)) ) { 1002 1002 abort( "Attempt by thread \"%.256s\" (%p) to access joined monitor %p.", thrd->self_cor.name, thrd, this ); 1003 } else if ( ! this->owner ) {1003 } else if ( ! this->owner ) { 1004 1004 // No one has the monitor, just take it 1005 1005 __set_owner( this, thrd ); 1006 1006 1007 __cfaabi_dbg_print_safe( "Kernel : 1007 __cfaabi_dbg_print_safe( "Kernel : mon is free \n" ); 1008 1008 } else if ( this->owner == thrd) { 1009 1009 // We already have the monitor, just note how many times we took it 1010 1010 this->recursion += 1; 1011 1011 1012 __cfaabi_dbg_print_safe( "Kernel : 1012 __cfaabi_dbg_print_safe( "Kernel : mon already owned \n" ); 1013 1013 } else { 1014 __cfaabi_dbg_print_safe( "Kernel : 1014 __cfaabi_dbg_print_safe( "Kernel : blocking \n" ); 1015 1015 1016 1016 // Some one else has the monitor, wait in line for it … … 1022 1022 park(); 1023 1023 1024 __cfaabi_dbg_print_safe( "Kernel : %10p Entered 1024 __cfaabi_dbg_print_safe( "Kernel : %10p Entered mon %p\n", thrd, this); 1025 1025 1026 1026 /* paranoid */ verifyf( active_thread() == this->owner, "Expected owner to be %p, got %p (r: %i, m: %p)", active_thread(), this->owner, this->recursion, this ); … … 1028 1028 } 1029 1029 1030 __cfaabi_dbg_print_safe( "Kernel : %10p Entered 1030 __cfaabi_dbg_print_safe( "Kernel : %10p Entered mon %p\n", thrd, this); 1031 1031 1032 1032 /* paranoid */ verifyf( active_thread() == this->owner, "Expected owner to be %p, got %p (r: %i, m: %p)", active_thread(), this->owner, this->recursion, this );
Note:
See TracChangeset
for help on using the changeset viewer.