fix #4876
[ghc-base.git] / Control / Concurrent / SampleVar.hs
1 -----------------------------------------------------------------------------
2 -- |
3 -- Module      :  Control.Concurrent.SampleVar
4 -- Copyright   :  (c) The University of Glasgow 2001
5 -- License     :  BSD-style (see the file libraries/base/LICENSE)
6 -- 
7 -- Maintainer  :  libraries@haskell.org
8 -- Stability   :  experimental
9 -- Portability :  non-portable (concurrency)
10 --
11 -- Sample variables
12 --
13 -----------------------------------------------------------------------------
14
15 module Control.Concurrent.SampleVar
16        (
17          -- * Sample Variables
18          SampleVar,         -- :: type _ =
19  
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
26
27        ) where
28
29 import Prelude
30
31 import Control.Concurrent.MVar
32
33 import Control.Exception ( mask_ )
34
35 import Data.Functor ( (<$>) )
36
37 -- |
38 -- Sample variables are slightly different from a normal 'MVar':
39 -- 
40 --  * Reading an empty 'SampleVar' causes the reader to block.
41 --    (same as 'takeMVar' on empty 'MVar')
42 -- 
43 --  * Reading a filled 'SampleVar' empties it and returns value.
44 --    (same as 'takeMVar')
45 -- 
46 --  * Writing to an empty 'SampleVar' fills it with a value, and
47 --    potentially, wakes up a blocked reader (same as for 'putMVar' on
48 --    empty 'MVar').
49 --
50 --  * Writing to a filled 'SampleVar' overwrites the current value.
51 --    (different from 'putMVar' on full 'MVar'.)
52
53 newtype SampleVar a = SampleVar ( MVar ( Int    -- 1  == full
54                                                 -- 0  == empty
55                                                 -- <0 no of readers blocked
56                                        , MVar a
57                                        )
58                                 )
59     deriving (Eq)
60
61 -- |Build a new, empty, 'SampleVar'
62 newEmptySampleVar :: IO (SampleVar a)
63 newEmptySampleVar = do
64    v <- newEmptyMVar
65    SampleVar <$> newMVar (0,v)
66
67 -- |Build a 'SampleVar' with an initial value.
68 newSampleVar :: a -> IO (SampleVar a)
69 newSampleVar a = do
70    v <- newMVar a
71    SampleVar <$> newMVar (1,v)
72
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
78      _ <- takeMVar var
79      putMVar v (0,var)
80     else
81      putMVar v s
82
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
86 --
87 -- filled => make empty and grab sample
88 -- not filled => try to grab value, empty when read val.
89 --
90    (readers,val) <- takeMVar svar
91    let readers' = readers-1
92    readers' `seq` putMVar svar (readers',val)
93    takeMVar val
94
95 -- |Write a value into the 'SampleVar', overwriting any previous value that
96 -- was there.
97 writeSampleVar :: SampleVar a -> a -> IO ()
98 writeSampleVar (SampleVar svar) v = mask_ $ do
99 --
100 -- filled => overwrite
101 -- not filled => fill, write val
102 --
103    s@(readers,val) <- takeMVar svar
104    case readers of
105      1 ->
106        swapMVar val v >>
107        putMVar svar s
108      _ ->
109        putMVar val v >>
110        let readers' = min 1 (readers+1)
111        in readers' `seq` putMVar svar (readers', val)
112
113 -- | Returns 'True' if the 'SampleVar' is currently empty.
114 --
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'.
119 --
120 isEmptySampleVar :: SampleVar a -> IO Bool
121 isEmptySampleVar (SampleVar svar) = do
122    (readers, _) <- readMVar svar
123    return (readers <= 0)
124