1 -----------------------------------------------------------------------------
3 -- Module : Control.Concurrent.SampleVar
4 -- Copyright : (c) The University of Glasgow 2001
5 -- License : BSD-style (see the file libraries/base/LICENSE)
7 -- Maintainer : libraries@haskell.org
8 -- Stability : experimental
9 -- Portability : non-portable (concurrency)
13 -----------------------------------------------------------------------------
15 module Control.Concurrent.SampleVar
18 SampleVar, -- :: type _ =
20 newEmptySampleVar, -- :: IO (SampleVar a)
21 newSampleVar, -- :: a -> IO (SampleVar a)
22 emptySampleVar, -- :: SampleVar a -> IO ()
23 readSampleVar, -- :: SampleVar a -> IO a
24 writeSampleVar, -- :: SampleVar a -> a -> IO ()
25 isEmptySampleVar, -- :: SampleVar a -> IO Bool
31 import Control.Concurrent.MVar
33 import Control.Exception ( mask_ )
35 import Data.Functor ( (<$>) )
38 -- Sample variables are slightly different from a normal 'MVar':
40 -- * Reading an empty 'SampleVar' causes the reader to block.
41 -- (same as 'takeMVar' on empty 'MVar')
43 -- * Reading a filled 'SampleVar' empties it and returns value.
44 -- (same as 'takeMVar')
46 -- * Writing to an empty 'SampleVar' fills it with a value, and
47 -- potentially, wakes up a blocked reader (same as for 'putMVar' on
50 -- * Writing to a filled 'SampleVar' overwrites the current value.
51 -- (different from 'putMVar' on full 'MVar'.)
53 newtype SampleVar a = SampleVar ( MVar ( Int -- 1 == full
55 -- <0 no of readers blocked
61 -- |Build a new, empty, 'SampleVar'
62 newEmptySampleVar :: IO (SampleVar a)
63 newEmptySampleVar = do
65 SampleVar <$> newMVar (0,v)
67 -- |Build a 'SampleVar' with an initial value.
68 newSampleVar :: a -> IO (SampleVar a)
71 SampleVar <$> newMVar (1,v)
73 -- |If the SampleVar is full, leave it empty. Otherwise, do nothing.
74 emptySampleVar :: SampleVar a -> IO ()
75 emptySampleVar (SampleVar v) = mask_ $ do
76 s@(readers, var) <- takeMVar v
77 if readers > 0 then do
83 -- |Wait for a value to become available, then take it and return.
84 readSampleVar :: SampleVar a -> IO a
85 readSampleVar (SampleVar svar) = mask_ $ do
87 -- filled => make empty and grab sample
88 -- not filled => try to grab value, empty when read val.
90 (readers,val) <- takeMVar svar
91 let readers' = readers-1
92 readers' `seq` putMVar svar (readers',val)
95 -- |Write a value into the 'SampleVar', overwriting any previous value that
97 writeSampleVar :: SampleVar a -> a -> IO ()
98 writeSampleVar (SampleVar svar) v = mask_ $ do
100 -- filled => overwrite
101 -- not filled => fill, write val
103 s@(readers,val) <- takeMVar svar
110 let readers' = min 1 (readers+1)
111 in readers' `seq` putMVar svar (readers', val)
113 -- | Returns 'True' if the 'SampleVar' is currently empty.
115 -- Note that this function is only useful if you know that no other
116 -- threads can be modifying the state of the 'SampleVar', because
117 -- otherwise the state of the 'SampleVar' may have changed by the time
118 -- you see the result of 'isEmptySampleVar'.
120 isEmptySampleVar :: SampleVar a -> IO Bool
121 isEmptySampleVar (SampleVar svar) = do
122 (readers, _) <- readMVar svar
123 return (readers == 0)