Changeset 5a05946


Ignore:
Timestamp:
May 15, 2023, 1:14:42 PM (12 months ago)
Author:
caparsons <caparson@…>
Branches:
ADT, ast-experimental, master
Children:
629c95a
Parents:
8cb06b6
Message:

cleanup up locks files and fixed a minor whitespace issue in preemption.cfa

Location:
libcfa/src/concurrency
Files:
3 edited

Legend:

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

    r8cb06b6 r5a05946  
    55// file "LICENCE" distributed with Cforall.
    66//
    7 // locks.hfa -- LIBCFATHREAD
     7// locks.cfa -- LIBCFATHREAD
    88// Runtime locks that used with the runtime thread system.
    99//
     
    113113        return ret;
    114114}
    115 
    116 // static void pop_and_set_new_owner( blocking_lock & this ) with( this ) {
    117 //      thread$ * t = &try_pop_front( blocked_threads );
    118 //      owner = t;
    119 //      recursion_count = ( t ? 1 : 0 );
    120 //      if ( t ) wait_count--;
    121 //      unpark( t );
    122 // }
    123115
    124116static inline void pop_node( blocking_lock & this ) with( this ) {
     
    388380                insert_last( blocked_threads, *i );
    389381                count++;
    390                 // size_t recursion_count = 0;
    391                 // if (i->lock) {
    392                 //      // if lock was passed get recursion count to reset to after waking thread
    393                 //      recursion_count = on_wait( *i->lock );
    394                 // }
    395                 // return recursion_count;
    396382        }
    397383
    398384    static size_t block_and_get_recursion( info_thread(L) & i, __cfa_pre_park pp_fn, void * pp_datum ) {
    399385        size_t recursion_count = 0;
    400                 if ( i.lock ) {
    401                         // if lock was passed get recursion count to reset to after waking thread
     386                if ( i.lock ) // if lock was passed get recursion count to reset to after waking thread
    402387                        recursion_count = on_wait( *i.lock, pp_fn, pp_datum ); // this call blocks
    403                 } else pre_park_then_park( pp_fn, pp_datum );
     388                else
     389            pre_park_then_park( pp_fn, pp_datum );
    404390        return recursion_count;
    405391    }
     
    410396                lock( lock __cfaabi_dbg_ctx2 );
    411397        enqueue_thread( this, &i );
    412                 // size_t recursion_count = queue_and_get_recursion( this, &i );
    413398                unlock( lock );
    414399
    415400                // blocks here
    416401        size_t recursion_count = block_and_get_recursion( i );
    417                 // park( );
    418402
    419403                // resets recursion count here after waking
     
    431415                lock( lock __cfaabi_dbg_ctx2 );
    432416        enqueue_thread( this, &info );
    433                 // size_t recursion_count = queue_and_get_recursion( this, &info );
    434417                alarm_node_wrap(L) node_wrap = { t, 0`s, callback, &this, &info };
    435418                unlock( lock );
    436419
    437                 // registers alarm outside cond lock to avoid deadlock
    438                 // register_self( &node_wrap.alarm_node );
    439 
    440                 // blocks here
     420                // blocks here and registers alarm node before blocking after releasing locks to avoid deadlock
    441421        size_t recursion_count = block_and_get_recursion( info, cond_alarm_register, (void *)(&node_wrap.alarm_node) );
    442422                // park();
     
    548528        bool empty ( pthread_cond_var(L) & this ) with(this) { return blocked_threads`isEmpty; }
    549529
    550         // static size_t queue_and_get_recursion( pthread_cond_var(L) & this, info_thread(L) * i ) with(this) {
    551         //      // add info_thread to waiting queue
    552         //      insert_last( blocked_threads, *i );
    553         //      size_t recursion_count = 0;
    554         //      recursion_count = on_wait( *i->lock );
    555         //      return recursion_count;
    556         // }
    557        
    558530        static void queue_info_thread_timeout( pthread_cond_var(L) & this, info_thread(L) & info, Duration t, Alarm_Callback callback ) with(this) {
    559531                lock( lock __cfaabi_dbg_ctx2 );
    560                 // size_t recursion_count = queue_and_get_recursion(this, &info);
    561532        insert_last( blocked_threads, info );
    562533                pthread_alarm_node_wrap(L) node_wrap = { t, 0`s, callback, &this, &info };
    563534                unlock( lock );
    564535
    565                 // registers alarm outside cond lock to avoid deadlock
    566                 // register_self( &node_wrap.alarm_node ); // C_TODO: fix race: registers itself and then alarm handler calls on_notify before block_and_get_recursion is run
    567 
    568                 // blocks here
     536                // blocks here and registers alarm node before blocking after releasing locks to avoid deadlock
    569537        size_t recursion_count = block_and_get_recursion( info, cond_alarm_register, (void *)(&node_wrap.alarm_node) );
    570                 // park();
    571 
    572                 // unregisters alarm so it doesn't go off if this happens first
     538
     539                // unregisters alarm so it doesn't go off if signal happens first
    573540                unregister_self( &node_wrap.alarm_node );
    574541
     
    585552                info_thread( L ) i = { active_thread(), info, &l };
    586553        insert_last( blocked_threads, i );
    587                 // size_t recursion_count = queue_and_get_recursion( this, &i );
    588554                unlock( lock );
    589555
    590556        // blocks here
    591557                size_t recursion_count = block_and_get_recursion( i );
    592                 // park();
     558
    593559                on_wakeup( *i.lock, recursion_count );
    594560        }
     
    679645        return thrd != 0p;
    680646}
     647
  • libcfa/src/concurrency/locks.hfa

    r8cb06b6 r5a05946  
    3939#include <unistd.h>
    4040
    41 // C_TODO: cleanup this and locks.cfa
    42 // - appropriate separation of interface and impl
    43 // - clean up unused/unneeded locks
    44 // - change messy big blocking lock from inheritance to composition to remove need for flags
    45 
    4641typedef void (*__cfa_pre_park)( void * );
    4742
     
    6661    park();
    6762}
     63
     64// macros for default routine impls for is_blocking_lock trait that do not wait-morph
     65
     66#define DEFAULT_ON_NOTIFY( lock_type ) \
     67    static inline void on_notify( lock_type & this, thread$ * t ){ unpark(t); }
     68
     69#define DEFAULT_ON_WAIT( lock_type ) \
     70    static inline size_t on_wait( lock_type & this, __cfa_pre_park pp_fn, void * pp_datum ) { \
     71        unlock( this ); \
     72        pre_park_then_park( pp_fn, pp_datum ); \
     73        return 0; \
     74    }
     75
     76// on_wakeup impl if lock should be reacquired after waking up
     77#define DEFAULT_ON_WAKEUP_REACQ( lock_type ) \
     78    static inline void on_wakeup( lock_type & this, size_t recursion ) { lock( this ); }
     79
     80// on_wakeup impl if lock will not be reacquired after waking up
     81#define DEFAULT_ON_WAKEUP_NO_REACQ( lock_type ) \
     82    static inline void on_wakeup( lock_type & this, size_t recursion ) {}
     83
     84
    6885
    6986//-----------------------------------------------------------------------------
     
    187204// - Kernel thd blocking alternative to the spinlock
    188205// - No ownership (will deadlock on reacq)
     206// - no reacq on wakeup
    189207struct futex_mutex {
    190208        // lock state any state other than UNLOCKED is locked
     
    200218}
    201219
    202 static inline void  ?{}( futex_mutex & this ) with(this) { val = 0; }
    203 
    204 static inline bool internal_try_lock(futex_mutex & this, int & compare_val) with(this) {
     220static inline void ?{}( futex_mutex & this ) with(this) { val = 0; }
     221
     222static inline bool internal_try_lock( futex_mutex & this, int & compare_val) with(this) {
    205223        return __atomic_compare_exchange_n((int*)&val, (int*)&compare_val, 1, false, __ATOMIC_ACQUIRE, __ATOMIC_ACQUIRE);
    206224}
    207225
    208 static inline int internal_exchange(futex_mutex & this) with(this) {
     226static inline int internal_exchange( futex_mutex & this ) with(this) {
    209227        return __atomic_exchange_n((int*)&val, 2, __ATOMIC_ACQUIRE);
    210228}
     
    240258}
    241259
    242 static inline void on_notify( futex_mutex & f, thread$ * t){ unpark(t); }
    243 static inline size_t on_wait( futex_mutex & this, __cfa_pre_park pp_fn, void * pp_datum ) {
    244     unlock( this );
    245     pre_park_then_park( pp_fn, pp_datum );
    246     return 0;
    247 }
    248 
    249 // to set recursion count after getting signalled;
    250 static inline void on_wakeup( futex_mutex & f, size_t recursion ) {}
     260DEFAULT_ON_NOTIFY( futex_mutex )
     261DEFAULT_ON_WAIT( futex_mutex )
     262DEFAULT_ON_WAKEUP_NO_REACQ( futex_mutex )
    251263
    252264//-----------------------------------------------------------------------------
     
    264276        int val;
    265277};
    266 
    267278static inline void  ?{}( go_mutex & this ) with(this) { val = 0; }
     279// static inline void ?{}( go_mutex & this, go_mutex this2 ) = void; // these don't compile correctly at the moment so they should be omitted
     280// static inline void ?=?( go_mutex & this, go_mutex this2 ) = void;
    268281
    269282static inline bool internal_try_lock(go_mutex & this, int & compare_val, int new_val ) with(this) {
     
    314327}
    315328
    316 static inline void on_notify( go_mutex & f, thread$ * t){ unpark( t ); }
    317 static inline size_t on_wait( go_mutex & this, __cfa_pre_park pp_fn, void * pp_datum ) {
    318     unlock( this );
    319     pre_park_then_park( pp_fn, pp_datum );
    320     return 0;
    321 }
    322 static inline void on_wakeup( go_mutex & f, size_t recursion ) {}
     329DEFAULT_ON_NOTIFY( go_mutex )
     330DEFAULT_ON_WAIT( go_mutex )
     331DEFAULT_ON_WAKEUP_NO_REACQ( go_mutex )
    323332
    324333//-----------------------------------------------------------------------------
     
    340349        this.lock_value = 0;
    341350}
     351static inline void ?{}( exp_backoff_then_block_lock & this, exp_backoff_then_block_lock this2 ) = void;
     352static inline void ?=?( exp_backoff_then_block_lock & this, exp_backoff_then_block_lock this2 ) = void;
    342353
    343354static inline void  ^?{}( exp_backoff_then_block_lock & this ){}
     
    392403}
    393404
    394 static inline void on_notify( exp_backoff_then_block_lock & this, struct thread$ * t ) { unpark( t ); }
    395 static inline size_t on_wait( exp_backoff_then_block_lock & this, __cfa_pre_park pp_fn, void * pp_datum ) {
    396     unlock( this );
    397     pre_park_then_park( pp_fn, pp_datum );
    398     return 0;
    399 }
    400 static inline void on_wakeup( exp_backoff_then_block_lock & this, size_t recursion ) { lock( this ); }
     405DEFAULT_ON_NOTIFY( exp_backoff_then_block_lock )
     406DEFAULT_ON_WAIT( exp_backoff_then_block_lock )
     407DEFAULT_ON_WAKEUP_REACQ( exp_backoff_then_block_lock )
    401408
    402409//-----------------------------------------------------------------------------
     
    454461    unlock( lock );
    455462}
    456 static inline size_t on_wait( fast_block_lock & this, __cfa_pre_park pp_fn, void * pp_datum ) {
    457     unlock( this );
    458     pre_park_then_park( pp_fn, pp_datum );
    459     return 0;
    460 }
    461 static inline void on_wakeup( fast_block_lock & this, size_t recursion ) { }
     463DEFAULT_ON_WAIT( fast_block_lock )
     464DEFAULT_ON_WAKEUP_NO_REACQ( fast_block_lock )
    462465
    463466//-----------------------------------------------------------------------------
     
    661664}
    662665
    663 static inline void on_notify( spin_queue_lock & this, struct thread$ * t ) {
    664         unpark(t);
    665 }
    666 static inline size_t on_wait( spin_queue_lock & this, __cfa_pre_park pp_fn, void * pp_datum ) {
    667     unlock( this );
    668     pre_park_then_park( pp_fn, pp_datum );
    669     return 0;
    670 }
    671 static inline void on_wakeup( spin_queue_lock & this, size_t recursion ) { lock( this ); }
    672 
     666DEFAULT_ON_NOTIFY( spin_queue_lock )
     667DEFAULT_ON_WAIT( spin_queue_lock )
     668DEFAULT_ON_WAKEUP_REACQ( spin_queue_lock )
    673669
    674670//-----------------------------------------------------------------------------
     
    708704}
    709705
    710 static inline void on_notify( mcs_block_spin_lock & this, struct thread$ * t ) { unpark( t ); }
    711 static inline size_t on_wait( mcs_block_spin_lock & this, __cfa_pre_park pp_fn, void * pp_datum ) {
    712     unlock( this );
    713     pre_park_then_park( pp_fn, pp_datum );
    714     return 0;
    715 }
    716 static inline void on_wakeup( mcs_block_spin_lock & this, size_t recursion ) {lock( this ); }
     706DEFAULT_ON_NOTIFY( mcs_block_spin_lock )
     707DEFAULT_ON_WAIT( mcs_block_spin_lock )
     708DEFAULT_ON_WAKEUP_REACQ( mcs_block_spin_lock )
    717709
    718710//-----------------------------------------------------------------------------
     
    765757        unpark(t);
    766758}
    767 static inline size_t on_wait( block_spin_lock & this, __cfa_pre_park pp_fn, void * pp_datum ) {
    768     unlock( this );
    769     pre_park_then_park( pp_fn, pp_datum );
    770     return 0;
    771 }
     759DEFAULT_ON_WAIT( block_spin_lock )
    772760static inline void on_wakeup( block_spin_lock & this, size_t recursion ) with(this) {
    773761        // now we acquire the entire block_spin_lock upon waking up
  • libcfa/src/concurrency/preemption.cfa

    r8cb06b6 r5a05946  
    117117                __cfadbg_print_buffer_decl( preemption, " KERNEL: preemption tick %lu\n", currtime.tn);
    118118                Duration period = node->period;
    119                 if( period == 0) {
     119                if( period == 0 ) {
    120120                        node->set = false;                  // Node is one-shot, just mark it as not pending
    121121                }
Note: See TracChangeset for help on using the changeset viewer.