2 #ifdef __GLASGOW_HASKELL__
3 {-# LANGUAGE DeriveDataTypeable, StandaloneDeriving #-}
6 -----------------------------------------------------------------------------
8 -- Module : Control.Concurrent.SampleVar
9 -- Copyright : (c) The University of Glasgow 2001
10 -- License : BSD-style (see the file libraries/base/LICENSE)
12 -- Maintainer : libraries@haskell.org
13 -- Stability : experimental
14 -- Portability : non-portable (concurrency)
18 -----------------------------------------------------------------------------
20 module Control.Concurrent.SampleVar
23 SampleVar, -- :: type _ =
25 newEmptySampleVar, -- :: IO (SampleVar a)
26 newSampleVar, -- :: a -> IO (SampleVar a)
27 emptySampleVar, -- :: SampleVar a -> IO ()
28 readSampleVar, -- :: SampleVar a -> IO a
29 writeSampleVar, -- :: SampleVar a -> a -> IO ()
30 isEmptySampleVar, -- :: SampleVar a -> IO Bool
36 import Control.Concurrent.MVar
38 import Control.Exception ( mask_ )
40 import Data.Functor ( (<$>) )
47 -- Sample variables are slightly different from a normal 'MVar':
49 -- * Reading an empty 'SampleVar' causes the reader to block.
50 -- (same as 'takeMVar' on empty 'MVar')
52 -- * Reading a filled 'SampleVar' empties it and returns value.
53 -- (same as 'takeMVar')
55 -- * Writing to an empty 'SampleVar' fills it with a value, and
56 -- potentially, wakes up a blocked reader (same as for 'putMVar' on
59 -- * Writing to a filled 'SampleVar' overwrites the current value.
60 -- (different from 'putMVar' on full 'MVar'.)
62 newtype SampleVar a = SampleVar ( MVar ( Int -- 1 == full
64 -- <0 no of readers blocked
70 INSTANCE_TYPEABLE1(SampleVar,sampleVarTc,"SampleVar")
72 -- |Build a new, empty, 'SampleVar'
73 newEmptySampleVar :: IO (SampleVar a)
74 newEmptySampleVar = do
76 SampleVar <$> newMVar (0,v)
78 -- |Build a 'SampleVar' with an initial value.
79 newSampleVar :: a -> IO (SampleVar a)
82 SampleVar <$> newMVar (1,v)
84 -- |If the SampleVar is full, leave it empty. Otherwise, do nothing.
85 emptySampleVar :: SampleVar a -> IO ()
86 emptySampleVar (SampleVar v) = mask_ $ do
87 s@(readers, var) <- takeMVar v
88 if readers > 0 then do
94 -- |Wait for a value to become available, then take it and return.
95 readSampleVar :: SampleVar a -> IO a
96 readSampleVar (SampleVar svar) = mask_ $ do
98 -- filled => make empty and grab sample
99 -- not filled => try to grab value, empty when read val.
101 (readers,val) <- takeMVar svar
102 let readers' = readers-1
103 readers' `seq` putMVar svar (readers',val)
106 -- |Write a value into the 'SampleVar', overwriting any previous value that
108 writeSampleVar :: SampleVar a -> a -> IO ()
109 writeSampleVar (SampleVar svar) v = mask_ $ do
111 -- filled => overwrite
112 -- not filled => fill, write val
114 s@(readers,val) <- takeMVar svar
121 let readers' = min 1 (readers+1)
122 in readers' `seq` putMVar svar (readers', val)
124 -- | Returns 'True' if the 'SampleVar' is currently empty.
126 -- Note that this function is only useful if you know that no other
127 -- threads can be modifying the state of the 'SampleVar', because
128 -- otherwise the state of the 'SampleVar' may have changed by the time
129 -- you see the result of 'isEmptySampleVar'.
131 isEmptySampleVar :: SampleVar a -> IO Bool
132 isEmptySampleVar (SampleVar svar) = do
133 (readers, _) <- readMVar svar
134 return (readers <= 0)