summaryrefslogtreecommitdiff
path: root/libraries/ghc-prim
diff options
context:
space:
mode:
authorTamar Christina <tamar@zhox.com>2019-06-01 11:20:39 +0100
committerMarge Bot <ben+marge-bot@smart-cactus.org>2020-06-14 09:07:44 -0400
commitc0e6dee99242eff08420176a36d77b715972f1f2 (patch)
tree4899be6a2dc500f79b9f67300362ba8a702a6b7a /libraries/ghc-prim
parenta31218f7737a65b6333ec7905e88dc094703f025 (diff)
downloadhaskell-c0e6dee99242eff08420176a36d77b715972f1f2.tar.gz
winio: Add Atomic Exchange PrimOp and implement Atomic Ptr exchanges.
The initial version was rewritten by Tamar Christina. It was rewritten in large parts by Andreas Klebinger. Co-authored-by: Andreas Klebinger <klebinger.andreas@gmx.at>
Diffstat (limited to 'libraries/ghc-prim')
-rw-r--r--libraries/ghc-prim/cbits/atomic.c33
-rw-r--r--libraries/ghc-prim/changelog.md5
2 files changed, 38 insertions, 0 deletions
diff --git a/libraries/ghc-prim/cbits/atomic.c b/libraries/ghc-prim/cbits/atomic.c
index d196ef23c7..18451016ea 100644
--- a/libraries/ghc-prim/cbits/atomic.c
+++ b/libraries/ghc-prim/cbits/atomic.c
@@ -318,6 +318,39 @@ hs_cmpxchg64(StgWord x, StgWord64 old, StgWord64 new)
}
#endif
+// Atomic exchange operations
+
+extern StgWord hs_xchg8(StgWord x, StgWord val);
+StgWord
+hs_xchg8(StgWord x, StgWord val)
+{
+ return (StgWord) __atomic_exchange_n((StgWord8 *) x, (StgWord8) val, __ATOMIC_SEQ_CST);
+}
+
+extern StgWord hs_xchg16(StgWord x, StgWord val);
+StgWord
+hs_xchg16(StgWord x, StgWord val)
+{
+ return (StgWord) __atomic_exchange_n((StgWord16 *)x, (StgWord16) val, __ATOMIC_SEQ_CST);
+}
+
+extern StgWord hs_xchg32(StgWord x, StgWord val);
+StgWord
+hs_xchg32(StgWord x, StgWord val)
+{
+ return (StgWord) __atomic_exchange_n((StgWord32 *) x, (StgWord32) val, __ATOMIC_SEQ_CST);
+}
+
+#if WORD_SIZE_IN_BITS == 64
+//GCC provides this even on 32bit, but StgWord is still 32 bits.
+extern StgWord hs_xchg64(StgWord x, StgWord val);
+StgWord
+hs_xchg64(StgWord x, StgWord val)
+{
+ return (StgWord) __atomic_exchange_n((StgWord64 *) x, (StgWord64) val, __ATOMIC_SEQ_CST);
+}
+#endif
+
// AtomicReadByteArrayOp_Int
// Implies a full memory barrier (see compiler/GHC/Builtin/primops.txt.pp)
// __ATOMIC_SEQ_CST: Full barrier in both directions (hoisting and sinking
diff --git a/libraries/ghc-prim/changelog.md b/libraries/ghc-prim/changelog.md
index 0973959910..effa32479f 100644
--- a/libraries/ghc-prim/changelog.md
+++ b/libraries/ghc-prim/changelog.md
@@ -19,6 +19,11 @@
- Renamed the singleton tuple `GHC.Tuple.Unit` to `GHC.Tuple.Solo`.
+- Add primops for atomic exchange:
+
+ interlockedExchangeAddr# :: Addr# -> Addr# -> State# s -> (# State# s, Addr# #)
+ interlockedExchangeInt# :: Addr# -> Int# -> State# s -> (# State# s, Int# #)
+
## 0.6.1 (edit as necessary)
- Shipped with GHC 8.10.1