{-# language MagicHash #-}
{-# language UnboxedTuples #-}
{-# language DataKinds #-}
{-# language PolyKinds #-}
{-# language RoleAnnotations #-}
{-# language ScopedTypeVariables #-}
{-# language TypeFamilies #-}
{-# language TypeOperators #-}
module Data.Primitive.Unlifted.Weak
( UnliftedWeak_ (..)
, UnliftedWeak
, mkWeakFromUnliftedToUnlifted
, mkWeakToUnlifted
, mkWeakFromUnlifted
, deRefUnliftedWeak
, finalizeUnlifted
, mkUnliftedWeakPtr
, addFinalizerUnlifted
, addCFinalizerToUnliftedWeak1
, addCFinalizerToUnliftedWeak2
, touchUnlifted
) where
import Control.Monad.Primitive (PrimMonad,PrimState,ioToPrim)
import GHC.Exts (RealWorld)
import Data.Primitive.Unlifted.Class (PrimUnlifted)
import Data.Primitive.Unlifted.Weak.IO (UnliftedWeak_ (..), UnliftedWeak)
import qualified Data.Primitive.Unlifted.Weak.IO as W
import qualified System.Mem.Weak as SMW
import Foreign.Ptr (Ptr, FunPtr)
mkWeakFromUnliftedToUnlifted
:: (PrimUnlifted k, PrimUnlifted v, PrimMonad m, PrimState m ~ RealWorld)
=> k -> v -> Maybe (IO ()) -> m (UnliftedWeak v)
{-# INLINE mkWeakFromUnliftedToUnlifted #-}
mkWeakFromUnliftedToUnlifted :: forall k v (m :: * -> *).
(PrimUnlifted k, PrimUnlifted v, PrimMonad m,
PrimState m ~ RealWorld) =>
k -> v -> Maybe (IO ()) -> m (UnliftedWeak v)
mkWeakFromUnliftedToUnlifted k
k v
v Maybe (IO ())
mf = IO (UnliftedWeak v) -> m (UnliftedWeak v)
forall (m :: * -> *) a.
(PrimMonad m, PrimState m ~ RealWorld) =>
IO a -> m a
ioToPrim (IO (UnliftedWeak v) -> m (UnliftedWeak v))
-> IO (UnliftedWeak v) -> m (UnliftedWeak v)
forall a b. (a -> b) -> a -> b
$ k -> v -> Maybe (IO ()) -> IO (UnliftedWeak v)
forall k v.
(PrimUnlifted k, PrimUnlifted v) =>
k -> v -> Maybe (IO ()) -> IO (UnliftedWeak v)
W.mkWeakFromUnliftedToUnlifted k
k v
v Maybe (IO ())
mf
mkWeakToUnlifted
:: (PrimUnlifted v, PrimMonad m, PrimState m ~ RealWorld)
=> k -> v -> Maybe (IO ()) -> m (UnliftedWeak v)
{-# INLINE mkWeakToUnlifted #-}
mkWeakToUnlifted :: forall v (m :: * -> *) k.
(PrimUnlifted v, PrimMonad m, PrimState m ~ RealWorld) =>
k -> v -> Maybe (IO ()) -> m (UnliftedWeak v)
mkWeakToUnlifted k
k v
v Maybe (IO ())
mf = IO (UnliftedWeak v) -> m (UnliftedWeak v)
forall (m :: * -> *) a.
(PrimMonad m, PrimState m ~ RealWorld) =>
IO a -> m a
ioToPrim (IO (UnliftedWeak v) -> m (UnliftedWeak v))
-> IO (UnliftedWeak v) -> m (UnliftedWeak v)
forall a b. (a -> b) -> a -> b
$ k -> v -> Maybe (IO ()) -> IO (UnliftedWeak v)
forall v k.
PrimUnlifted v =>
k -> v -> Maybe (IO ()) -> IO (UnliftedWeak v)
W.mkWeakToUnlifted k
k v
v Maybe (IO ())
mf
mkWeakFromUnlifted
:: (PrimUnlifted k, PrimMonad m, PrimState m ~ RealWorld)
=> k -> v -> Maybe (IO ()) -> m (SMW.Weak v)
{-# INLINE mkWeakFromUnlifted #-}
mkWeakFromUnlifted :: forall k (m :: * -> *) v.
(PrimUnlifted k, PrimMonad m, PrimState m ~ RealWorld) =>
k -> v -> Maybe (IO ()) -> m (Weak v)
mkWeakFromUnlifted k
k v
v Maybe (IO ())
mf = IO (Weak v) -> m (Weak v)
forall (m :: * -> *) a.
(PrimMonad m, PrimState m ~ RealWorld) =>
IO a -> m a
ioToPrim (IO (Weak v) -> m (Weak v)) -> IO (Weak v) -> m (Weak v)
forall a b. (a -> b) -> a -> b
$ k -> v -> Maybe (IO ()) -> IO (Weak v)
forall k v.
PrimUnlifted k =>
k -> v -> Maybe (IO ()) -> IO (Weak v)
W.mkWeakFromUnlifted k
k v
v Maybe (IO ())
mf
deRefUnliftedWeak
:: (PrimUnlifted v, PrimMonad m, PrimState m ~ RealWorld)
=> UnliftedWeak v -> m (Maybe v)
{-# INLINE deRefUnliftedWeak #-}
deRefUnliftedWeak :: forall v (m :: * -> *).
(PrimUnlifted v, PrimMonad m, PrimState m ~ RealWorld) =>
UnliftedWeak v -> m (Maybe v)
deRefUnliftedWeak UnliftedWeak v
w = IO (Maybe v) -> m (Maybe v)
forall (m :: * -> *) a.
(PrimMonad m, PrimState m ~ RealWorld) =>
IO a -> m a
ioToPrim (IO (Maybe v) -> m (Maybe v)) -> IO (Maybe v) -> m (Maybe v)
forall a b. (a -> b) -> a -> b
$ UnliftedWeak v -> IO (Maybe v)
forall v. PrimUnlifted v => UnliftedWeak v -> IO (Maybe v)
W.deRefUnliftedWeak UnliftedWeak v
w
finalizeUnlifted
:: (PrimMonad m, PrimState m ~ RealWorld)
=> UnliftedWeak v -> m ()
{-# INLINE finalizeUnlifted #-}
finalizeUnlifted :: forall (m :: * -> *) v.
(PrimMonad m, PrimState m ~ RealWorld) =>
UnliftedWeak v -> m ()
finalizeUnlifted UnliftedWeak v
w = IO () -> m ()
forall (m :: * -> *) a.
(PrimMonad m, PrimState m ~ RealWorld) =>
IO a -> m a
ioToPrim (IO () -> m ()) -> IO () -> m ()
forall a b. (a -> b) -> a -> b
$ UnliftedWeak v -> IO ()
forall v. UnliftedWeak v -> IO ()
W.finalizeUnlifted UnliftedWeak v
w
mkUnliftedWeakPtr
:: (PrimUnlifted k, PrimMonad m, PrimState m ~ RealWorld)
=> k -> Maybe (IO ()) -> m (UnliftedWeak k)
{-# INLINE mkUnliftedWeakPtr #-}
mkUnliftedWeakPtr :: forall k (m :: * -> *).
(PrimUnlifted k, PrimMonad m, PrimState m ~ RealWorld) =>
k -> Maybe (IO ()) -> m (UnliftedWeak k)
mkUnliftedWeakPtr k
k Maybe (IO ())
fin = IO (UnliftedWeak k) -> m (UnliftedWeak k)
forall (m :: * -> *) a.
(PrimMonad m, PrimState m ~ RealWorld) =>
IO a -> m a
ioToPrim (IO (UnliftedWeak k) -> m (UnliftedWeak k))
-> IO (UnliftedWeak k) -> m (UnliftedWeak k)
forall a b. (a -> b) -> a -> b
$ k -> Maybe (IO ()) -> IO (UnliftedWeak k)
forall k.
PrimUnlifted k =>
k -> Maybe (IO ()) -> IO (UnliftedWeak k)
W.mkUnliftedWeakPtr k
k Maybe (IO ())
fin
addFinalizerUnlifted
:: (PrimUnlifted k, PrimMonad m, PrimState m ~ RealWorld)
=> k -> IO () -> m ()
{-# INLINE addFinalizerUnlifted #-}
addFinalizerUnlifted :: forall k (m :: * -> *).
(PrimUnlifted k, PrimMonad m, PrimState m ~ RealWorld) =>
k -> IO () -> m ()
addFinalizerUnlifted k
k IO ()
fin = IO () -> m ()
forall (m :: * -> *) a.
(PrimMonad m, PrimState m ~ RealWorld) =>
IO a -> m a
ioToPrim (IO () -> m ()) -> IO () -> m ()
forall a b. (a -> b) -> a -> b
$ k -> IO () -> IO ()
forall k. PrimUnlifted k => k -> IO () -> IO ()
W.addFinalizerUnlifted k
k IO ()
fin
addCFinalizerToUnliftedWeak1
:: (PrimMonad m, PrimState m ~ RealWorld)
=> FunPtr (a -> IO ()) -> Ptr a -> UnliftedWeak b -> m Bool
{-# INLINE addCFinalizerToUnliftedWeak1 #-}
addCFinalizerToUnliftedWeak1 :: forall (m :: * -> *) a b.
(PrimMonad m, PrimState m ~ RealWorld) =>
FunPtr (a -> IO ()) -> Ptr a -> UnliftedWeak b -> m Bool
addCFinalizerToUnliftedWeak1 FunPtr (a -> IO ())
f Ptr a
a UnliftedWeak b
w = IO Bool -> m Bool
forall (m :: * -> *) a.
(PrimMonad m, PrimState m ~ RealWorld) =>
IO a -> m a
ioToPrim (IO Bool -> m Bool) -> IO Bool -> m Bool
forall a b. (a -> b) -> a -> b
$ FunPtr (a -> IO ()) -> Ptr a -> UnliftedWeak b -> IO Bool
forall a b.
FunPtr (a -> IO ()) -> Ptr a -> UnliftedWeak b -> IO Bool
W.addCFinalizerToUnliftedWeak1 FunPtr (a -> IO ())
f Ptr a
a UnliftedWeak b
w
addCFinalizerToUnliftedWeak2
:: (PrimMonad m, PrimState m ~ RealWorld)
=> FunPtr (a -> b -> IO ()) -> Ptr a -> Ptr b -> UnliftedWeak c -> m Bool
{-# INLINE addCFinalizerToUnliftedWeak2 #-}
addCFinalizerToUnliftedWeak2 :: forall (m :: * -> *) a b c.
(PrimMonad m, PrimState m ~ RealWorld) =>
FunPtr (a -> b -> IO ())
-> Ptr a -> Ptr b -> UnliftedWeak c -> m Bool
addCFinalizerToUnliftedWeak2 FunPtr (a -> b -> IO ())
f Ptr a
a Ptr b
b UnliftedWeak c
w = IO Bool -> m Bool
forall (m :: * -> *) a.
(PrimMonad m, PrimState m ~ RealWorld) =>
IO a -> m a
ioToPrim (IO Bool -> m Bool) -> IO Bool -> m Bool
forall a b. (a -> b) -> a -> b
$ FunPtr (a -> b -> IO ())
-> Ptr a -> Ptr b -> UnliftedWeak c -> IO Bool
forall a b c.
FunPtr (a -> b -> IO ())
-> Ptr a -> Ptr b -> UnliftedWeak c -> IO Bool
W.addCFinalizerToUnliftedWeak2 FunPtr (a -> b -> IO ())
f Ptr a
a Ptr b
b UnliftedWeak c
w
touchUnlifted
:: (PrimUnlifted a, PrimMonad m, PrimState m ~ RealWorld)
=> a -> m ()
touchUnlifted :: forall a (m :: * -> *).
(PrimUnlifted a, PrimMonad m, PrimState m ~ RealWorld) =>
a -> m ()
touchUnlifted a
a = IO () -> m ()
forall (m :: * -> *) a.
(PrimMonad m, PrimState m ~ RealWorld) =>
IO a -> m a
ioToPrim (IO () -> m ()) -> IO () -> m ()
forall a b. (a -> b) -> a -> b
$ a -> IO ()
forall a. PrimUnlifted a => a -> IO ()
W.touchUnlifted a
a