X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=GHC%2FSTRef.lhs;h=9306b2473c8f0a2db7679d2a0c6b85faa319b062;hb=be2750a0a11b919fb03cc070074e430f88bdfa90;hp=7145932627b597bed235140cd99378faa608e916;hpb=260e7f2ed9a43c6ecf5a556d77817f39ed2893ab;p=ghc-base.git diff --git a/GHC/STRef.lhs b/GHC/STRef.lhs index 7145932..9306b24 100644 --- a/GHC/STRef.lhs +++ b/GHC/STRef.lhs @@ -1,28 +1,47 @@ \begin{code} -{-# OPTIONS -fno-implicit-prelude #-} +{-# LANGUAGE NoImplicitPrelude, MagicHash, UnboxedTuples #-} +{-# OPTIONS_HADDOCK hide #-} + +----------------------------------------------------------------------------- +-- | +-- Module : GHC.STRef +-- Copyright : (c) The University of Glasgow, 1994-2002 +-- License : see libraries/base/LICENSE +-- +-- Maintainer : cvs-ghc@haskell.org +-- Stability : internal +-- Portability : non-portable (GHC Extensions) +-- +-- References in the 'ST' monad. +-- +----------------------------------------------------------------------------- + +-- #hide module GHC.STRef where import GHC.ST import GHC.Base data STRef s a = STRef (MutVar# s a) +-- ^ a value of type @STRef s a@ is a mutable variable in state thread @s@, +-- containing a value of type @a@ +-- |Build a new 'STRef' in the current state thread newSTRef :: a -> ST s (STRef s a) newSTRef init = ST $ \s1# -> case newMutVar# init s1# of { (# s2#, var# #) -> (# s2#, STRef var# #) } +-- |Read the value of an 'STRef' readSTRef :: STRef s a -> ST s a readSTRef (STRef var#) = ST $ \s1# -> readMutVar# var# s1# +-- |Write a new value into an 'STRef' writeSTRef :: STRef s a -> a -> ST s () writeSTRef (STRef var#) val = ST $ \s1# -> case writeMutVar# var# val s1# of { s2# -> (# s2#, () #) } -modifySTRef :: STRef s a -> (a -> a) -> ST s () -modifySTRef ref f = readSTRef ref >>= writeSTRef ref . f - -- Just pointer equality on mutable references: instance Eq (STRef s a) where STRef v1# == STRef v2# = sameMutVar# v1# v2#