[GHC] #13656: Consider implementing atomicModifyMutVar# in Haskell

#13656: Consider implementing atomicModifyMutVar# in Haskell -------------------------------------+------------------------------------- Reporter: dfeuer | Owner: (none) Type: task | Status: new Priority: low | Milestone: 8.4.1 Component: Runtime | Version: 8.2.1-rc2 System | Keywords: | Operating System: Unknown/Multiple Architecture: | Type of failure: None/Unknown Unknown/Multiple | Test Case: | Blocked By: Blocking: | Related Tickets: Differential Rev(s): | Wiki Page: -------------------------------------+------------------------------------- I believe we could implement `atomicModifyMutVar#` in Haskell, rather than making it a primop. This seems like a simpler/cleaner approach (among other things, it gets a perfectly legitimate type signature!), but we'd have to see how it affects performance. I think something like this should work: {{{#!hs atomicModifyMutVar# :: MutVar# s a -> (a -> (a,b)) -> State# s -> (# State# s, b #) atomicModifyMutVar# mv f s = case readMutVar# mv s of { (# s', old #) -> let f_old = f old in case casMutVar# mv old (fst $ f_old) s of { (# s', unchanged, new #) -> case unchanged of 0# -> (# s', snd f_old #) _ -> atomicModifyMutVar# mv f s' }} }}} -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/13656 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#13656: Consider implementing atomicModifyMutVar# in Haskell -------------------------------------+------------------------------------- Reporter: dfeuer | Owner: dfeuer Type: task | Status: new Priority: low | Milestone: 8.4.1 Component: Runtime System | Version: 8.2.1-rc2 Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Changes (by dfeuer): * owner: (none) => dfeuer -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/13656#comment:1 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler

#13656: Consider implementing atomicModifyMutVar# in Haskell -------------------------------------+------------------------------------- Reporter: dfeuer | Owner: dfeuer Type: task | Status: closed Priority: low | Milestone: 8.4.1 Component: Runtime System | Version: 8.2.1-rc2 Resolution: invalid | Keywords: Operating System: Unknown/Multiple | Architecture: | Unknown/Multiple Type of failure: None/Unknown | Test Case: Blocked By: | Blocking: Related Tickets: | Differential Rev(s): Wiki Page: | -------------------------------------+------------------------------------- Changes (by dfeuer): * status: new => closed * resolution: => invalid Comment: fryguybob first explained that the implementation above will allocate like mad. I countered with {{{#!hs atomicModifyMutVar# :: s ~ RealWorld => MutVar# s a -> (a -> (a,b)) -> State# s -> (# State# s, b #) atomicModifyMutVar# mv f s = case readMutVar# mv s of { (# s', old #) -> case newMutVar# old s' of { (# s'', tempy #) -> let thunk = case runRW# $ \ss -> readMutVar# tempy ss of (# _, now #) -> f now in go# tempy mv (fst thunk) (snd thunk) s''}} go# :: s ~ RealWorld => MutVar# s a -> MutVar# s a -> a -> b -> State# s -> (# State# s, b #) go# tempy mv thunka thunkb s = case readMutVar# mv s of { (# s', old #) -> case writeMutVar# tempy old s of { s'' -> case casMutVar# mv old thunka s'' of { (# s''', unchanged, new #) -> case unchanged of 0# -> (# s''', thunkb #) _ -> go# tempy mv thunka thunkb s''' }}} }}} fryguybob then explained that the cmm implementation is able to avoid write barrier inefficiencies that we can't in Haskell. -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/13656#comment:2 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler
participants (1)
-
GHC