#4876: isEmptySampleVar returns False when threads are waiting on an empty
SampleVar
---------------------------------+------------------------------------------
    Reporter:  guest             |       Owner:                             
        Type:  bug               |      Status:  new                        
    Priority:  normal            |   Component:  libraries/base             
     Version:  7.0.1             |    Keywords:                             
    Testcase:                    |   Blockedby:                             
          Os:  Unknown/Multiple  |    Blocking:                             
Architecture:  Unknown/Multiple  |     Failure:  Incorrect result at runtime
---------------------------------+------------------------------------------
 The source code for Control.Concurrent.SampleVar.isEmptySampleVar:

 {{{
 isEmptySampleVar :: SampleVar a -> IO Bool
 isEmptySampleVar (SampleVar svar) = do
    (readers, _) <- readMVar svar
    return (readers == 0)
 }}}

 should have readers <= 0, as the state readers < 0 is used to indicate the
 SampleVar is empty and has threads waiting on it.

 As an example:

 {{{
 import System.Random
 import Control.Concurrent.SampleVar
 import Control.Concurrent

 do_something = threadDelay 100000     -- 100 ms
 loop body = body >> loop body

 produce, consume :: SampleVar Int -> IO ()
 produce svar = do
    do_something
    b <- isEmptySampleVar svar
    if b then randomIO >>= writeSampleVar svar else return ()

 consume svar = readSampleVar svar >>= print

 main = do
    svar <- newEmptySampleVar
    forkIO $ loop $ produce svar
    forkIO $ loop $ consume svar
    threadDelay 1000000       -- one second
 }}}

 This code deadlocks instead of printing random numbers.

-- 
Ticket URL: <http://hackage.haskell.org/trac/ghc/ticket/4876>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler

_______________________________________________
Glasgow-haskell-bugs mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/glasgow-haskell-bugs

Reply via email to