Changeset b7763da


Ignore:
Timestamp:
Jul 5, 2021, 2:21:50 PM (13 months ago)
Author:
caparsons <caparson@…>
Branches:
enum, forall-pointer-decay, jacob/cs343-translation, master, new-ast-unique-expr, pthread-emulation
Children:
55ad35c, ee23a8d
Parents:
d5f6a14
Message:

added martin lock and improvement

File:
1 edited

Legend:

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

    rd5f6a14 rb7763da  
    274274        this.yield_count = yield_count;
    275275}
    276 static inline void  ?{}( linear_backoff_then_block_lock & this ) { this{4, 1024, 16, 0}; }
     276static inline void  ?{}( linear_backoff_then_block_lock & this ) { this{4, 1024, 16, 0};
     277printf("lock_ctor: %p\n", &this); }
    277278static inline void ^?{}( linear_backoff_then_block_lock & this ) {}
    278279
     
    346347}
    347348
     349static inline bool lock_improved(linear_backoff_then_block_lock & this) with(this) {
     350        // if owner just return
     351        if (active_thread() == owner) return true;
     352        size_t compare_val = 0;
     353        int spin = spin_start;
     354        // linear backoff
     355        for( ;; ) {
     356                compare_val = 0;
     357                if (internal_try_lock(this, compare_val)) return true;
     358                if (2 == compare_val) break;
     359                for (int i = 0; i < spin; i++) Pause();
     360                if (spin >= spin_end) break;
     361                spin += spin;
     362        }
     363
     364        // linear backoff bounded by spin_count
     365        spin = spin_start;
     366        int spin_counter = 0;
     367        int yield_counter = 0;
     368        for ( ;; ) {
     369                compare_val = 0;
     370                if(internal_try_lock(this, compare_val)) return true;
     371                if (2 == compare_val) break;
     372                if(spin_counter < spin_count) {
     373                        for (int i = 0; i < spin; i++) Pause();
     374                        if (spin < spin_end) spin += spin;
     375                        else spin_counter++;
     376                } else if (yield_counter < yield_count) {
     377                        // after linear backoff yield yield_count times
     378                        yield_counter++;
     379                        yield();
     380                } else { break; }
     381        }
     382
     383        if(2 != compare_val && try_lock_contention(this)) return true;
     384        // block until signalled
     385        while (block(this)) if(try_lock_contention(this)) return true;
     386       
     387        // this should never be reached as block(this) always returns true
     388        return false;
     389}
     390
    348391static inline void unlock(linear_backoff_then_block_lock & this) with(this) {
    349392        verify(lock_value > 0);
     
    358401static inline void on_notify(linear_backoff_then_block_lock & this, struct $thread * t ) { unpark(t); }
    359402static inline size_t on_wait(linear_backoff_then_block_lock & this) { unlock(this); return 0; }
    360 static inline void on_wakeup(linear_backoff_then_block_lock & this, size_t recursion ) { lock(this); }
     403static inline void on_wakeup(linear_backoff_then_block_lock & this, size_t recursion ) { lock_improved(this); }
    361404
    362405//-----------------------------------------------------------------------------
Note: See TracChangeset for help on using the changeset viewer.