- File:
-
- 1 edited
-
src/libcfa/concurrency/monitor.c (modified) (37 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/libcfa/concurrency/monitor.c
rc2b9f21 r0cf5b79 19 19 #include <inttypes.h> 20 20 21 #include "libhdr.h" 21 22 #include "kernel_private.h" 22 23 … … 90 91 static void __enter_monitor_desc( monitor_desc * this, const __monitor_group_t & group ) { 91 92 // Lock the monitor spinlock 92 DO_LOCK( this->lock __cfaabi_dbg_ctx2 );93 DO_LOCK( this->lock DEBUG_CTX2 ); 93 94 thread_desc * thrd = this_thread; 94 95 95 __cfaabi_dbg_print_safe("Kernel : %10p Entering mon %p (%p)\n", thrd, this, this->owner);96 LIB_DEBUG_PRINT_SAFE("Kernel : %10p Entering mon %p (%p)\n", thrd, this, this->owner); 96 97 97 98 if( !this->owner ) { … … 99 100 set_owner( this, thrd ); 100 101 101 __cfaabi_dbg_print_safe("Kernel : mon is free \n");102 LIB_DEBUG_PRINT_SAFE("Kernel : mon is free \n"); 102 103 } 103 104 else if( this->owner == thrd) { … … 105 106 this->recursion += 1; 106 107 107 __cfaabi_dbg_print_safe("Kernel : mon already owned \n");108 LIB_DEBUG_PRINT_SAFE("Kernel : mon already owned \n"); 108 109 } 109 110 else if( is_accepted( this, group) ) { … … 114 115 reset_mask( this ); 115 116 116 __cfaabi_dbg_print_safe("Kernel : mon accepts \n");117 LIB_DEBUG_PRINT_SAFE("Kernel : mon accepts \n"); 117 118 } 118 119 else { 119 __cfaabi_dbg_print_safe("Kernel : blocking \n");120 LIB_DEBUG_PRINT_SAFE("Kernel : blocking \n"); 120 121 121 122 // Some one else has the monitor, wait in line for it … … 123 124 BlockInternal( &this->lock ); 124 125 125 __cfaabi_dbg_print_safe("Kernel : %10p Entered mon %p\n", thrd, this);126 LIB_DEBUG_PRINT_SAFE("Kernel : %10p Entered mon %p\n", thrd, this); 126 127 127 128 // BlockInternal will unlock spinlock, no need to unlock ourselves … … 129 130 } 130 131 131 __cfaabi_dbg_print_safe("Kernel : %10p Entered mon %p\n", thrd, this);132 LIB_DEBUG_PRINT_SAFE("Kernel : %10p Entered mon %p\n", thrd, this); 132 133 133 134 // Release the lock and leave … … 138 139 static void __enter_monitor_dtor( monitor_desc * this, fptr_t func ) { 139 140 // Lock the monitor spinlock 140 DO_LOCK( this->lock __cfaabi_dbg_ctx2 );141 DO_LOCK( this->lock DEBUG_CTX2 ); 141 142 thread_desc * thrd = this_thread; 142 143 143 __cfaabi_dbg_print_safe("Kernel : %10p Entering dtor for mon %p (%p)\n", thrd, this, this->owner);144 LIB_DEBUG_PRINT_SAFE("Kernel : %10p Entering dtor for mon %p (%p)\n", thrd, this, this->owner); 144 145 145 146 146 147 if( !this->owner ) { 147 __cfaabi_dbg_print_safe("Kernel : Destroying free mon %p\n", this);148 LIB_DEBUG_PRINT_SAFE("Kernel : Destroying free mon %p\n", this); 148 149 149 150 // No one has the monitor, just take it … … 163 164 __monitor_group_t group = { &this, 1, func }; 164 165 if( is_accepted( this, group) ) { 165 __cfaabi_dbg_print_safe("Kernel : mon accepts dtor, block and signal it \n");166 LIB_DEBUG_PRINT_SAFE("Kernel : mon accepts dtor, block and signal it \n"); 166 167 167 168 // Wake the thread that is waiting for this … … 182 183 } 183 184 else { 184 __cfaabi_dbg_print_safe("Kernel : blocking \n");185 LIB_DEBUG_PRINT_SAFE("Kernel : blocking \n"); 185 186 186 187 wait_ctx( this_thread, 0 ) … … 195 196 } 196 197 197 __cfaabi_dbg_print_safe("Kernel : Destroying %p\n", this);198 LIB_DEBUG_PRINT_SAFE("Kernel : Destroying %p\n", this); 198 199 199 200 } … … 202 203 void __leave_monitor_desc( monitor_desc * this ) { 203 204 // Lock the monitor spinlock, DO_LOCK to reduce contention 204 DO_LOCK( this->lock __cfaabi_dbg_ctx2 );205 206 __cfaabi_dbg_print_safe("Kernel : %10p Leaving mon %p (%p)\n", this_thread, this, this->owner);205 DO_LOCK( this->lock DEBUG_CTX2 ); 206 207 LIB_DEBUG_PRINT_SAFE("Kernel : %10p Leaving mon %p (%p)\n", this_thread, this, this->owner); 207 208 208 209 verifyf( this_thread == this->owner, "Expected owner to be %p, got %p (r: %i, m: %p)", this_thread, this->owner, this->recursion, this ); … … 214 215 // it means we don't need to do anything 215 216 if( this->recursion != 0) { 216 __cfaabi_dbg_print_safe("Kernel : recursion still %d\n", this->recursion);217 LIB_DEBUG_PRINT_SAFE("Kernel : recursion still %d\n", this->recursion); 217 218 unlock( this->lock ); 218 219 return; … … 231 232 // Leave single monitor for the last time 232 233 void __leave_dtor_monitor_desc( monitor_desc * this ) { 233 __cfaabi_dbg_debug_do(234 LIB_DEBUG_DO( 234 235 if( this_thread != this->owner ) { 235 236 abortf("Destroyed monitor %p has inconsistent owner, expected %p got %p.\n", this, this_thread, this->owner); … … 248 249 249 250 // Lock the monitor now 250 DO_LOCK( this->lock __cfaabi_dbg_ctx2 );251 DO_LOCK( this->lock DEBUG_CTX2 ); 251 252 252 253 disable_interrupts(); … … 307 308 (this_thread->monitors){m, count, func}; 308 309 309 // __cfaabi_dbg_print_safe("MGUARD : enter %d\n", count);310 // LIB_DEBUG_PRINT_SAFE("MGUARD : enter %d\n", count); 310 311 311 312 // Enter the monitors in order … … 313 314 enter( group ); 314 315 315 // __cfaabi_dbg_print_safe("MGUARD : entered\n");316 // LIB_DEBUG_PRINT_SAFE("MGUARD : entered\n"); 316 317 } 317 318 … … 319 320 // Dtor for monitor guard 320 321 void ^?{}( monitor_guard_t & this ) { 321 // __cfaabi_dbg_print_safe("MGUARD : leaving %d\n", this.count);322 // LIB_DEBUG_PRINT_SAFE("MGUARD : leaving %d\n", this.count); 322 323 323 324 // Leave the monitors in order 324 325 leave( this.m, this.count ); 325 326 326 // __cfaabi_dbg_print_safe("MGUARD : left\n");327 // LIB_DEBUG_PRINT_SAFE("MGUARD : left\n"); 327 328 328 329 // Restore thread context … … 429 430 430 431 //Some more checking in debug 431 __cfaabi_dbg_debug_do(432 LIB_DEBUG_DO( 432 433 thread_desc * this_thrd = this_thread; 433 434 if ( this.monitor_count != this_thrd->monitors.size ) { … … 486 487 set_owner( monitors, count, signallee ); 487 488 488 __cfaabi_dbg_print_buffer_decl( "Kernel : signal_block condition %p (s: %p)\n", &this, signallee );489 LIB_DEBUG_PRINT_BUFFER_DECL( "Kernel : signal_block condition %p (s: %p)\n", &this, signallee ); 489 490 490 491 //Everything is ready to go to sleep … … 495 496 496 497 497 __cfaabi_dbg_print_buffer_local( "Kernel : signal_block returned\n" );498 LIB_DEBUG_PRINT_BUFFER_LOCAL( "Kernel : signal_block returned\n" ); 498 499 499 500 //We are back, restore the masks and recursions … … 534 535 __lock_size_t actual_count = aggregate( mon_storage, mask ); 535 536 536 __cfaabi_dbg_print_buffer_decl( "Kernel : waitfor %d (s: %d, m: %d)\n", actual_count, mask.size, (__lock_size_t)max);537 LIB_DEBUG_PRINT_BUFFER_DECL( "Kernel : waitfor %d (s: %d, m: %d)\n", actual_count, mask.size, (__lock_size_t)max); 537 538 538 539 if(actual_count == 0) return; 539 540 540 __cfaabi_dbg_print_buffer_local( "Kernel : waitfor internal proceeding\n");541 LIB_DEBUG_PRINT_BUFFER_LOCAL( "Kernel : waitfor internal proceeding\n"); 541 542 542 543 // Create storage for monitor context … … 555 556 __acceptable_t& accepted = mask[index]; 556 557 if( accepted.is_dtor ) { 557 __cfaabi_dbg_print_buffer_local( "Kernel : dtor already there\n");558 LIB_DEBUG_PRINT_BUFFER_LOCAL( "Kernel : dtor already there\n"); 558 559 verifyf( accepted.size == 1, "ERROR: Accepted dtor has more than 1 mutex parameter." ); 559 560 … … 567 568 } 568 569 else { 569 __cfaabi_dbg_print_buffer_local( "Kernel : thread present, baton-passing\n");570 LIB_DEBUG_PRINT_BUFFER_LOCAL( "Kernel : thread present, baton-passing\n"); 570 571 571 572 // Create the node specific to this wait operation … … 575 576 monitor_save; 576 577 577 __cfaabi_dbg_print_buffer_local( "Kernel : baton of %d monitors : ", count );578 LIB_DEBUG_PRINT_BUFFER_LOCAL( "Kernel : baton of %d monitors : ", count ); 578 579 #ifdef __CFA_DEBUG_PRINT__ 579 580 for( int i = 0; i < count; i++) { 580 __cfaabi_dbg_print_buffer_local( "%p %p ", monitors[i], monitors[i]->signal_stack.top );581 LIB_DEBUG_PRINT_BUFFER_LOCAL( "%p %p ", monitors[i], monitors[i]->signal_stack.top ); 581 582 } 582 583 #endif 583 __cfaabi_dbg_print_buffer_local( "\n");584 LIB_DEBUG_PRINT_BUFFER_LOCAL( "\n"); 584 585 585 586 // Set the owners to be the next thread … … 592 593 monitor_restore; 593 594 594 __cfaabi_dbg_print_buffer_local( "Kernel : thread present, returned\n");595 LIB_DEBUG_PRINT_BUFFER_LOCAL( "Kernel : thread present, returned\n"); 595 596 } 596 597 597 __cfaabi_dbg_print_buffer_local( "Kernel : accepted %d\n", *mask.accepted);598 LIB_DEBUG_PRINT_BUFFER_LOCAL( "Kernel : accepted %d\n", *mask.accepted); 598 599 return; 599 600 } … … 602 603 603 604 if( duration == 0 ) { 604 __cfaabi_dbg_print_buffer_local( "Kernel : non-blocking, exiting\n");605 LIB_DEBUG_PRINT_BUFFER_LOCAL( "Kernel : non-blocking, exiting\n"); 605 606 606 607 unlock_all( locks, count ); 607 608 608 __cfaabi_dbg_print_buffer_local( "Kernel : accepted %d\n", *mask.accepted);609 LIB_DEBUG_PRINT_BUFFER_LOCAL( "Kernel : accepted %d\n", *mask.accepted); 609 610 return; 610 611 } … … 613 614 verifyf( duration < 0, "Timeout on waitfor statments not supported yet."); 614 615 615 __cfaabi_dbg_print_buffer_local( "Kernel : blocking waitfor\n");616 LIB_DEBUG_PRINT_BUFFER_LOCAL( "Kernel : blocking waitfor\n"); 616 617 617 618 // Create the node specific to this wait operation … … 635 636 monitor_restore; 636 637 637 __cfaabi_dbg_print_buffer_local( "Kernel : exiting\n");638 639 __cfaabi_dbg_print_buffer_local( "Kernel : accepted %d\n", *mask.accepted);638 LIB_DEBUG_PRINT_BUFFER_LOCAL( "Kernel : exiting\n"); 639 640 LIB_DEBUG_PRINT_BUFFER_LOCAL( "Kernel : accepted %d\n", *mask.accepted); 640 641 } 641 642 … … 644 645 645 646 static inline void set_owner( monitor_desc * this, thread_desc * owner ) { 646 // __cfaabi_dbg_print_safe("Kernal : Setting owner of %p to %p ( was %p)\n", this, owner, this->owner );647 // LIB_DEBUG_PRINT_SAFE("Kernal : Setting owner of %p to %p ( was %p)\n", this, owner, this->owner ); 647 648 648 649 //Pass the monitor appropriately … … 676 677 static inline thread_desc * next_thread( monitor_desc * this ) { 677 678 //Check the signaller stack 678 __cfaabi_dbg_print_safe("Kernel : mon %p AS-stack top %p\n", this, this->signal_stack.top);679 LIB_DEBUG_PRINT_SAFE("Kernel : mon %p AS-stack top %p\n", this, this->signal_stack.top); 679 680 __condition_criterion_t * urgent = pop( this->signal_stack ); 680 681 if( urgent ) { … … 728 729 for( __lock_size_t i = 0; i < count; i++) { 729 730 (criteria[i]){ monitors[i], waiter }; 730 __cfaabi_dbg_print_safe( "Kernel : target %p = %p\n", criteria[i].target, &criteria[i] );731 LIB_DEBUG_PRINT_SAFE( "Kernel : target %p = %p\n", criteria[i].target, &criteria[i] ); 731 732 push( criteria[i].target->signal_stack, &criteria[i] ); 732 733 } … … 737 738 static inline void lock_all( __spinlock_t * locks [], __lock_size_t count ) { 738 739 for( __lock_size_t i = 0; i < count; i++ ) { 739 DO_LOCK( *locks[i] __cfaabi_dbg_ctx2 );740 DO_LOCK( *locks[i] DEBUG_CTX2 ); 740 741 } 741 742 } … … 744 745 for( __lock_size_t i = 0; i < count; i++ ) { 745 746 __spinlock_t * l = &source[i]->lock; 746 DO_LOCK( *l __cfaabi_dbg_ctx2 );747 DO_LOCK( *l DEBUG_CTX2 ); 747 748 if(locks) locks[i] = l; 748 749 } … … 802 803 for( int i = 0; i < count; i++ ) { 803 804 804 // __cfaabi_dbg_print_safe( "Checking %p for %p\n", &criteria[i], target );805 // LIB_DEBUG_PRINT_SAFE( "Checking %p for %p\n", &criteria[i], target ); 805 806 if( &criteria[i] == target ) { 806 807 criteria[i].ready = true; 807 // __cfaabi_dbg_print_safe( "True\n" );808 // LIB_DEBUG_PRINT_SAFE( "True\n" ); 808 809 } 809 810 … … 811 812 } 812 813 813 __cfaabi_dbg_print_safe( "Kernel : Runing %i (%p)\n", ready2run, ready2run ? node->waiting_thread : NULL );814 LIB_DEBUG_PRINT_SAFE( "Kernel : Runing %i (%p)\n", ready2run, ready2run ? node->waiting_thread : NULL ); 814 815 return ready2run ? node->waiting_thread : NULL; 815 816 } … … 818 819 thread_desc * thrd = this_thread; 819 820 if( !this.monitors ) { 820 // __cfaabi_dbg_print_safe("Branding\n");821 // LIB_DEBUG_PRINT_SAFE("Branding\n"); 821 822 assertf( thrd->monitors.data != NULL, "No current monitor to brand condition %p", thrd->monitors.data ); 822 823 this.monitor_count = thrd->monitors.size; 823 824 824 this.monitors = (monitor_desc **)malloc( this.monitor_count * sizeof( *this.monitors ) );825 this.monitors = malloc( this.monitor_count * sizeof( *this.monitors ) ); 825 826 for( int i = 0; i < this.monitor_count; i++ ) { 826 827 this.monitors[i] = thrd->monitors[i];
Note:
See TracChangeset
for help on using the changeset viewer.