Ignore:
Timestamp:
Dec 8, 2025, 11:29:33 AM (2 months ago)
Author:
Michael Brooks <mlbrooks@…>
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.
Message:

Merge remote-tracking branch 'refs/remotes/origin/master'

File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/concurrency/locks.cfa

    r8f448e0 r5e0b6657  
    186186
    187187// waituntil() support
    188 bool register_select( blocking_lock & this, select_node & node ) with(this) {
     188bool register_select$( blocking_lock & this, select_node & node ) with(this) {
    189189        lock( lock __cfaabi_dbg_ctx2 );
    190190        thread$ * thrd = active_thread();
     
    218218}
    219219
    220 bool unregister_select( blocking_lock & this, select_node & node ) with(this) {
     220bool unregister_select$( blocking_lock & this, select_node & node ) with(this) {
    221221        lock( lock __cfaabi_dbg_ctx2 );
    222222        if ( isListed( node ) ) {
     
    239239}
    240240
    241 bool on_selected( blocking_lock & this, select_node & node ) { return true; }
     241bool on_selected$( blocking_lock & this, select_node & node ) { return true; }
    242242
    243243//-----------------------------------------------------------------------------
     
    583583        }
    584584}
     585
    585586//-----------------------------------------------------------------------------
    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
     589void ?{}( semaphore & sem, ssize_t count = 1 ) with( sem ) {
     590        count$ = count;
     591        (lock$){};
     592        (waiting$){};
     593}
     594
     595bool 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
    603601                park();
    604                 return true;
     602                return false;
     603        } // if
     604        unlock( lock$ );
     605        return true;
     606}
     607
     608bool 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
    605614        } 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();
    607623                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
     629bool 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
     640bool V( semaphore & sem ) with( sem ) {
    612641        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
    630649        return thrd != 0p;
    631650}
    632651
    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 
     652size_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.