1 {-# LANGUAGE CPP, NoImplicitPrelude #-}
3 -----------------------------------------------------------------------------
5 -- Module : Control.Concurrent.MVar
6 -- Copyright : (c) The University of Glasgow 2001
7 -- License : BSD-style (see the file libraries/base/LICENSE)
9 -- Maintainer : libraries@haskell.org
10 -- Stability : experimental
11 -- Portability : non-portable (concurrency)
13 -- An @'MVar' t@ is mutable location that is either empty or contains a
14 -- value of type @t@. It has two fundamental operations: 'putMVar'
15 -- which fills an 'MVar' if it is empty and blocks otherwise, and
16 -- 'takeMVar' which empties an 'MVar' if it is full and blocks
17 -- otherwise. They can be used in multiple different ways:
19 -- 1. As synchronized mutable variables,
20 -- 2. As channels, with 'takeMVar' and 'putMVar' as receive and send, and
21 -- 3. As a binary semaphore @'MVar' ()@, with 'takeMVar' and 'putMVar' as
24 -- They were introduced in the paper "Concurrent Haskell" by Simon
25 -- Peyton Jones, Andrew Gordon and Sigbjorn Finne, though some details
26 -- of their implementation have since then changed (in particular, a
27 -- put on a full MVar used to error, but now merely blocks.)
31 -- 'MVar's offer more flexibility than 'IORef's, but less flexibility
32 -- than 'STM'. They are appropriate for building synchronization
33 -- primitives and performing simple interthread communication; however
34 -- they are very simple and susceptible to race conditions, deadlocks or
35 -- uncaught exceptions. Do not use them if you need perform larger
36 -- atomic operations such as reading from multiple variables: use 'STM'
39 -- In particular, the "bigger" functions in this module ('readMVar',
40 -- 'swapMVar', 'withMVar', 'modifyMVar_' and 'modifyMVar') are simply
41 -- the composition of a 'takeMVar' followed by a 'putMVar' with
43 -- These only have atomicity guarantees if all other threads
44 -- perform a 'takeMVar' before a 'putMVar' as well; otherwise, they may
49 -- No thread can be blocked indefinitely on an 'MVar' unless another
50 -- thread holds that 'MVar' indefinitely. One usual implementation of
51 -- this fairness guarantee is that threads blocked on an 'MVar' are
52 -- served in a first-in-first-out fashion, but this is not guaranteed
57 -- Like many other Haskell data structures, 'MVar's are lazy. This
58 -- means that if you place an expensive unevaluated thunk inside an
59 -- 'MVar', it will be evaluated by the thread that consumes it, not the
60 -- thread that produced it. Be sure to 'evaluate' values to be placed
61 -- in an 'MVar' to the appropriate normal form, or utilize a strict
62 -- MVar provided by the strict-concurrency package.
66 -- 'MVar' operations are always observed to take place in the order
67 -- they are written in the program, regardless of the memory model of
68 -- the underlying machine. This is in contrast to 'IORef' operations
69 -- which may appear out-of-order to another thread in some cases.
73 -- Consider the following concurrent data structure, a skip channel.
74 -- This is a channel for an intermittent source of high bandwidth
75 -- information (for example, mouse movement events.) Writing to the
76 -- channel never blocks, and reading from the channel only returns the
77 -- most recent value, or blocks if there are no new values. Multiple
78 -- readers are supported with a @dupSkipChan@ operation.
80 -- A skip channel is a pair of 'MVar's. The first 'MVar' contains the
81 -- current value, and a list of semaphores that need to be notified
82 -- when it changes. The second 'MVar' is a semaphore for this particular
83 -- reader: it is full if there is a value in the channel that this
84 -- reader has not read yet, and empty otherwise.
87 -- data SkipChan a = SkipChan (MVar (a, [MVar ()])) (MVar ())
89 -- newSkipChan :: IO (SkipChan a)
91 -- sem <- newEmptyMVar
92 -- main <- newMVar (undefined, [sem])
93 -- return (SkipChan main sem)
95 -- putSkipChan :: SkipChan a -> a -> IO ()
96 -- putSkipChan (SkipChan main _) v = do
97 -- (_, sems) <- takeMVar main
98 -- putMVar main (v, [])
99 -- mapM_ (\sem -> putMVar sem ()) sems
101 -- getSkipChan :: SkipChan a -> IO a
102 -- getSkipChan (SkipChan main sem) = do
104 -- (v, sems) <- takeMVar main
105 -- putMVar main (v, sem:sems)
108 -- dupSkipChan :: SkipChan a -> IO (SkipChan a)
109 -- dupSkipChan (SkipChan main _) = do
110 -- sem <- newEmptyMVar
111 -- (v, sems) <- takeMVar main
112 -- putMVar main (v, sem:sems)
113 -- return (SkipChan main sem)
116 -- This example was adapted from the original Concurrent Haskell paper.
117 -- For more examples of 'MVar's being used to build higher-level
118 -- synchronization primitives, see 'Control.Concurrent.Chan' and
119 -- 'Control.Concurrent.QSem'.
121 -----------------------------------------------------------------------------
123 module Control.Concurrent.MVar
127 , newEmptyMVar -- :: IO (MVar a)
128 , newMVar -- :: a -> IO (MVar a)
129 , takeMVar -- :: MVar a -> IO a
130 , putMVar -- :: MVar a -> a -> IO ()
131 , readMVar -- :: MVar a -> IO a
132 , swapMVar -- :: MVar a -> a -> IO a
133 , tryTakeMVar -- :: MVar a -> IO (Maybe a)
134 , tryPutMVar -- :: MVar a -> a -> IO Bool
135 , isEmptyMVar -- :: MVar a -> IO Bool
136 , withMVar -- :: MVar a -> (a -> IO b) -> IO b
137 , modifyMVar_ -- :: MVar a -> (a -> IO a) -> IO ()
138 , modifyMVar -- :: MVar a -> (a -> IO (a,b)) -> IO b
140 , addMVarFinalizer -- :: MVar a -> IO () -> IO ()
145 import Hugs.ConcBase ( MVar, newEmptyMVar, newMVar, takeMVar, putMVar,
146 tryTakeMVar, tryPutMVar, isEmptyMVar,
150 #ifdef __GLASGOW_HASKELL__
151 import GHC.MVar ( MVar, newEmptyMVar, newMVar, takeMVar, putMVar,
152 tryTakeMVar, tryPutMVar, isEmptyMVar, addMVarFinalizer
156 #ifdef __GLASGOW_HASKELL__
162 import Control.Exception.Base
165 This is a combination of 'takeMVar' and 'putMVar'; ie. it takes the value
166 from the 'MVar', puts it back, and also returns it. This function
167 is atomic only if there are no other producers (i.e. threads calling
168 'putMVar') for this 'MVar'.
170 readMVar :: MVar a -> IO a
178 Take a value from an 'MVar', put a new value into the 'MVar' and
179 return the value taken. This function is atomic only if there are
180 no other producers for this 'MVar'.
182 swapMVar :: MVar a -> a -> IO a
190 'withMVar' is an exception-safe wrapper for operating on the contents
191 of an 'MVar'. This operation is exception-safe: it will replace the
192 original contents of the 'MVar' if an exception is raised (see
193 "Control.Exception"). However, it is only atomic if there are no
194 other producers for this 'MVar'.
196 {-# INLINE withMVar #-}
197 -- inlining has been reported to have dramatic effects; see
198 -- http://www.haskell.org//pipermail/haskell/2006-May/017907.html
199 withMVar :: MVar a -> (a -> IO b) -> IO b
201 mask $ \restore -> do
203 b <- restore (io a) `onException` putMVar m a
208 An exception-safe wrapper for modifying the contents of an 'MVar'.
209 Like 'withMVar', 'modifyMVar' will replace the original contents of
210 the 'MVar' if an exception is raised during the operation. This
211 function is only atomic if there are no other producers for this
214 {-# INLINE modifyMVar_ #-}
215 modifyMVar_ :: MVar a -> (a -> IO a) -> IO ()
217 mask $ \restore -> do
219 a' <- restore (io a) `onException` putMVar m a
223 A slight variation on 'modifyMVar_' that allows a value to be
224 returned (@b@) in addition to the modified value of the 'MVar'.
226 {-# INLINE modifyMVar #-}
227 modifyMVar :: MVar a -> (a -> IO (a,b)) -> IO b
229 mask $ \restore -> do
231 (a',b) <- restore (io a) `onException` putMVar m a