1 {-# OPTIONS_GHC -XNoImplicitPrelude #-}
2 -----------------------------------------------------------------------------
4 -- Module : Control.Concurrent.MVar
5 -- Copyright : (c) The University of Glasgow 2001
6 -- License : BSD-style (see the file libraries/base/LICENSE)
8 -- Maintainer : libraries@haskell.org
9 -- Stability : experimental
10 -- Portability : non-portable (concurrency)
12 -- Synchronising variables
14 -----------------------------------------------------------------------------
16 module Control.Concurrent.MVar
20 , newEmptyMVar -- :: IO (MVar a)
21 , newMVar -- :: a -> IO (MVar a)
22 , takeMVar -- :: MVar a -> IO a
23 , putMVar -- :: MVar a -> a -> IO ()
24 , readMVar -- :: MVar a -> IO a
25 , swapMVar -- :: MVar a -> a -> IO a
26 , tryTakeMVar -- :: MVar a -> IO (Maybe a)
27 , tryPutMVar -- :: MVar a -> a -> IO Bool
28 , isEmptyMVar -- :: MVar a -> IO Bool
29 , withMVar -- :: MVar a -> (a -> IO b) -> IO b
30 , modifyMVar_ -- :: MVar a -> (a -> IO a) -> IO ()
31 , modifyMVar -- :: MVar a -> (a -> IO (a,b)) -> IO b
33 , addMVarFinalizer -- :: MVar a -> IO () -> IO ()
38 import Hugs.ConcBase ( MVar, newEmptyMVar, newMVar, takeMVar, putMVar,
39 tryTakeMVar, tryPutMVar, isEmptyMVar,
43 #ifdef __GLASGOW_HASKELL__
44 import GHC.MVar ( MVar, newEmptyMVar, newMVar, takeMVar, putMVar,
45 tryTakeMVar, tryPutMVar, isEmptyMVar, addMVarFinalizer
49 #ifdef __GLASGOW_HASKELL__
55 import Control.Exception.Base
58 This is a combination of 'takeMVar' and 'putMVar'; ie. it takes the value
59 from the 'MVar', puts it back, and also returns it.
61 readMVar :: MVar a -> IO a
69 Take a value from an 'MVar', put a new value into the 'MVar' and
70 return the value taken. Note that there is a race condition whereby
71 another process can put something in the 'MVar' after the take
72 happens but before the put does.
74 swapMVar :: MVar a -> a -> IO a
82 'withMVar' is a safe wrapper for operating on the contents of an
83 'MVar'. This operation is exception-safe: it will replace the
84 original contents of the 'MVar' if an exception is raised (see
87 {-# INLINE withMVar #-}
88 -- inlining has been reported to have dramatic effects; see
89 -- http://www.haskell.org//pipermail/haskell/2006-May/017907.html
90 withMVar :: MVar a -> (a -> IO b) -> IO b
94 b <- unblock (io a) `onException` putMVar m a
99 A safe wrapper for modifying the contents of an 'MVar'. Like 'withMVar',
100 'modifyMVar' will replace the original contents of the 'MVar' if an
101 exception is raised during the operation.
103 {-# INLINE modifyMVar_ #-}
104 modifyMVar_ :: MVar a -> (a -> IO a) -> IO ()
108 a' <- unblock (io a) `onException` putMVar m a
112 A slight variation on 'modifyMVar_' that allows a value to be
113 returned (@b@) in addition to the modified value of the 'MVar'.
115 {-# INLINE modifyMVar #-}
116 modifyMVar :: MVar a -> (a -> IO (a,b)) -> IO b
120 (a',b) <- unblock (io a) `onException` putMVar m a