3 % (c) The GRASP/AQUA Project, Glasgow University, 1995
5 \section[ChannelVar]{Channel variables}
7 Channel variables, are one-element channels described in the Concurrent
8 Haskell paper (available from @ftp://ftp.dcs.gla.ac.uk/pub/glasgow-fp/drafts@)
16 newCVar, --:: IO (CVar a)
17 putCVar, --:: CVar a -> a -> IO ()
18 getCVar, --:: CVar a -> IO a
27 @MVars@ provide the basic mechanisms for synchronising access to a shared
28 resource. @CVars@, or channel variables, provide an abstraction that guarantee
29 that the producer is not allowed to run riot, but enforces the interleaved
30 access to the channel variable,i.e., a producer is forced to wait up for
31 a consumer to remove the previous value before it can deposit a new one in the @CVar@.
37 = CVar (MVar a) -- prod -> cons
38 (MVar ()) -- cons -> prod
40 newCVar :: IO (CVar a)
41 putCVar :: CVar a -> a -> IO ()
42 getCVar :: CVar a -> IO a
45 = newEmptyMVar >>= \ datum ->
46 newMVar () >>= \ ack ->
47 return (CVar datum ack)
49 putCVar (CVar datum ack) val
54 getCVar (CVar datum ack)
55 = takeMVar datum >>= \ val ->