Changeset 5e0b6657 for libcfa/src/concurrency/locks.cfa
- Timestamp:
- Dec 8, 2025, 11:29:33 AM (2 months ago)
- Branches:
- master
- Children:
- 79ba50c
- Parents:
- 8f448e0 (diff), 79ec8c3 (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
-
libcfa/src/concurrency/locks.cfa (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
-
libcfa/src/concurrency/locks.cfa
r8f448e0 r5e0b6657 186 186 187 187 // waituntil() support 188 bool register_select ( blocking_lock & this, select_node & node ) with(this) {188 bool register_select$( blocking_lock & this, select_node & node ) with(this) { 189 189 lock( lock __cfaabi_dbg_ctx2 ); 190 190 thread$ * thrd = active_thread(); … … 218 218 } 219 219 220 bool unregister_select ( blocking_lock & this, select_node & node ) with(this) {220 bool unregister_select$( blocking_lock & this, select_node & node ) with(this) { 221 221 lock( lock __cfaabi_dbg_ctx2 ); 222 222 if ( isListed( node ) ) { … … 239 239 } 240 240 241 bool on_selected ( blocking_lock & this, select_node & node ) { return true; }241 bool on_selected$( blocking_lock & this, select_node & node ) { return true; } 242 242 243 243 //----------------------------------------------------------------------------- … … 583 583 } 584 584 } 585 585 586 //----------------------------------------------------------------------------- 586 // Semaphore 587 void ?{}( semaphore & this, int count = 1 ) { 588 (this.lock){}; 589 this.count = count; 590 (this.waiting){}; 591 } 592 void ^?{}(semaphore & this) {} 593 594 bool P(semaphore & this) with( this ){ 595 lock( lock __cfaabi_dbg_ctx2 ); 596 count -= 1; 597 if ( count < 0 ) { 598 // queue current task 599 append( waiting, active_thread() ); 600 601 // atomically release spin lock and block 602 unlock( lock ); 587 // Semaphore, counting 588 589 void ?{}( semaphore & sem, ssize_t count = 1 ) with( sem ) { 590 count$ = count; 591 (lock$){}; 592 (waiting$){}; 593 } 594 595 bool P( semaphore & sem ) with( sem ) { 596 lock( lock$ __cfaabi_dbg_ctx2 ); 597 count$ -= 1; 598 if ( count$ < 0 ) { // in use ? 599 append( waiting$, active_thread() ); // queue current task 600 unlock( lock$ ); // atomically release spin lock and block 603 601 park(); 604 return true; 602 return false; 603 } // if 604 unlock( lock$ ); 605 return true; 606 } 607 608 bool P( semaphore & sem, semaphore & lock ) with( sem ) { 609 lock( lock$ __cfaabi_dbg_ctx2 ); 610 if ( &sem == &lock ) { // perform operation on self ? 611 if ( count$ < 0 ) { // V my semaphore 612 unpark( pop_head( waiting$ ) ); // unblock task at head of waiting list 613 } // if 605 614 } else { 606 unlock( lock ); 615 V( lock ); // V mutex semaphore 616 count$ -= 1; 617 } // if 618 619 if ( count$ < 0 ) { // in use ? 620 append( waiting$, active_thread() ); // queue current task 621 unlock( lock$ ); // atomically release spin lock and block 622 park(); 607 623 return false; 608 } 609 } 610 611 thread$ * V (semaphore & this, const bool doUnpark ) with( this ) { 624 } // if 625 unlock( lock$ ); 626 return true; 627 } 628 629 bool try_P( semaphore & sem ) with( sem ) { 630 lock( lock$ __cfaabi_dbg_ctx2 ); 631 if ( count$ <= 0 ) { // in use ? 632 unlock( lock$ ); 633 return false; 634 } // if 635 count$ -= 1; // acquire 636 unlock( lock$ ); 637 return true; 638 } 639 640 bool V( semaphore & sem ) with( sem ) { 612 641 thread$ * thrd = 0p; 613 lock( lock __cfaabi_dbg_ctx2 ); 614 count += 1; 615 if ( count <= 0 ) { 616 // remove task at head of waiting list 617 thrd = pop_head( waiting ); 618 } 619 620 unlock( lock ); 621 622 // make new owner 623 if ( doUnpark ) unpark( thrd ); 624 625 return thrd; 626 } 627 628 bool V(semaphore & this) with( this ) { 629 thread$ * thrd = V(this, true); 642 lock( lock$ __cfaabi_dbg_ctx2 ); 643 count$ += 1; 644 if ( count$ <= 0 ) { 645 thrd = pop_head( waiting$ ); // remove task at head of waiting list 646 } 647 unlock( lock$ ); 648 if ( true ) unpark( thrd ); // make new owner 630 649 return thrd != 0p; 631 650 } 632 651 633 bool V(semaphore & this, unsigned diff) with( this ) { 634 thread$ * thrd = 0p; 635 lock( lock __cfaabi_dbg_ctx2 ); 636 int release = max(-count, (int)diff); 637 count += diff; 638 for(release) { 639 unpark( pop_head( waiting ) ); 640 } 641 642 unlock( lock ); 643 644 return thrd != 0p; 645 } 646 652 size_t V( semaphore & sem, size_t count ) with( sem ) { 653 lock( lock$ __cfaabi_dbg_ctx2 ); 654 size_t release = max( (size_t)-count$, count ); 655 count$ += count; 656 for ( release ) unpark( pop_head( waiting$ ) ); 657 unlock( lock$ ); 658 return release; 659 }
Note:
See TracChangeset
for help on using the changeset viewer.