module Streamly.Internal.Data.IOFinalizer
(
IOFinalizer(..)
, newIOFinalizer
, runIOFinalizer
, clearingIOFinalizer
)
where
import Control.Exception (mask_)
import Control.Monad (void)
import Control.Monad.IO.Class (MonadIO(..))
import Data.IORef (newIORef, readIORef, mkWeakIORef, writeIORef, IORef)
newtype IOFinalizer = IOFinalizer (IORef (Maybe (IO ())))
runFinalizerGC :: IORef (Maybe (IO ())) -> IO ()
runFinalizerGC :: IORef (Maybe (IO ())) -> IO ()
runFinalizerGC IORef (Maybe (IO ()))
ref = do
Maybe (IO ())
res <- IORef (Maybe (IO ())) -> IO (Maybe (IO ()))
forall a. IORef a -> IO a
readIORef IORef (Maybe (IO ()))
ref
case Maybe (IO ())
res of
Maybe (IO ())
Nothing -> () -> IO ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
Just IO ()
f -> IO ()
f
newIOFinalizer :: MonadIO m => IO a -> m IOFinalizer
newIOFinalizer :: forall (m :: * -> *) a. MonadIO m => IO a -> m IOFinalizer
newIOFinalizer IO a
finalizer = IO IOFinalizer -> m IOFinalizer
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO IOFinalizer -> m IOFinalizer)
-> IO IOFinalizer -> m IOFinalizer
forall a b. (a -> b) -> a -> b
$ do
let f :: IO ()
f = IO a -> IO ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void IO a
finalizer
IORef (Maybe (IO ()))
ref <- Maybe (IO ()) -> IO (IORef (Maybe (IO ())))
forall a. a -> IO (IORef a)
newIORef (Maybe (IO ()) -> IO (IORef (Maybe (IO ()))))
-> Maybe (IO ()) -> IO (IORef (Maybe (IO ())))
forall a b. (a -> b) -> a -> b
$ IO () -> Maybe (IO ())
forall a. a -> Maybe a
Just IO ()
f
Weak (IORef (Maybe (IO ())))
_ <- IORef (Maybe (IO ())) -> IO () -> IO (Weak (IORef (Maybe (IO ()))))
forall a. IORef a -> IO () -> IO (Weak (IORef a))
mkWeakIORef IORef (Maybe (IO ()))
ref (IORef (Maybe (IO ())) -> IO ()
runFinalizerGC IORef (Maybe (IO ()))
ref)
IOFinalizer -> IO IOFinalizer
forall (m :: * -> *) a. Monad m => a -> m a
return (IOFinalizer -> IO IOFinalizer) -> IOFinalizer -> IO IOFinalizer
forall a b. (a -> b) -> a -> b
$ IORef (Maybe (IO ())) -> IOFinalizer
IOFinalizer IORef (Maybe (IO ()))
ref
runIOFinalizer :: MonadIO m => IOFinalizer -> m ()
runIOFinalizer :: forall (m :: * -> *). MonadIO m => IOFinalizer -> m ()
runIOFinalizer (IOFinalizer IORef (Maybe (IO ()))
ref) = IO () -> m ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ()) -> IO () -> m ()
forall a b. (a -> b) -> a -> b
$ do
Maybe (IO ())
res <- IORef (Maybe (IO ())) -> IO (Maybe (IO ()))
forall a. IORef a -> IO a
readIORef IORef (Maybe (IO ()))
ref
case Maybe (IO ())
res of
Maybe (IO ())
Nothing -> () -> IO ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
Just IO ()
action -> do
IO () -> IO ()
forall a. IO a -> IO a
mask_ (IO () -> IO ()) -> IO () -> IO ()
forall a b. (a -> b) -> a -> b
$ do
IORef (Maybe (IO ())) -> Maybe (IO ()) -> IO ()
forall a. IORef a -> a -> IO ()
writeIORef IORef (Maybe (IO ()))
ref Maybe (IO ())
forall a. Maybe a
Nothing
IO ()
action
clearingIOFinalizer :: MonadIO m => IOFinalizer -> IO a -> m a
clearingIOFinalizer :: forall (m :: * -> *) a. MonadIO m => IOFinalizer -> IO a -> m a
clearingIOFinalizer (IOFinalizer IORef (Maybe (IO ()))
ref) IO a
action = do
IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO a -> m a) -> IO a -> m a
forall a b. (a -> b) -> a -> b
$ IO a -> IO a
forall a. IO a -> IO a
mask_ (IO a -> IO a) -> IO a -> IO a
forall a b. (a -> b) -> a -> b
$ do
Maybe (IO ())
res <- IORef (Maybe (IO ())) -> IO (Maybe (IO ()))
forall a. IORef a -> IO a
readIORef IORef (Maybe (IO ()))
ref
case Maybe (IO ())
res of
Just IO ()
_ -> do
IORef (Maybe (IO ())) -> Maybe (IO ()) -> IO ()
forall a. IORef a -> a -> IO ()
writeIORef IORef (Maybe (IO ()))
ref Maybe (IO ())
forall a. Maybe a
Nothing
IO a
action
Maybe (IO ())
Nothing -> [Char] -> IO a
forall a. HasCallStack => [Char] -> a
error [Char]
"clearingIOFinalizer: finalizer not set"