1 -----------------------------------------------------------------------------
3 -- Module : Control.Concurrent.MVar
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)
11 -- Synchronising variables
13 -----------------------------------------------------------------------------
15 module Control.Concurrent.MVar
19 , newEmptyMVar -- :: IO (MVar a)
20 , newMVar -- :: a -> IO (MVar a)
21 , takeMVar -- :: MVar a -> IO a
22 , putMVar -- :: MVar a -> a -> IO ()
23 , readMVar -- :: MVar a -> IO a
24 , swapMVar -- :: MVar a -> a -> IO a
25 , tryTakeMVar -- :: MVar a -> IO (Maybe a)
26 , tryPutMVar -- :: MVar a -> a -> IO Bool
27 , isEmptyMVar -- :: MVar a -> IO Bool
28 , withMVar -- :: MVar a -> (a -> IO b) -> IO b
29 , modifyMVar_ -- :: MVar a -> (a -> IO a) -> IO ()
30 , modifyMVar -- :: MVar a -> (a -> IO (a,b)) -> IO b
32 , addMVarFinalizer -- :: MVar a -> IO () -> IO ()
37 import Hugs.ConcBase ( MVar, newEmptyMVar, newMVar, takeMVar, putMVar,
38 tryTakeMVar, tryPutMVar, isEmptyMVar,
42 #ifdef __GLASGOW_HASKELL__
43 import GHC.Conc ( MVar, newEmptyMVar, newMVar, takeMVar, putMVar,
44 tryTakeMVar, tryPutMVar, isEmptyMVar, addMVarFinalizer
49 import Control.Exception as Exception
52 This is a combination of 'takeMVar' and 'putMVar'; ie. it takes the value
53 from the 'MVar', puts it back, and also returns it.
55 readMVar :: MVar a -> IO a
62 -- |Swap the contents of an 'MVar' for a new value.
63 swapMVar :: MVar a -> a -> IO a
71 'withMVar' is a safe wrapper for operating on the contents of an
72 'MVar'. This operation is exception-safe: it will replace the
73 original contents of the 'MVar' if an exception is raised (see
76 withMVar :: MVar a -> (a -> IO b) -> IO b
80 b <- Exception.catch (unblock (io a))
81 (\e -> do putMVar m a; throw e)
86 A safe wrapper for modifying the contents of an 'MVar'. Like 'withMVar',
87 'modifyMVar' will replace the original contents of the 'MVar' if an
88 exception is raised during the operation.
90 modifyMVar_ :: MVar a -> (a -> IO a) -> IO ()
94 a' <- Exception.catch (unblock (io a))
95 (\e -> do putMVar m a; throw e)
99 A slight variation on 'modifyMVar_' that allows a value to be
100 returned (@b@) in addition to the modified value of the 'MVar'.
102 modifyMVar :: MVar a -> (a -> IO (a,b)) -> IO b
106 (a',b) <- Exception.catch (unblock (io a))
107 (\e -> do putMVar m a; throw e)