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 ( block )
36 -- Sample variables are slightly different from a normal 'MVar':
38 -- * Reading an empty 'SampleVar' causes the reader to block.
39 -- (same as 'takeMVar' on empty 'MVar')
41 -- * Reading a filled 'SampleVar' empties it and returns value.
42 -- (same as 'takeMVar')
44 -- * Writing to an empty 'SampleVar' fills it with a value, and
45 -- potentially, wakes up a blocked reader (same as for 'putMVar' on
48 -- * Writing to a filled 'SampleVar' overwrites the current value.
49 -- (different from 'putMVar' on full 'MVar'.)
52 = MVar (Int, -- 1 == full
54 -- <0 no of readers blocked
57 -- |Build a new, empty, 'SampleVar'
58 newEmptySampleVar :: IO (SampleVar a)
59 newEmptySampleVar = do
63 -- |Build a 'SampleVar' with an initial value.
64 newSampleVar :: a -> IO (SampleVar a)
69 -- |If the SampleVar is full, leave it empty. Otherwise, do nothing.
70 emptySampleVar :: SampleVar a -> IO ()
71 emptySampleVar v = block $ do
72 s@(readers, var) <- block $ takeMVar v
73 if readers > 0 then do
79 -- |Wait for a value to become available, then take it and return.
80 readSampleVar :: SampleVar a -> IO a
81 readSampleVar svar = block $ do
83 -- filled => make empty and grab sample
84 -- not filled => try to grab value, empty when read val.
86 (readers,val) <- takeMVar svar
87 let readers' = readers-1
88 readers' `seq` putMVar svar (readers',val)
91 -- |Write a value into the 'SampleVar', overwriting any previous value that
93 writeSampleVar :: SampleVar a -> a -> IO ()
94 writeSampleVar svar v = block $ do
96 -- filled => overwrite
97 -- not filled => fill, write val
99 s@(readers,val) <- takeMVar svar
106 let readers' = min 1 (readers+1)
107 in readers' `seq` putMVar svar (readers', val)
109 -- | Returns 'True' if the 'SampleVar' is currently empty.
111 -- Note that this function is only useful if you know that no other
112 -- threads can be modifying the state of the 'SampleVar', because
113 -- otherwise the state of the 'SampleVar' may have changed by the time
114 -- you see the result of 'isEmptySampleVar'.
116 isEmptySampleVar :: SampleVar a -> IO Bool
117 isEmptySampleVar svar = do
118 (readers, _) <- readMVar svar
119 return (readers == 0)