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)
import Streamly.Internal.Control.Concurrent (MonadRunInIO, askRunInIO, runInIO, withRunInIO)
newtype IOFinalizer = IOFinalizer (IORef (Maybe (IO ())))
mkIOFinalizer :: MonadRunInIO m => m b -> m (IO ())
mkIOFinalizer :: forall (m :: * -> *) b. MonadRunInIO m => m b -> m (IO ())
mkIOFinalizer m b
f = do
RunInIO m
mrun <- forall (m :: * -> *). MonadRunInIO m => m (RunInIO m)
askRunInIO
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$
forall (f :: * -> *) a. Functor f => f a -> f ()
void forall a b. (a -> b) -> a -> b
$ do
StM m b
_ <- forall (m :: * -> *). RunInIO m -> forall b. m b -> IO (StM m b)
runInIO RunInIO m
mrun m b
f
forall (m :: * -> *) a. Monad m => a -> m a
return ()
runFinalizerGC :: IORef (Maybe (IO ())) -> IO ()
runFinalizerGC :: IORef (Maybe (IO ())) -> IO ()
runFinalizerGC IORef (Maybe (IO ()))
ref = do
Maybe (IO ())
res <- forall a. IORef a -> IO a
readIORef IORef (Maybe (IO ()))
ref
case Maybe (IO ())
res of
Maybe (IO ())
Nothing -> forall (m :: * -> *) a. Monad m => a -> m a
return ()
Just IO ()
f -> IO ()
f
newIOFinalizer :: MonadRunInIO m => m a -> m IOFinalizer
newIOFinalizer :: forall (m :: * -> *) a. MonadRunInIO m => m a -> m IOFinalizer
newIOFinalizer m a
finalizer = do
IO ()
f <- forall (m :: * -> *) b. MonadRunInIO m => m b -> m (IO ())
mkIOFinalizer m a
finalizer
IORef (Maybe (IO ()))
ref <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall a. a -> IO (IORef a)
newIORef forall a b. (a -> b) -> a -> b
$ forall a. a -> Maybe a
Just IO ()
f
Weak (IORef (Maybe (IO ())))
_ <- forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall a. IORef a -> IO () -> IO (Weak (IORef a))
mkWeakIORef IORef (Maybe (IO ()))
ref (IORef (Maybe (IO ())) -> IO ()
runFinalizerGC IORef (Maybe (IO ()))
ref)
forall (m :: * -> *) a. Monad m => a -> m a
return 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) = forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ do
Maybe (IO ())
res <- forall a. IORef a -> IO a
readIORef IORef (Maybe (IO ()))
ref
case Maybe (IO ())
res of
Maybe (IO ())
Nothing -> forall (m :: * -> *) a. Monad m => a -> m a
return ()
Just IO ()
action -> do
forall a. IO a -> IO a
mask_ forall a b. (a -> b) -> a -> b
$ do
forall a. IORef a -> a -> IO ()
writeIORef IORef (Maybe (IO ()))
ref forall a. Maybe a
Nothing
IO ()
action
clearingIOFinalizer :: MonadRunInIO m => IOFinalizer -> m a -> m a
clearingIOFinalizer :: forall (m :: * -> *) a. MonadRunInIO m => IOFinalizer -> m a -> m a
clearingIOFinalizer (IOFinalizer IORef (Maybe (IO ()))
ref) m a
action = do
forall (m :: * -> *) b.
MonadRunInIO m =>
((forall a. m a -> IO (StM m a)) -> IO (StM m b)) -> m b
withRunInIO forall a b. (a -> b) -> a -> b
$ \forall a. m a -> IO (StM m a)
runinio ->
forall a. IO a -> IO a
mask_ forall a b. (a -> b) -> a -> b
$ do
forall a. IORef a -> a -> IO ()
writeIORef IORef (Maybe (IO ()))
ref forall a. Maybe a
Nothing
forall a. m a -> IO (StM m a)
runinio m a
action