{-# 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 :: k -> v -> Maybe (IO ()) -> IO (Weak v)
mkWeak key :: k
key val :: v
val (Just (IO finalizer :: 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
$ \s :: State# RealWorld
s ->
case 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# k
key v
val State# RealWorld -> (# State# RealWorld, () #)
finalizer State# RealWorld
s of { (# s1 :: State# RealWorld
s1, w :: Weak# v
w #) -> (# State# RealWorld
s1, Weak# v -> Weak v
forall v. Weak# v -> Weak v
Weak Weak# v
w #) }
mkWeak key :: k
key val :: v
val 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
$ \s :: State# RealWorld
s ->
case k -> v -> State# RealWorld -> (# State# RealWorld, Weak# v #)
forall a b.
a -> b -> State# RealWorld -> (# State# RealWorld, Weak# b #)
mkWeakNoFinalizer# k
key v
val State# RealWorld
s of { (# s1 :: State# RealWorld
s1, w :: 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 :: Weak v -> IO (Maybe v)
deRefWeak (Weak w :: 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
$ \s :: 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
(# s1 :: State# RealWorld
s1, flag :: Int#
flag, p :: v
p #) -> case Int#
flag of
0# -> (# State# RealWorld
s1, Maybe v
forall a. Maybe a
Nothing #)
_ -> (# State# RealWorld
s1, v -> Maybe v
forall a. a -> Maybe a
Just v
p #)
finalize :: Weak v -> IO ()
finalize :: Weak v -> IO ()
finalize (Weak w :: 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
$ \s :: 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
(# s1 :: State# RealWorld
s1, 0#, _ #) -> (# State# RealWorld
s1, () #)
(# s1 :: State# RealWorld
s1, _, f :: 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# n :: Int#
n) arr :: Array# (State# RealWorld -> State# RealWorld)
arr =
let go :: Int# -> IO ()
go m :: 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
$ \s :: State# RealWorld
s ->
case Int#
m of
0# -> (# State# RealWorld
s, () #)
_ -> let !m' :: Int#
m' = Int#
m 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 { (# io :: 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# (\p :: State# RealWorld
p -> (# State# RealWorld -> State# RealWorld
io State# RealWorld
p, () #))
(\_ s'' :: State# RealWorld
s'' -> (# State# RealWorld
s'', () #)) State# RealWorld
s of {
(# s' :: 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