From: simonmar Date: Mon, 26 Jan 2004 11:24:54 +0000 (+0000) Subject: [project @ 2004-01-26 11:24:54 by simonmar] X-Git-Tag: nhc98-1-18-release~402 X-Git-Url: http://git.megacz.com/?a=commitdiff_plain;h=8a56be24381b777f3b8b8fe3ceb48b134da7ccf9;p=ghc-base.git [project @ 2004-01-26 11:24:54 by simonmar] - fix bug(s) in emptySampleVar - add isEmptySampleVar, with similar caveats on its use as isEmptyMVar --- diff --git a/Control/Concurrent/SampleVar.hs b/Control/Concurrent/SampleVar.hs index ccb93e1..4d88a19 100644 --- a/Control/Concurrent/SampleVar.hs +++ b/Control/Concurrent/SampleVar.hs @@ -21,7 +21,8 @@ module Control.Concurrent.SampleVar newSampleVar, -- :: a -> IO (SampleVar a) emptySampleVar, -- :: SampleVar a -> IO () readSampleVar, -- :: SampleVar a -> IO a - writeSampleVar -- :: SampleVar a -> a -> IO () + writeSampleVar, -- :: SampleVar a -> a -> IO () + isEmptySampleVar, -- :: SampleVar a -> IO Bool ) where @@ -68,7 +69,8 @@ newSampleVar a = do emptySampleVar :: SampleVar a -> IO () emptySampleVar v = do (readers, var) <- takeMVar v - if readers >= 0 then + if readers > 0 then do + takeMVar var putMVar v (0,var) else putMVar v (readers,var) @@ -100,3 +102,16 @@ writeSampleVar svar v = do _ -> putMVar val v >> putMVar svar (min 1 (readers+1), val) + +-- | Returns 'True' if the 'SampleVar' is currently empty. +-- +-- Note that this function is only useful if you know that no other +-- threads can be modifying the state of the 'SampleVar', because +-- otherwise the state of the 'SampleVar' may have changed by the time +-- you see the result of 'isEmptySampleVar'. +-- +isEmptySampleVar :: SampleVar a -> IO Bool +isEmptySampleVar svar = do + (readers,val) <- readMVar svar + return (readers == 0) +