3 -----------------------------------------------------------------------------
5 -- Module : Control.Concurrent.SampleVar
6 -- Copyright : (c) The University of Glasgow 2001
7 -- License : BSD-style (see the file libraries/base/LICENSE)
9 -- Maintainer : libraries@haskell.org
10 -- Stability : experimental
11 -- Portability : non-portable (concurrency)
15 -----------------------------------------------------------------------------
17 module Control.Concurrent.SampleVar
20 SampleVar, -- :: type _ =
22 newEmptySampleVar, -- :: IO (SampleVar a)
23 newSampleVar, -- :: a -> IO (SampleVar a)
24 emptySampleVar, -- :: SampleVar a -> IO ()
25 readSampleVar, -- :: SampleVar a -> IO a
26 writeSampleVar, -- :: SampleVar a -> a -> IO ()
27 isEmptySampleVar, -- :: SampleVar a -> IO Bool
33 import Control.Concurrent.MVar
35 import Control.Exception ( mask_ )
37 import Data.Functor ( (<$>) )
44 -- Sample variables are slightly different from a normal 'MVar':
46 -- * Reading an empty 'SampleVar' causes the reader to block.
47 -- (same as 'takeMVar' on empty 'MVar')
49 -- * Reading a filled 'SampleVar' empties it and returns value.
50 -- (same as 'takeMVar')
52 -- * Writing to an empty 'SampleVar' fills it with a value, and
53 -- potentially, wakes up a blocked reader (same as for 'putMVar' on
56 -- * Writing to a filled 'SampleVar' overwrites the current value.
57 -- (different from 'putMVar' on full 'MVar'.)
59 newtype SampleVar a = SampleVar ( MVar ( Int -- 1 == full
61 -- <0 no of readers blocked
67 INSTANCE_TYPEABLE1(SampleVar,sampleVarTc,"SampleVar")
69 -- |Build a new, empty, 'SampleVar'
70 newEmptySampleVar :: IO (SampleVar a)
71 newEmptySampleVar = do
73 SampleVar <$> newMVar (0,v)
75 -- |Build a 'SampleVar' with an initial value.
76 newSampleVar :: a -> IO (SampleVar a)
79 SampleVar <$> newMVar (1,v)
81 -- |If the SampleVar is full, leave it empty. Otherwise, do nothing.
82 emptySampleVar :: SampleVar a -> IO ()
83 emptySampleVar (SampleVar v) = mask_ $ do
84 s@(readers, var) <- takeMVar v
85 if readers > 0 then do
91 -- |Wait for a value to become available, then take it and return.
92 readSampleVar :: SampleVar a -> IO a
93 readSampleVar (SampleVar svar) = mask_ $ do
95 -- filled => make empty and grab sample
96 -- not filled => try to grab value, empty when read val.
98 (readers,val) <- takeMVar svar
99 let readers' = readers-1
100 readers' `seq` putMVar svar (readers',val)
103 -- |Write a value into the 'SampleVar', overwriting any previous value that
105 writeSampleVar :: SampleVar a -> a -> IO ()
106 writeSampleVar (SampleVar svar) v = mask_ $ do
108 -- filled => overwrite
109 -- not filled => fill, write val
111 s@(readers,val) <- takeMVar svar
118 let readers' = min 1 (readers+1)
119 in readers' `seq` putMVar svar (readers', val)
121 -- | Returns 'True' if the 'SampleVar' is currently empty.
123 -- Note that this function is only useful if you know that no other
124 -- threads can be modifying the state of the 'SampleVar', because
125 -- otherwise the state of the 'SampleVar' may have changed by the time
126 -- you see the result of 'isEmptySampleVar'.
128 isEmptySampleVar :: SampleVar a -> IO Bool
129 isEmptySampleVar (SampleVar svar) = do
130 (readers, _) <- readMVar svar
131 return (readers <= 0)