
#4001: Implement an atomic readMVar -------------------------------------------+-------------------------------- Reporter: simonmar | Owner: Type: task | Status: new Priority: low | Milestone: 7.6.2 Component: Runtime System | Version: 6.12.2 Keywords: | Os: Unknown/Multiple Architecture: Unknown/Multiple | Failure: None/Unknown Difficulty: Moderate (less than a day) | Testcase: Blockedby: | Blocking: Related: | -------------------------------------------+-------------------------------- Comment(by lpsmith): There is another possible use case here; if GHC implemented both an atomic `readMVar` and `tryReadMVar`, then `Control.Concurrent.Chan.isEmptyChan` could be implemented in a thread- safe way. Admittedly, this would require the receiving end of a `Chan a` to use an external semaphore by changing the `MVar (Stream a)` to a `(IORef (Stream a), MVar ())` pair, so that the current read hole could be obtained without blocking. But the problem is that without atomic `readMVar`s, there is a chance that `isEmptyChan` would observe the stream when another thread is in the process of reading from the thread, and thus say that the channel is empty when in fact it is not. Similarly, without an atomic `tryReadMVar`, there is the chance that `isEmptyChan` would observe the stream when another thread is in the process of calling `isEmptyChan`, and get a faulty empty result. -- Ticket URL: http://hackage.haskell.org/trac/ghc/ticket/4001#comment:10 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler