Optimizations for mutable structures?
Ian Lynagh
igloo at earth.li
Wed Dec 7 10:13:34 EST 2005
On Wed, Dec 07, 2005 at 02:15:24PM -0000, Simon Marlow wrote:
> On 07 December 2005 13:38, Malcolm Wallace wrote:
>
> > Jan-Willem Maessen <jmaessen at alum.mit.edu> writes:
> >
> >> - Fetch elimination for imperative reads:
> >> writeIORef r e >> acts >> readIORef r
> >> === writeIORef r e >> acts >> return e
> >
> > This transformation is valid only on single-threaded systems.
> > If there is any possibility of an IORef being shared across threads,
> > you are out of luck.
>
> (assuming 'acts' doesn't modify 'r').
>
> No, Jan's transformation is correct even in a multithreaded setting. It
> might eliminate some possible outcomes from a non-deterministic program,
> but that's ok. There's no requirement that all interleavings according
> to the semantics have to be implemented. This is a hard property to
> state precisely, indeed we gave up trying to in the concurrency/FFI
> paper: http://www.haskell.org/~simonmar/papers/conc-ffi.pdf, see Section
> 6.1.
I don't think it's true for this program:
import Data.IORef
import Control.Concurrent
main = do m1 <- newEmptyMVar
m2 <- newEmptyMVar
let e = 6
not_e = 7
acts = putMVar m1 () >> takeMVar m2
r <- newIORef 5
forkIO $ do takeMVar m1
writeIORef r not_e
putMVar m2 ()
writeIORef r e
acts
readIORef r >>= print
Thanks
Ian
More information about the Glasgow-haskell-users
mailing list