{-# OPTIONS_HADDOCK hide #-}
module Data.IORef.Storable (
IORef,
newIORef,
readIORef,
writeIORef,
modifyIORef,
) where
import Foreign.ForeignPtr
import Foreign.Storable
import GHC.ForeignPtr
data IORef a = IORef {-# UNPACK #-} !(ForeignPtr a)
newIORef :: Storable a => a -> IO (IORef a)
newIORef v = do
fp <- mallocPlainForeignPtr
withForeignPtr fp $ \p -> poke p v
return (IORef fp)
readIORef :: Storable a => IORef a -> IO a
readIORef (IORef fp) = withForeignPtr fp peek
writeIORef :: Storable a => IORef a -> a -> IO ()
writeIORef (IORef fp) v = withForeignPtr fp $ \p -> poke p v
modifyIORef :: Storable a => IORef a -> (a -> a) -> IO ()
modifyIORef ref f = readIORef ref >>= writeIORef ref . f