{-# LANGUAGE Unsafe #-}
{-# LANGUAGE NoImplicitPrelude
, BangPatterns
, MagicHash
, UnboxedTuples
, StandaloneDeriving
#-}
{-# OPTIONS_HADDOCK not-home #-}
module GHC.Weak (
Weak(..),
mkWeak,
deRefWeak,
finalize,
runFinalizerBatch
) where
import GHC.Base
data Weak v = Weak (Weak# v)
mkWeak :: k
-> v
-> Maybe (IO ())
-> IO (Weak v)
mkWeak :: forall k v. k -> v -> Maybe (IO ()) -> IO (Weak v)
mkWeak k
key v
val (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 k
-> v
-> (State# RealWorld -> (# State# RealWorld, () #))
-> State# RealWorld
-> (# State# RealWorld, Weak# v #)
mkWeak# k
key v
val 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 #) }
mkWeak k
key v
val 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 k -> v -> State# RealWorld -> (# State# RealWorld, Weak# v #)
mkWeakNoFinalizer# k
key v
val 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 #) }
deRefWeak :: Weak v -> IO (Maybe v)
deRefWeak :: forall v. Weak v -> IO (Maybe v)
deRefWeak (Weak Weak# v
w) = (State# RealWorld -> (# State# RealWorld, Maybe v #))
-> IO (Maybe v)
forall a. (State# RealWorld -> (# State# RealWorld, a #)) -> IO a
IO ((State# RealWorld -> (# State# RealWorld, Maybe v #))
-> IO (Maybe v))
-> (State# RealWorld -> (# State# RealWorld, Maybe v #))
-> IO (Maybe v)
forall a b. (a -> b) -> a -> b
$ \State# RealWorld
s ->
case Weak# v -> State# RealWorld -> (# State# RealWorld, Int#, v #)
forall a.
Weak# a -> State# RealWorld -> (# State# RealWorld, Int#, a #)
deRefWeak# Weak# v
w State# RealWorld
s of
(# State# RealWorld
s1, Int#
flag, v
p #) -> case Int#
flag of
Int#
0# -> (# State# RealWorld
s1, Maybe v
forall a. Maybe a
Nothing #)
Int#
_ -> (# State# RealWorld
s1, v -> Maybe v
forall a. a -> Maybe a
Just v
p #)
finalize :: Weak v -> IO ()
finalize :: forall v. Weak v -> IO ()
finalize (Weak Weak# v
w) = (State# RealWorld -> (# State# RealWorld, () #)) -> IO ()
forall a. (State# RealWorld -> (# State# RealWorld, a #)) -> IO a
IO ((State# RealWorld -> (# State# RealWorld, () #)) -> IO ())
-> (State# RealWorld -> (# State# RealWorld, () #)) -> IO ()
forall a b. (a -> b) -> a -> b
$ \State# RealWorld
s ->
case Weak# v
-> State# RealWorld
-> (# State# RealWorld, Int#,
State# RealWorld -> (# State# RealWorld, () #) #)
forall a b.
Weak# a
-> State# RealWorld
-> (# State# RealWorld, Int#,
State# RealWorld -> (# State# RealWorld, b #) #)
finalizeWeak# Weak# v
w State# RealWorld
s of
(# State# RealWorld
s1, Int#
0#, State# RealWorld -> (# State# RealWorld, () #)
_ #) -> (# State# RealWorld
s1, () #)
(# State# RealWorld
s1, Int#
_, State# RealWorld -> (# State# RealWorld, () #)
f #) -> State# RealWorld -> (# State# RealWorld, () #)
f State# RealWorld
s1
runFinalizerBatch :: Int -> Array# (State# RealWorld -> State# RealWorld)
-> IO ()
runFinalizerBatch :: Int -> Array# (State# RealWorld -> State# RealWorld) -> IO ()
runFinalizerBatch (I# Int#
n) Array# (State# RealWorld -> State# RealWorld)
arr =
let go :: Int# -> IO ()
go Int#
m = (State# RealWorld -> (# State# RealWorld, () #)) -> IO ()
forall a. (State# RealWorld -> (# State# RealWorld, a #)) -> IO a
IO ((State# RealWorld -> (# State# RealWorld, () #)) -> IO ())
-> (State# RealWorld -> (# State# RealWorld, () #)) -> IO ()
forall a b. (a -> b) -> a -> b
$ \State# RealWorld
s ->
case Int#
m of
Int#
0# -> (# State# RealWorld
s, () #)
Int#
_ -> let !m' :: Int#
m' = Int#
m Int# -> Int# -> Int#
-# Int#
1# in
case Array# (State# RealWorld -> State# RealWorld)
-> Int# -> (# State# RealWorld -> State# RealWorld #)
forall a. Array# a -> Int# -> (# a #)
indexArray# Array# (State# RealWorld -> State# RealWorld)
arr Int#
m' of { (# State# RealWorld -> State# RealWorld
io #) ->
case (State# RealWorld -> (# State# RealWorld, () #))
-> (Any -> State# RealWorld -> (# State# RealWorld, () #))
-> State# RealWorld
-> (# State# RealWorld, () #)
forall a b.
(State# RealWorld -> (# State# RealWorld, a #))
-> (b -> State# RealWorld -> (# State# RealWorld, a #))
-> State# RealWorld
-> (# State# RealWorld, a #)
catch# (\State# RealWorld
p -> (# State# RealWorld -> State# RealWorld
io State# RealWorld
p, () #))
(\Any
_ State# RealWorld
s'' -> (# State# RealWorld
s'', () #)) State# RealWorld
s of {
(# State# RealWorld
s', ()
_ #) -> IO () -> State# RealWorld -> (# State# RealWorld, () #)
forall a. IO a -> State# RealWorld -> (# State# RealWorld, a #)
unIO (Int# -> IO ()
go Int#
m') State# RealWorld
s'
}}
in
Int# -> IO ()
go Int#
n