Ignore:
Timestamp:
Nov 7, 2025, 8:30:59 AM (4 days ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
master
Parents:
0048327
Message:

update semaphore lock

Location:
libcfa/src/concurrency
Files:
2 edited

Legend:

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

    r0048327 rb94579a  
    587587// Semaphore, counting
    588588
    589 // PRIVATE
    590 
    591 thread$ * V( semaphore & sem, const bool doUnpark ) with( sem ) {
    592         thread$ * thrd = 0p;
    593         lock( lock$ __cfaabi_dbg_ctx2 );
    594         count$ += 1;
    595         if ( count$ <= 0 ) {
    596                 thrd = pop_head( waiting$ );                                            // remove task at head of waiting list
    597         }
    598         unlock( lock$ );
    599         if ( doUnpark ) unpark( thrd );                                         // make new owner
    600         return thrd;
    601 }
    602 
    603 // PUBLIC
    604 
    605589void ?{}( semaphore & sem, ssize_t count = 1 ) with( sem ) {
    606590        count$ = count;
     
    613597        count$ -= 1;
    614598        if ( count$ < 0 ) {                                                                     // in use ?
    615                 append( waiting$, active_thread() );                            // queue current task
    616                 unlock( lock$ );                                                                        // atomically release spin lock and block
     599                append( waiting$, active_thread() );                    // queue current task
     600                unlock( lock$ );                                                                // atomically release spin lock and block
    617601                park();
    618602                return false;
     
    629613                } // if
    630614        } else {
    631                 V( sem );                                                                               // V other semaphore
     615                V( lock );                                                                              // V mutex semaphore
    632616                count$ -= 1;
    633617        } // if
     
    654638}
    655639
    656 bool V( semaphore & sem ) with( sem ) {
     640thread$ * V( semaphore & sem, const bool doUnpark ) with( sem ) {
    657641        thread$ * thrd = 0p;
    658642        lock( lock$ __cfaabi_dbg_ctx2 );
     
    662646        }
    663647        unlock( lock$ );
    664         if ( true ) unpark( thrd );                                             // make new owner
    665 //      thread$ * thrd = V(sem, true);
    666         return thrd != 0p;
     648        if ( doUnpark ) unpark( thrd );                                         // make new owner
     649        return thrd;
    667650}
    668651
  • libcfa/src/concurrency/locks.hfa

    r0048327 rb94579a  
    1111// Created On       : Thu Jan 21 19:46:50 2021
    1212// Last Modified By : Peter A. Buhr
    13 // Last Modified On : Fri Oct 31 09:20:22 2025
    14 // Update Count     : 59
     13// Last Modified On : Wed Nov  5 10:27:45 2025
     14// Update Count     : 61
    1515//
    1616
     
    9191};
    9292
    93 // PRIVATE
    94 //thread$ * V( semaphore & sem, bool );
    95 
    96 // PUBLIC
    9793void ?{}( semaphore & sem, ssize_t count = 1 );
    9894// Return values are used for composition. Calling threads know if a thread is unblocked, which can be useful for
     
    10399bool try_P( semaphore & sem );
    104100static inline bool P( semaphore & sem, semaphore & lock, uintptr_t shadow ) { active_thread()->shadow$ = shadow; return P( sem, lock ); }
    105 bool V( semaphore & sem );
     101thread$ * V( semaphore & sem, const bool doUnpark = true );
     102static inline bool V( semaphore & sem ) with( sem ) { return V( sem, true ) != 0p; }
    106103bool V( semaphore & sem, size_t count );
    107104static inline uintptr_t front( semaphore & sem ) with( sem ) { // return shadow information for first waiting thread
Note: See TracChangeset for help on using the changeset viewer.