[GHC] #4001: Implement an atomic readMVar

GHC ghc-devs at haskell.org
Sat Jun 15 16:45:57 CEST 2013


#4001: Implement an atomic readMVar
-------------------------------------------+--------------------------------
    Reporter:  simonmar                    |       Owner:  ezyang          
        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):

 To clarify my previous comment,  one could implement a thread-safe
 `isEmptyChan`[1][2] as follows:

 {{{
 data Chan a = Chan
     { write_end_ref :: MVar (Stream a)
     , read_end_ref  :: IORef (Stream a)
     , read_lock     :: MVar ()
     }

 readChan :: Chan a -> IO a
 readChan chan = do
     modifyMVarMasked (read_lock chan) $ \_ -> do
         read_end <- readIORef (read_end_ref chan)
         (ChItem val new_read_end) <- atomicReadMVar read_end
         writeIORef (read_end_ref chan) new_read_end
         return ((), val)

 isEmptyChan :: Chan a -> IO Bool
 isEmptyChan chan = do
     read_end <- readIORef (read_end_ref chan)
     mval <- atomicTryReadMVar read_end
     return $! case mval of
                 Nothing -> True
                 Just _  -> False
 }}}

 [1] [http://www.haskell.org/ghc/docs/7.6.3/html/libraries/base-4.6.0.1
 /Control-Concurrent-Chan.html#v:isEmptyChan]

 [2] #4154

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



More information about the ghc-tickets mailing list