-----------------------------------------------------------------------------
-- |
-- Module : Control.Concurrent.SampleVar
-----------------------------------------------------------------------------
-- |
-- Module : Control.Concurrent.SampleVar
- emptySampleVar, -- :: SampleVar a -> IO ()
- readSampleVar, -- :: SampleVar a -> IO a
- writeSampleVar, -- :: SampleVar a -> a -> IO ()
- isEmptySampleVar, -- :: SampleVar a -> IO Bool
+ emptySampleVar, -- :: SampleVar a -> IO ()
+ readSampleVar, -- :: SampleVar a -> IO a
+ writeSampleVar, -- :: SampleVar a -> a -> IO ()
+ isEmptySampleVar, -- :: SampleVar a -> IO Bool
-- * Writing to a filled 'SampleVar' overwrites the current value.
-- (different from 'putMVar' on full 'MVar'.)
-- * Writing to a filled 'SampleVar' overwrites the current value.
-- (different from 'putMVar' on full 'MVar'.)
-type SampleVar a
- = MVar (Int, -- 1 == full
- -- 0 == empty
- -- <0 no of readers blocked
- MVar a)
+newtype SampleVar a = SampleVar ( MVar ( Int -- 1 == full
+ -- 0 == empty
+ -- <0 no of readers blocked
+ , MVar a
+ )
+ )
+ deriving (Eq)
+
+INSTANCE_TYPEABLE1(SampleVar,sampleVarTc,"SampleVar")
-- |Build a new, empty, 'SampleVar'
newEmptySampleVar :: IO (SampleVar a)
newEmptySampleVar = do
v <- newEmptyMVar
-- |Build a new, empty, 'SampleVar'
newEmptySampleVar :: IO (SampleVar a)
newEmptySampleVar = do
v <- newEmptyMVar
-- |If the SampleVar is full, leave it empty. Otherwise, do nothing.
emptySampleVar :: SampleVar a -> IO ()
-- |If the SampleVar is full, leave it empty. Otherwise, do nothing.
emptySampleVar :: SampleVar a -> IO ()
-- |Wait for a value to become available, then take it and return.
readSampleVar :: SampleVar a -> IO a
-- |Wait for a value to become available, then take it and return.
readSampleVar :: SampleVar a -> IO a
--
-- filled => make empty and grab sample
-- not filled => try to grab value, empty when read val.
--
(readers,val) <- takeMVar svar
--
-- filled => make empty and grab sample
-- not filled => try to grab value, empty when read val.
--
(readers,val) <- takeMVar svar
takeMVar val
-- |Write a value into the 'SampleVar', overwriting any previous value that
-- was there.
writeSampleVar :: SampleVar a -> a -> IO ()
takeMVar val
-- |Write a value into the 'SampleVar', overwriting any previous value that
-- was there.
writeSampleVar :: SampleVar a -> a -> IO ()
-isEmptySampleVar svar = do
- (readers,val) <- readMVar svar
- return (readers == 0)
+isEmptySampleVar (SampleVar svar) = do
+ (readers, _) <- readMVar svar
+ return (readers <= 0)