2 {-# LANGUAGE NoImplicitPrelude, MagicHash, UnboxedTuples #-}
3 {-# OPTIONS_HADDOCK hide #-}
5 -----------------------------------------------------------------------------
8 -- Copyright : (c) The University of Glasgow, 1994-2002
9 -- License : see libraries/base/LICENSE
11 -- Maintainer : cvs-ghc@haskell.org
12 -- Stability : internal
13 -- Portability : non-portable (GHC Extensions)
15 -- References in the 'ST' monad.
17 -----------------------------------------------------------------------------
20 module GHC.STRef where
25 data STRef s a = STRef (MutVar# s a)
26 -- ^ a value of type @STRef s a@ is a mutable variable in state thread @s@,
27 -- containing a value of type @a@
29 -- |Build a new 'STRef' in the current state thread
30 newSTRef :: a -> ST s (STRef s a)
31 newSTRef init = ST $ \s1# ->
32 case newMutVar# init s1# of { (# s2#, var# #) ->
33 (# s2#, STRef var# #) }
35 -- |Read the value of an 'STRef'
36 readSTRef :: STRef s a -> ST s a
37 readSTRef (STRef var#) = ST $ \s1# -> readMutVar# var# s1#
39 -- |Write a new value into an 'STRef'
40 writeSTRef :: STRef s a -> a -> ST s ()
41 writeSTRef (STRef var#) val = ST $ \s1# ->
42 case writeMutVar# var# val s1# of { s2# ->
45 -- Just pointer equality on mutable references:
46 instance Eq (STRef s a) where
47 STRef v1# == STRef v2# = sameMutVar# v1# v2#