diff options
| author | Ben Gamari <ben@smart-cactus.org> | 2020-10-29 16:14:29 -0400 |
|---|---|---|
| committer | Ben Gamari <ben@well-typed.com> | 2020-12-01 12:48:54 -0500 |
| commit | e97ed9201681099b12da4071e95688cc00494ace (patch) | |
| tree | 68ee89988d7022495e39091ebea76962ef25d7ce | |
| parent | 0ea9a980ce452f34259880e060986b5b5a68f0eb (diff) | |
| download | haskell-e97ed9201681099b12da4071e95688cc00494ace.tar.gz | |
rts/SpinLock: Separate out slow path
Not only is this in general a good idea, but it turns out that GCC
unrolls the retry loop, resulting is massive code bloat in critical
parts of the RTS (e.g. `evacuate`).
| -rw-r--r-- | includes/rts/SpinLock.h | 17 | ||||
| -rw-r--r-- | rts/SpinLock.c | 45 | ||||
| -rw-r--r-- | rts/rts.cabal.in | 1 |
3 files changed, 51 insertions, 12 deletions
diff --git a/includes/rts/SpinLock.h b/includes/rts/SpinLock.h index d2ef7979f4..9ffafc1b83 100644 --- a/includes/rts/SpinLock.h +++ b/includes/rts/SpinLock.h @@ -38,21 +38,14 @@ typedef StgWord SpinLock; // PROF_SPIN enables counting the number of times we spin on a lock +void acquire_spin_lock_slow_path(SpinLock * p); + // 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); - if (r != 0) return; - __atomic_fetch_add(&p->spin, 1, __ATOMIC_RELAXED); - busy_wait_nop(); - } - __atomic_fetch_add(&p->yield, 1, __ATOMIC_RELAXED); - yieldThread(); - } while (1); + StgWord32 r = cas((StgVolatilePtr)&(p->lock), 1, 0); + if (RTS_UNLIKELY(r == 0)) + acquire_spin_lock_slow_path(p); } // release spin lock diff --git a/rts/SpinLock.c b/rts/SpinLock.c new file mode 100644 index 0000000000..271502516d --- /dev/null +++ b/rts/SpinLock.c @@ -0,0 +1,45 @@ +/* ---------------------------------------------------------------------------- + * + * (c) The GHC Team, 2006-2009 + * + * Spin locks + * + * These are simple spin-only locks as opposed to Mutexes which + * probably spin for a while before blocking in the kernel. We use + * these when we are sure that all our threads are actively running on + * a CPU, eg. in the GC. + * + * TODO: measure whether we really need these, or whether Mutexes + * would do (and be a bit safer if a CPU becomes loaded). + * + * Do not #include this file directly: #include "Rts.h" instead. + * + * To understand the structure of the RTS headers, see the wiki: + * https://gitlab.haskell.org/ghc/ghc/wikis/commentary/source-tree/includes + * + * -------------------------------------------------------------------------- */ + +#include "PosixSource.h" +#include "Rts.h" + +#if defined(THREADED_RTS) + +void acquire_spin_lock_slow_path(SpinLock * p) +{ + do { + for (uint32_t i = 0; i < SPIN_COUNT; i++) { + StgWord32 r = cas((StgVolatilePtr)&(p->lock), 1, 0); + if (r != 0) return; +#if defined(PROF_SPIN) + __atomic_fetch_add(&p->spin, 1, __ATOMIC_RELAXED); +#endif + busy_wait_nop(); + } +#if defined(PROF_SPIN) + __atomic_fetch_add(&p->yield, 1, __ATOMIC_RELAXED); +#endif + yieldThread(); + } while (1); +} + +#endif diff --git a/rts/rts.cabal.in b/rts/rts.cabal.in index 2279482491..cab390ed30 100644 --- a/rts/rts.cabal.in +++ b/rts/rts.cabal.in @@ -432,6 +432,7 @@ library STM.c Schedule.c Sparks.c + SpinLock.c StableName.c StablePtr.c StaticPtrTable.c |
