Ignore:
Timestamp:
May 4, 2022, 3:28:00 PM (2 years ago)
Author:
caparsons <caparson@…>
Branches:
ADT, ast-experimental, master, pthread-emulation, qualifiedEnum
Children:
ec57856
Parents:
f6737ae1
Message:

added fast lock/cond var

File:
1 edited

Legend:

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

    rf6737ae1 r7f958c4  
    220220
    221221//-----------------------------------------------------------------------------
    222 // condition variable
     222// Synchronization Locks
    223223forall(L & | is_blocking_lock(L)) {
    224224
     225        //-----------------------------------------------------------------------------
     226        // condition variable
    225227        void ?{}( condition_variable(L) & this ){
    226228                this.lock{};
     
    337339        bool wait( condition_variable(L) & this, L & l, Duration duration                 ) with(this) { WAIT_TIME( 0   , &l , duration ) }
    338340        bool wait( condition_variable(L) & this, L & l, uintptr_t info, Duration duration ) with(this) { WAIT_TIME( info, &l , duration ) }
     341
     342        //-----------------------------------------------------------------------------
     343        // fast_cond_var
     344        void  ?{}( fast_cond_var(L) & this ){
     345                this.blocked_threads{};
     346                #ifdef __CFA_DEBUG__
     347                this.lock_used = 0p;
     348                #endif
     349        }
     350        void ^?{}( fast_cond_var(L) & this ){ }
     351
     352        bool notify_one( fast_cond_var(L) & this ) with(this) {
     353                bool ret = ! blocked_threads`isEmpty;
     354                if ( ret ) {
     355                        info_thread(L) & popped = try_pop_front( blocked_threads );
     356                        on_notify(*popped.lock, popped.t);
     357                }
     358                return ret;
     359        }
     360        bool notify_all( fast_cond_var(L) & this ) with(this) {
     361                bool ret = ! blocked_threads`isEmpty;
     362                while( ! blocked_threads`isEmpty ) {
     363                        info_thread(L) & popped = try_pop_front( blocked_threads );
     364                        on_notify(*popped.lock, popped.t);
     365                }
     366                return ret;
     367        }
     368
     369        uintptr_t front( fast_cond_var(L) & this ) with(this) { return blocked_threads`isEmpty ? NULL : blocked_threads`first.info; }
     370        bool empty ( fast_cond_var(L) & this ) with(this) { return blocked_threads`isEmpty; }
     371
     372        void wait( fast_cond_var(L) & this, L & l ) {
     373                wait( this, l, 0 );
     374        }
     375
     376        void wait( fast_cond_var(L) & this, L & l, uintptr_t info ) with(this) {
     377                // brand cond lock with lock
     378                #ifdef __CFA_DEBUG__
     379                        if ( lock_used == 0p ) lock_used = &l;
     380                        else { assert(lock_used == &l); }
     381                #endif
     382                info_thread( L ) i = { active_thread(), info, &l };
     383                insert_last( blocked_threads, i );
     384                size_t recursion_count = on_wait( *i.lock );
     385                park( );
     386                on_wakeup(*i.lock, recursion_count);
     387        }
    339388}
    340389
Note: See TracChangeset for help on using the changeset viewer.