- File:
-
- 1 edited
-
libcfa/src/concurrency/monitor.cfa (modified) (19 diffs)
Legend:
- Unmodified
- Added
- Removed
-
libcfa/src/concurrency/monitor.cfa
r09f357ec rb0c7419 117 117 118 118 // Some one else has the monitor, wait in line for it 119 /* paranoid */ verify( thrd->next == 0p ); 119 120 append( this->entry_queue, thrd ); 120 121 BlockInternal( &this->lock ); 121 /* paranoid */ verify( thrd->next == 1p ); 122 123 unlock( this->lock ); 124 park(); 122 125 123 126 __cfaabi_dbg_print_safe( "Kernel : %10p Entered mon %p\n", thrd, this); 124 127 125 / / BlockInternal will unlock spinlock, no need to unlock ourselves128 /* paranoid */ verifyf( kernelTLS.this_thread == this->owner, "Expected owner to be %p, got %p (r: %i, m: %p)", kernelTLS.this_thread, this->owner, this->recursion, this ); 126 129 return; 127 130 } 128 131 129 132 __cfaabi_dbg_print_safe( "Kernel : %10p Entered mon %p\n", thrd, this); 133 134 /* paranoid */ verifyf( kernelTLS.this_thread == this->owner, "Expected owner to be %p, got %p (r: %i, m: %p)", kernelTLS.this_thread, this->owner, this->recursion, this ); 135 /* paranoid */ verify( this->lock.lock ); 130 136 131 137 // Release the lock and leave … … 149 155 set_owner( this, thrd ); 150 156 157 verifyf( kernelTLS.this_thread == this->owner, "Expected owner to be %p, got %p (r: %i, m: %p)", kernelTLS.this_thread, this->owner, this->recursion, this ); 158 151 159 unlock( this->lock ); 152 160 return; … … 166 174 // Wake the thread that is waiting for this 167 175 __condition_criterion_t * urgent = pop( this->signal_stack ); 168 verify( urgent );176 /* paranoid */ verify( urgent ); 169 177 170 178 // Reset mask … … 175 183 176 184 // Some one else has the monitor, wait for him to finish and then run 177 BlockInternal( &this->lock, urgent->owner->waiting_thread ); 185 unlock( this->lock ); 186 187 // Release the next thread 188 /* paranoid */ verifyf( urgent->owner->waiting_thread == this->owner, "Expected owner to be %p, got %p (r: %i, m: %p)", kernelTLS.this_thread, this->owner, this->recursion, this ); 189 unpark( urgent->owner->waiting_thread ); 190 191 // Park current thread waiting 192 park(); 178 193 179 194 // Some one was waiting for us, enter 180 set_owner( this, thrd);195 /* paranoid */ verifyf( kernelTLS.this_thread == this->owner, "Expected owner to be %p, got %p (r: %i, m: %p)", kernelTLS.this_thread, this->owner, this->recursion, this ); 181 196 } 182 197 else { … … 187 202 188 203 // Some one else has the monitor, wait in line for it 204 /* paranoid */ verify( thrd->next == 0p ); 189 205 append( this->entry_queue, thrd ); 190 BlockInternal( &this->lock ); 191 192 // BlockInternal will unlock spinlock, no need to unlock ourselves 206 /* paranoid */ verify( thrd->next == 1p ); 207 unlock( this->lock ); 208 209 // Park current thread waiting 210 park(); 211 212 /* paranoid */ verifyf( kernelTLS.this_thread == this->owner, "Expected owner to be %p, got %p (r: %i, m: %p)", kernelTLS.this_thread, this->owner, this->recursion, this ); 193 213 return; 194 214 } … … 205 225 __cfaabi_dbg_print_safe( "Kernel : %10p Leaving mon %p (%p)\n", kernelTLS.this_thread, this, this->owner); 206 226 207 verifyf( kernelTLS.this_thread == this->owner, "Expected owner to be %p, got %p (r: %i, m: %p)", kernelTLS.this_thread, this->owner, this->recursion, this );227 /* paranoid */ verifyf( kernelTLS.this_thread == this->owner, "Expected owner to be %p, got %p (r: %i, m: %p)", kernelTLS.this_thread, this->owner, this->recursion, this ); 208 228 209 229 // Leaving a recursion level, decrement the counter … … 221 241 thread_desc * new_owner = next_thread( this ); 222 242 243 // Check the new owner is consistent with who we wake-up 244 // new_owner might be null even if someone owns the monitor when the owner is still waiting for another monitor 245 /* paranoid */ verifyf( !new_owner || new_owner == this->owner, "Expected owner to be %p, got %p (m: %p)", new_owner, this->owner, this ); 246 223 247 // We can now let other threads in safely 224 248 unlock( this->lock ); 225 249 226 250 //We need to wake-up the thread 227 WakeThread( new_owner ); 251 /* paranoid */ verifyf( !new_owner || new_owner == this->owner, "Expected owner to be %p, got %p (m: %p)", new_owner, this->owner, this ); 252 unpark( new_owner ); 228 253 } 229 254 … … 252 277 disable_interrupts(); 253 278 254 thrd->s elf_cor.state = Halted;255 256 verifyf( thrd == this->owner, "Expected owner to be %p, got %p (r: %i, m: %p)", thrd, this->owner, this->recursion, this );279 thrd->state = Halted; 280 281 /* paranoid */ verifyf( thrd == this->owner, "Expected owner to be %p, got %p (r: %i, m: %p)", thrd, this->owner, this->recursion, this ); 257 282 258 283 // Leaving a recursion level, decrement the counter … … 266 291 thread_desc * new_owner = next_thread( this ); 267 292 268 // Leave the thread, this will unlock the spinlock 269 // Use leave thread instead of BlockInternal which is 270 // specialized for this case and supports null new_owner 271 LeaveThread( &this->lock, new_owner ); 293 // Release the monitor lock 294 unlock( this->lock ); 295 296 // Unpark the next owner if needed 297 /* paranoid */ verifyf( !new_owner || new_owner == this->owner, "Expected owner to be %p, got %p (m: %p)", new_owner, this->owner, this ); 298 /* paranoid */ verify( ! kernelTLS.preemption_state.enabled ); 299 /* paranoid */ verify( ! kernelTLS.this_processor->destroyer ); 300 /* paranoid */ verify( thrd->state == Halted ); 301 302 kernelTLS.this_processor->destroyer = new_owner; 303 304 // Leave the thread 305 __leave_thread(); 272 306 273 307 // Control flow should never reach here! … … 400 434 // Append the current wait operation to the ones already queued on the condition 401 435 // We don't need locks for that since conditions must always be waited on inside monitor mutual exclusion 436 /* paranoid */ verify( waiter.next == 0p ); 402 437 append( this.blocked, &waiter ); 438 /* paranoid */ verify( waiter.next == 1p ); 403 439 404 440 // Lock all monitors (aggregates the locks as well) … … 419 455 } 420 456 457 // Unlock the locks, we don't need them anymore 458 for(int i = 0; i < count; i++) { 459 unlock( *locks[i] ); 460 } 461 462 // Wake the threads 463 for(int i = 0; i < thread_count; i++) { 464 unpark( threads[i] ); 465 } 466 421 467 // Everything is ready to go to sleep 422 BlockInternal( locks, count, threads, thread_count);468 park(); 423 469 424 470 // We are back, restore the owners and recursions … … 490 536 //Find the thread to run 491 537 thread_desc * signallee = pop_head( this.blocked )->waiting_thread; 538 /* paranoid */ verify( signallee->next == 0p ); 492 539 set_owner( monitors, count, signallee ); 493 540 494 541 __cfaabi_dbg_print_buffer_decl( "Kernel : signal_block condition %p (s: %p)\n", &this, signallee ); 495 542 543 // unlock all the monitors 544 unlock_all( locks, count ); 545 546 // unpark the thread we signalled 547 unpark( signallee ); 548 496 549 //Everything is ready to go to sleep 497 BlockInternal( locks, count, &signallee, 1);550 park(); 498 551 499 552 … … 592 645 set_owner( monitors, count, next ); 593 646 594 // Everything is ready to go to sleep 595 BlockInternal( locks, count, &next, 1 ); 647 // unlock all the monitors 648 unlock_all( locks, count ); 649 650 // unpark the thread we signalled 651 unpark( next ); 652 653 //Everything is ready to go to sleep 654 park(); 596 655 597 656 // We are back, restore the owners and recursions … … 631 690 } 632 691 692 // unlock all the monitors 693 unlock_all( locks, count ); 694 633 695 //Everything is ready to go to sleep 634 BlockInternal( locks, count);696 park(); 635 697 636 698 … … 650 712 651 713 static inline void set_owner( monitor_desc * this, thread_desc * owner ) { 652 / / __cfaabi_dbg_print_safe( "Kernal : Setting owner of %p to %p ( was %p)\n", this, owner, this->owner);714 /* paranoid */ verify( this->lock.lock ); 653 715 654 716 //Pass the monitor appropriately … … 660 722 661 723 static inline void set_owner( monitor_desc * monitors [], __lock_size_t count, thread_desc * owner ) { 662 monitors[0]->owner = owner; 663 monitors[0]->recursion = 1; 724 /* paranoid */ verify ( monitors[0]->lock.lock ); 725 /* paranoid */ verifyf( monitors[0]->owner == kernelTLS.this_thread, "Expected owner to be %p, got %p (r: %i, m: %p)", kernelTLS.this_thread, monitors[0]->owner, monitors[0]->recursion, monitors[0] ); 726 monitors[0]->owner = owner; 727 monitors[0]->recursion = 1; 664 728 for( __lock_size_t i = 1; i < count; i++ ) { 665 monitors[i]->owner = owner; 666 monitors[i]->recursion = 0; 729 /* paranoid */ verify ( monitors[i]->lock.lock ); 730 /* paranoid */ verifyf( monitors[i]->owner == kernelTLS.this_thread, "Expected owner to be %p, got %p (r: %i, m: %p)", kernelTLS.this_thread, monitors[i]->owner, monitors[i]->recursion, monitors[i] ); 731 monitors[i]->owner = owner; 732 monitors[i]->recursion = 0; 667 733 } 668 734 } … … 688 754 //regardless of if we are ready to baton pass, 689 755 //we need to set the monitor as in use 756 /* paranoid */ verifyf( !this->owner || kernelTLS.this_thread == this->owner, "Expected owner to be %p, got %p (r: %i, m: %p)", kernelTLS.this_thread, this->owner, this->recursion, this ); 690 757 set_owner( this, urgent->owner->waiting_thread ); 691 758 … … 696 763 // Get the next thread in the entry_queue 697 764 thread_desc * new_owner = pop_head( this->entry_queue ); 765 /* paranoid */ verifyf( !this->owner || kernelTLS.this_thread == this->owner, "Expected owner to be %p, got %p (r: %i, m: %p)", kernelTLS.this_thread, this->owner, this->recursion, this ); 766 /* paranoid */ verify( !new_owner || new_owner->next == 0p ); 698 767 set_owner( this, new_owner ); 699 768 … … 841 910 // For each thread in the entry-queue 842 911 for( thread_desc ** thrd_it = &entry_queue.head; 843 *thrd_it ;912 *thrd_it != 1p; 844 913 thrd_it = &(*thrd_it)->next 845 914 ) {
Note:
See TracChangeset
for help on using the changeset viewer.