+{-# LANGUAGE CPP #-}
+
-----------------------------------------------------------------------------
-- |
-- Module : Control.Concurrent.SampleVar
import Control.Concurrent.MVar
-import Control.Exception ( block )
+import Control.Exception ( mask_ )
import Data.Functor ( (<$>) )
+import Data.Typeable
+
+#include "Typeable.h"
+
-- |
-- Sample variables are slightly different from a normal 'MVar':
--
)
deriving (Eq)
+INSTANCE_TYPEABLE1(SampleVar,sampleVarTc,"SampleVar")
+
-- |Build a new, empty, 'SampleVar'
newEmptySampleVar :: IO (SampleVar a)
newEmptySampleVar = do
-- |If the SampleVar is full, leave it empty. Otherwise, do nothing.
emptySampleVar :: SampleVar a -> IO ()
-emptySampleVar (SampleVar v) = block $ do
- s@(readers, var) <- block $ takeMVar v
+emptySampleVar (SampleVar v) = mask_ $ do
+ s@(readers, var) <- takeMVar v
if readers > 0 then do
_ <- takeMVar var
putMVar v (0,var)
-- |Wait for a value to become available, then take it and return.
readSampleVar :: SampleVar a -> IO a
-readSampleVar (SampleVar svar) = block $ do
+readSampleVar (SampleVar svar) = mask_ $ do
--
-- filled => make empty and grab sample
-- not filled => try to grab value, empty when read val.
-- |Write a value into the 'SampleVar', overwriting any previous value that
-- was there.
writeSampleVar :: SampleVar a -> a -> IO ()
-writeSampleVar (SampleVar svar) v = block $ do
+writeSampleVar (SampleVar svar) v = mask_ $ do
--
-- filled => overwrite
-- not filled => fill, write val
isEmptySampleVar :: SampleVar a -> IO Bool
isEmptySampleVar (SampleVar svar) = do
(readers, _) <- readMVar svar
- return (readers == 0)
+ return (readers <= 0)