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