[Git][ghc/ghc][wip/ioref-swap-xchg] 3 commits: testsuite: Add test for atomicSwapIORef
Ben Gamari (@bgamari)
gitlab at gitlab.haskell.org
Thu Mar 23 20:02:36 UTC 2023
Ben Gamari pushed to branch wip/ioref-swap-xchg at Glasgow Haskell Compiler / GHC
Commits:
c726aa07 by Ben Gamari at 2023-03-23T16:02:31-04:00
testsuite: Add test for atomicSwapIORef
- - - - -
7a2d5890 by Ben Gamari at 2023-03-23T16:02:31-04:00
compiler: Implement atomicSwapIORef with xchg
- - - - -
129e7ff1 by Ben Gamari at 2023-03-23T16:02:31-04:00
Make atomicSwapMutVar# an inline primop
- - - - -
8 changed files:
- compiler/GHC/Builtin/primops.txt.pp
- compiler/GHC/StgToCmm/Prim.hs
- compiler/GHC/StgToJS/Prim.hs
- libraries/base/GHC/IORef.hs
- + libraries/base/tests/AtomicSwapIORef.hs
- + libraries/base/tests/AtomicSwapIORef.stdout
- libraries/base/tests/all.T
- rts/include/Cmm.h
Changes:
=====================================
compiler/GHC/Builtin/primops.txt.pp
=====================================
@@ -2513,6 +2513,12 @@ primop WriteMutVarOp "writeMutVar#" GenPrimOp
has_side_effects = True
code_size = { primOpCodeSizeForeignCall } -- for the write barrier
+primop AtomicSwapMutVarOp "atomicSwapMutVar#" GenPrimOp
+ MutVar# s v -> v -> State# s -> (# State# s, v #)
+ {Atomically exchange the value of a 'MutVar#'.}
+ with
+ has_side_effects = True
+
-- Note [Why not an unboxed tuple in atomicModifyMutVar2#?]
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- Looking at the type of atomicModifyMutVar2#, one might wonder why
=====================================
compiler/GHC/StgToCmm/Prim.hs
=====================================
@@ -308,6 +308,10 @@ emitPrimOp cfg primop =
(closureInfoPtr platform (stgToCmmAlignCheck cfg) mutv))
mkdirtyMutVarCCall
+ AtomicSwapMutVarOp -> \[mutv, val] -> opIntoRegs $ \[res] -> do
+ let dst = cmmOffsetW platform mutv (fixedHdrSizeW profile)
+ emitPrimCall [res] (MO_Xchg (wordWidth platform)) [dst, val]
+
-- #define sizzeofByteArrayzh(r,a) \
-- r = ((StgArrBytes *)(a))->bytes
SizeofByteArrayOp -> \[arg] -> opIntoRegs $ \[res] ->
=====================================
compiler/GHC/StgToJS/Prim.hs
=====================================
@@ -859,6 +859,8 @@ genPrim prof bound ty op = case op of
AtomicModifyMutVar2Op -> \[r1,r2] [m,f] -> PrimInline $ appT [r1,r2] "h$atomicModifyMutVar2" [m,f]
AtomicModifyMutVar_Op -> \[r1,r2] [m,f] -> PrimInline $ appT [r1,r2] "h$atomicModifyMutVar" [m,f]
+ AtomicSwapMutVarOp -> \[r] [mv,v] -> PrimInline $ mconcat
+ [ r |= mv .^ "val", mv .^ "val" |= v ]
CasMutVarOp -> \[status,r] [mv,o,n] -> PrimInline $ ifS (mv .^ "val" .===. o)
(mconcat [status |= zero_, r |= n, mv .^ "val" |= n])
(mconcat [status |= one_ , r |= mv .^ "val"])
=====================================
libraries/base/GHC/IORef.hs
=====================================
@@ -127,12 +127,9 @@ atomicModifyIORef'_ ref f = do
-- | Atomically replace the contents of an 'IORef', returning
-- the old contents.
atomicSwapIORef :: IORef a -> a -> IO a
--- Bad implementation! This will be a primop shortly.
atomicSwapIORef (IORef (STRef ref)) new = IO $ \s ->
- case atomicModifyMutVar2# ref (\_old -> Box new) s of
- (# s', old, Box _new #) -> (# s', old #)
-
-data Box a = Box a
+ case atomicSwapMutVar# ref new s of
+ (# s', old #) -> (# s', old #)
-- | Strict version of 'Data.IORef.atomicModifyIORef'. This forces both
-- the value stored in the 'IORef' and the value returned. The new value
=====================================
libraries/base/tests/AtomicSwapIORef.hs
=====================================
@@ -0,0 +1,8 @@
+import Data.IORef
+import GHC.IORef
+
+main :: IO ()
+main = do
+ r <- newIORef 42 :: IO (IORef Int)
+ atomicSwapIORef r 43
+ readIORef r >>= print
=====================================
libraries/base/tests/AtomicSwapIORef.stdout
=====================================
@@ -0,0 +1 @@
+43
=====================================
libraries/base/tests/all.T
=====================================
@@ -296,3 +296,4 @@ test('T22816', normal, compile_and_run, [''])
test('trace', normal, compile_and_run, [''])
test('listThreads', js_broken(22261), compile_and_run, [''])
test('inits1tails1', normal, compile_and_run, [''])
+test('AtomicSwapIORef', normal, compile_and_run, [''])
=====================================
rts/include/Cmm.h
=====================================
@@ -193,8 +193,10 @@
#if SIZEOF_W == 4
#define cmpxchgW cmpxchg32
+#define xchgW xchg32
#elif SIZEOF_W == 8
#define cmpxchgW cmpxchg64
+#define xchgW xchg64
#endif
/* -----------------------------------------------------------------------------
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/373f22f7e8813caf26556d51978aac2669b4aaf1...129e7ff13e577c68a5084cbdb9978e1ebebbee0c
--
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/compare/373f22f7e8813caf26556d51978aac2669b4aaf1...129e7ff13e577c68a5084cbdb9978e1ebebbee0c
You're receiving this email because of your account on gitlab.haskell.org.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/ghc-commits/attachments/20230323/c9640ff5/attachment-0001.html>
More information about the ghc-commits
mailing list