Remove Control.Parallel*, now in package parallel
[haskell-directory.git] / GHC / STRef.lhs
1 \begin{code}
2 {-# OPTIONS_GHC -fno-implicit-prelude #-}
3 -----------------------------------------------------------------------------
4 -- |
5 -- Module      :  GHC.STRef
6 -- Copyright   :  (c) The University of Glasgow, 1994-2002
7 -- License     :  see libraries/base/LICENSE
8 -- 
9 -- Maintainer  :  cvs-ghc@haskell.org
10 -- Stability   :  internal
11 -- Portability :  non-portable (GHC Extensions)
12 --
13 -- References in the 'ST' monad.
14 --
15 -----------------------------------------------------------------------------
16
17 -- #hide
18 module GHC.STRef where
19
20 import GHC.ST
21 import GHC.Base
22
23 data STRef s a = STRef (MutVar# s a)
24 -- ^ a value of type @STRef s a@ is a mutable variable in state thread @s@,
25 -- containing a value of type @a@
26
27 -- |Build a new 'STRef' in the current state thread
28 newSTRef :: a -> ST s (STRef s a)
29 newSTRef init = ST $ \s1# ->
30     case newMutVar# init s1#            of { (# s2#, var# #) ->
31     (# s2#, STRef var# #) }
32
33 -- |Read the value of an 'STRef'
34 readSTRef :: STRef s a -> ST s a
35 readSTRef (STRef var#) = ST $ \s1# -> readMutVar# var# s1#
36
37 -- |Write a new value into an 'STRef'
38 writeSTRef :: STRef s a -> a -> ST s ()
39 writeSTRef (STRef var#) val = ST $ \s1# ->
40     case writeMutVar# var# val s1#      of { s2# ->
41     (# s2#, () #) }
42
43 -- Just pointer equality on mutable references:
44 instance Eq (STRef s a) where
45     STRef v1# == STRef v2# = sameMutVar# v1# v2#
46 \end{code}