| type MVar# s elt -- primitive | | newMVar# :: State# s -> (# State# s, MVar# s elt #) | takeMVar# :: SynchVar# s elt -> State# s -> (# State# s, elt #) | putMVar# :: SynchVar# s elt -> State# s -> State# s Bad idea to look at the primops. The important things are the MVar type the operations newMVar, takeMVar, putMVar the forkIO operation Their semantics are described (completely I think) in my "Tackling the awkward squad" tutorial. | As another aside, in the hierarchical libraries docs (package base, | Control.Concurrent.MVar) there appears to be an ambiguity: | | readMVar :: MVar a -> IO a | This is a combination of takeMVar and putMVar; ie. it takes the | value from the MVar, puts it back, and also returns it. | | It specifically avoids describing the combination as atomic, so I | am uncertain whether the thread can be pre-empted between the 'take' | and the 'put'? Normally the answer would be yes, but then there is no | benefit from having a separate 'readMVar' to combine the operations, | so I am guessing that it /should/ be atomic. False. readMVar is completely described by readMVar m = do { v <- takeMVar m; putMVar m v; return v } If it gets pre-empted between the take and the put, no matter; anyone trying to take from that same MVar will block. Simon
"Simon Peyton-Jones" <simonpj@microsoft.com> writes:
| takeMVar# :: SynchVar# s elt -> State# s -> (# State# s, elt #) | putMVar# :: SynchVar# s elt -> State# s -> State# s
Bad idea to look at the primops.
(a) It was the first mention of MVars that I found in the docs. (b) I only really mentioned it because the type sigs are wrong.
| readMVar :: MVar a -> IO a | This is a combination of takeMVar and putMVar; ie. it takes the | value from the MVar, puts it back, and also returns it. | | It specifically avoids describing the combination as atomic ...
False. readMVar is completely described by readMVar m = do { v <- takeMVar m; putMVar m v; return v }
If it gets pre-empted between the take and the put, no matter; anyone trying to take from that same MVar will block.
Doesn't it block if another thread manages to sneak a putMVar into the middle? Maybe I should read your "Awkward Squad" paper to find out if this matters. Regards, Malcolm
participants (2)
-
Malcolm Wallace -
Simon Peyton-Jones