3 % (c) The GRASP/AQUA Project, Glasgow University, 1995
5 \section[SampleVar]{Sample variables}
7 Sample variables are slightly different from a normal @MVar@:
10 \item Reading an empty @SampleVar@ causes the reader to block.
11 (same as @takeMVar@ on empty @MVar@)
12 \item Reading a filled @SampleVar@ empties it and returns value.
14 \item Writing to an empty @SampleVar@ fills it with a value, and
15 potentially, wakes up a blocked reader (same as for @putMVar@ on empty @MVar@).
16 \item Writing to a filled @SampleVar@ overwrites the current value.
17 (different from @putMVar@ on full @MVar@.)
23 SampleVar, --:: type _ =
25 newSampleVar, --:: IO (SampleVar a)
26 emptySampleVar, --:: SampleVar a -> IO ()
27 readSample, --:: SampleVar a -> IO a
28 writeSample --:: SampleVar a -> a -> IO ()
35 = MVar (Int, -- 1 == full
37 -- <0 no of readers blocked
40 -- Initally, a @SampleVar@ is empty/unfilled.
42 newSampleVar :: IO (SampleVar a)
44 = newEmptyMVar >>= \ val ->
47 emptySampleVar :: SampleVar a -> IO ()
49 = takeMVar v >>= \ (readers,var) ->
53 putMVar v (readers,var)
56 -- filled => make empty and grab sample
57 -- not filled => try to grab value, empty when read val.
59 readSample :: SampleVar a -> IO a
61 = takeMVar svar >>= \ (readers,val) ->
62 putMVar svar (readers-1,val) >>
66 -- filled => overwrite
67 -- not filled => fill, write val
69 writeSample :: SampleVar a -> a -> IO ()
71 = takeMVar svar >>= \ (readers, val) ->
78 putMVar svar (min 1 (readers+1), val)