diff options
author | Ben Gamari <ben@smart-cactus.org> | 2019-11-18 10:49:28 -0500 |
---|---|---|
committer | Ben Gamari <ben@smart-cactus.org> | 2019-11-18 10:50:09 -0500 |
commit | 373eabe9255069033b375d685434902676100def (patch) | |
tree | 99e25223a7dcac5753d524b018b61ccc18ea5e45 | |
parent | 4a37a29b67db078ba29722c5615a37fd8091989b (diff) | |
download | haskell-wip/consolidate-spinlock.tar.gz |
rts: Consolidate spinlock implementationwip/consolidate-spinlock
Previously we had two distinct implementations: one with spinlock
profiling and another without. This seems like needless duplication.
-rw-r--r-- | includes/rts/SpinLock.h | 60 |
1 files changed, 13 insertions, 47 deletions
diff --git a/includes/rts/SpinLock.h b/includes/rts/SpinLock.h index 037975268e..9f09099e2e 100644 --- a/includes/rts/SpinLock.h +++ b/includes/rts/SpinLock.h @@ -23,34 +23,33 @@ #if defined(THREADED_RTS) -#if defined(PROF_SPIN) typedef struct SpinLock_ { StgWord lock; +#if defined(PROF_SPIN) StgWord64 spin; // incremented every time we spin in ACQUIRE_SPIN_LOCK StgWord64 yield; // incremented every time we yield in ACQUIRE_SPIN_LOCK -} SpinLock; -#else -typedef StgWord SpinLock; #endif - -#if defined(PROF_SPIN) +} SpinLock; // PROF_SPIN enables counting the number of times we spin on a lock +#if defined(PROF_SPIN) +#define IF_PROF_SPIN(x) x +#else +#define IF_PROF_SPIN(x) +#endif // acquire spin lock INLINE_HEADER void ACQUIRE_SPIN_LOCK(SpinLock * p) { - StgWord32 r = 0; - uint32_t i; do { - for (i = 0; i < SPIN_COUNT; i++) { - r = cas((StgVolatilePtr)&(p->lock), 1, 0); + for (uint32_t i = 0; i < SPIN_COUNT; i++) { + StgWord32 r = cas((StgVolatilePtr)&(p->lock), 1, 0); if (r != 0) return; - p->spin++; + IF_PROF_SPIN(p->spin++); busy_wait_nop(); } - p->yield++; + IF_PROF_SPIN(p->yield++); yieldThread(); } while (1); } @@ -67,43 +66,10 @@ INLINE_HEADER void initSpinLock(SpinLock * p) { write_barrier(); p->lock = 1; - p->spin = 0; - p->yield = 0; + IF_PROF_SPIN(p->spin = 0); + IF_PROF_SPIN(p->yield = 0); } -#else - -// acquire spin lock -INLINE_HEADER void ACQUIRE_SPIN_LOCK(SpinLock * p) -{ - StgWord32 r = 0; - uint32_t i; - do { - for (i = 0; i < SPIN_COUNT; i++) { - r = cas((StgVolatilePtr)p, 1, 0); - if (r != 0) return; - busy_wait_nop(); - } - yieldThread(); - } while (1); -} - -// release spin lock -INLINE_HEADER void RELEASE_SPIN_LOCK(SpinLock * p) -{ - write_barrier(); - (*p) = 1; -} - -// init spin lock -INLINE_HEADER void initSpinLock(SpinLock * p) -{ - write_barrier(); - (*p) = 1; -} - -#endif /* PROF_SPIN */ - #else /* !THREADED_RTS */ // Using macros here means we don't have to ensure the argument is in scope |