{-# language MagicHash #-}
{-# language UnboxedTuples #-}
{-# language LambdaCase #-}
module System.Mem.Weak.IORef
( mkWeakIORef'
, mkWeakIORefPtr
, mkWeakIORefPair
) where
import GHC.Base
import GHC.Weak
import GHC.IORef
import GHC.STRef
mkWeakIORef' :: IORef k -> v -> Maybe (IO ()) -> IO (Weak v)
mkWeakIORef' :: IORef k -> v -> Maybe (IO ()) -> IO (Weak v)
mkWeakIORef' (IORef (STRef MutVar# RealWorld k
k#)) v
v = \case
Maybe (IO ())
Nothing -> (State# RealWorld -> (# State# RealWorld, Weak v #)) -> IO (Weak v)
forall a. (State# RealWorld -> (# State# RealWorld, a #)) -> IO a
IO ((State# RealWorld -> (# State# RealWorld, Weak v #))
-> IO (Weak v))
-> (State# RealWorld -> (# State# RealWorld, Weak v #))
-> IO (Weak v)
forall a b. (a -> b) -> a -> b
$ \State# RealWorld
s -> case MutVar# RealWorld k
-> v -> State# RealWorld -> (# State# RealWorld, Weak# v #)
forall a b.
a -> b -> State# RealWorld -> (# State# RealWorld, Weak# b #)
mkWeakNoFinalizer# MutVar# RealWorld k
k# v
v State# RealWorld
s of (# State# RealWorld
s1, Weak# v
w #) -> (#State# RealWorld
s1, Weak# v -> Weak v
forall v. Weak# v -> Weak v
Weak Weak# v
w #)
Just (IO State# RealWorld -> (# State# RealWorld, () #)
finalizer) -> (State# RealWorld -> (# State# RealWorld, Weak v #)) -> IO (Weak v)
forall a. (State# RealWorld -> (# State# RealWorld, a #)) -> IO a
IO ((State# RealWorld -> (# State# RealWorld, Weak v #))
-> IO (Weak v))
-> (State# RealWorld -> (# State# RealWorld, Weak v #))
-> IO (Weak v)
forall a b. (a -> b) -> a -> b
$ \State# RealWorld
s -> case MutVar# RealWorld k
-> v
-> (State# RealWorld -> (# State# RealWorld, () #))
-> State# RealWorld
-> (# State# RealWorld, Weak# v #)
forall a b c.
a
-> b
-> (State# RealWorld -> (# State# RealWorld, c #))
-> State# RealWorld
-> (# State# RealWorld, Weak# b #)
mkWeak# MutVar# RealWorld k
k# v
v State# RealWorld -> (# State# RealWorld, () #)
finalizer State# RealWorld
s of (# State# RealWorld
s1, Weak# v
w #) -> (#State# RealWorld
s1, Weak# v -> Weak v
forall v. Weak# v -> Weak v
Weak Weak# v
w #)
{-# inlinable mkWeakIORef' #-}
mkWeakIORefPtr :: IORef k -> Maybe (IO ()) -> IO (Weak (IORef k))
mkWeakIORefPtr :: IORef k -> Maybe (IO ()) -> IO (Weak (IORef k))
mkWeakIORefPtr IORef k
k = IORef k -> IORef k -> Maybe (IO ()) -> IO (Weak (IORef k))
forall k v. IORef k -> v -> Maybe (IO ()) -> IO (Weak v)
mkWeakIORef' IORef k
k IORef k
k
{-# inlinable mkWeakIORefPtr #-}
mkWeakIORefPair :: IORef k -> v -> Maybe (IO ()) -> IO (Weak (IORef k, v))
mkWeakIORefPair :: IORef k -> v -> Maybe (IO ()) -> IO (Weak (IORef k, v))
mkWeakIORefPair IORef k
k v
v = IORef k -> (IORef k, v) -> Maybe (IO ()) -> IO (Weak (IORef k, v))
forall k v. IORef k -> v -> Maybe (IO ()) -> IO (Weak v)
mkWeakIORef' IORef k
k (IORef k
k,v
v)
{-# inlinable mkWeakIORefPair #-}