2-- Module : Streamly.Internal.Data.IOFinalizer
3-- Copyright : (c) 2020 Composewell Technologies and Contributors
4-- License : BSD-3-Clause
5-- Maintainer : streamly@composewell.com
6-- Stability : experimental
9-- A value associated with an IO action that is automatically called whenever
10-- the value is garbage collected.
12module Streamly.Internal.Data.IOFinalizer
21import Control.Exception (mask_)
22import Control.Monad (void)
23import Control.Monad.IO.Class (MonadIO(..))
24import Control.Monad.Trans.Control (MonadBaseControl, control)
25import Data.IORef (newIORef, readIORef, mkWeakIORef, writeIORef, IORef)
26import Streamly.Internal.Control.Concurrent (captureMonadState, runInIO)
28-- | An 'IOFinalizer' has an associated IO action that is automatically called
29-- whenever the finalizer is garbage collected. The action can be run and
30-- cleared prematurely.
32-- You can hold a reference to the finalizer in your data structure, if the
33-- data structure gets garbage collected the finalizer will be called.
35-- It is implemented using 'mkWeakIORef'.
38newtype IOFinalizer = IOFinalizer (IORef (Maybe (IO ())))
40-- | Make a finalizer from a monadic action @m a@ that can run in IO monad.
41mkIOFinalizer :: MonadBaseControl IO m => m b -> m (IO ())
43 mrun <- captureMonadState
49-- | GC hook to run an IO action stored in a finalized IORef.
50runFinalizerGC :: IORef (Maybe (IO ())) -> IO ()
51runFinalizerGC ref = do
57-- | Create a finalizer that calls the supplied function automatically when the
58-- it is garbage collected.
60-- /The finalizer is always run using the state of the monad that is captured
61-- at the time of calling 'newFinalizer'./
63-- Note: To run it on garbage collection we have no option but to use the monad
64-- state captured at some earlier point of time. For the case when the
65-- finalizer is run manually before GC we could run it with the current state
66-- of the monad but we want to keep both the cases consistent.
69newIOFinalizer :: (MonadIO m, MonadBaseControl IO m) => m a -> m IOFinalizer
70newIOFinalizer finalizer = do
71 f <- mkIOFinalizer finalizer
72 ref <- liftIO $ newIORef $ Just f
73 _ <- liftIO $ mkWeakIORef ref (runFinalizerGC ref)
74 return $ IOFinalizer ref
76-- | Run the action associated with the finalizer and deactivate it so that it
77-- never runs again. Note, the finalizing action runs with async exceptions
81runIOFinalizer :: MonadIO m => IOFinalizer -> m ()
82runIOFinalizer (IOFinalizer ref) = liftIO $ do
87 -- if an async exception comes after writing 'Nothing' then the
88 -- finalizing action will never be run. We need to do this
89 -- atomically wrt async exceptions.
91 writeIORef ref Nothing
94-- | Run an action clearing the finalizer atomically wrt async exceptions. The
95-- action is run with async exceptions masked.
98clearingIOFinalizer :: MonadBaseControl IO m => IOFinalizer -> m a -> m a
99clearingIOFinalizer (IOFinalizer ref) action = do
100 control $ \runinio ->
102 writeIORef ref Nothing
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list