resourcet-1.1.8: Deterministic allocation and freeing of scarce resources.

Safe HaskellSafe
LanguageHaskell98

Control.Monad.Trans.Resource

Contents

Description

Allocate resources which are guaranteed to be released.

For more information, see https://www.fpcomplete.com/user/snoyberg/library-documentation/resourcet.

One point to note: all register cleanup actions live in the IO monad, not the main monad. This allows both more efficient code, and for monads to be transformed.

Synopsis

Data types

data ResourceT m a Source #

The Resource transformer. This transformer keeps track of all registered actions, and calls them upon exit (via runResourceT). Actions may be registered via register, or resources may be allocated atomically via allocate. allocate corresponds closely to bracket.

Releasing may be performed before exit via the release function. This is a highly recommended optimization, as it will ensure that scarce resources are freed early. Note that calling release will deregister the action, so that a release action will only ever be called once.

Since 0.3.0

Instances

MFunctor ResourceT Source #

Since 0.4.7

Methods

hoist :: Monad m => (forall a. m a -> n a) -> ResourceT m b -> ResourceT n b #

MMonad ResourceT Source #

Since 0.4.7

Methods

embed :: Monad n => (forall a. m a -> ResourceT n a) -> ResourceT m b -> ResourceT n b #

MonadTrans ResourceT Source # 

Methods

lift :: Monad m => m a -> ResourceT m a #

MonadTransControl ResourceT Source # 

Associated Types

type StT (ResourceT :: (* -> *) -> * -> *) a :: * #

Methods

liftWith :: Monad m => (Run ResourceT -> m a) -> ResourceT m a #

restoreT :: Monad m => m (StT ResourceT a) -> ResourceT m a #

MonadRWS r w s m => MonadRWS r w s (ResourceT m) Source # 
MonadBase b m => MonadBase b (ResourceT m) Source # 

Methods

liftBase :: b α -> ResourceT m α #

MonadBaseControl b m => MonadBaseControl b (ResourceT m) Source # 

Associated Types

type StM (ResourceT m :: * -> *) a :: * #

Methods

liftBaseWith :: (RunInBase (ResourceT m) b -> b a) -> ResourceT m a #

restoreM :: StM (ResourceT m) a -> ResourceT m a #

MonadError e m => MonadError e (ResourceT m) Source # 

Methods

throwError :: e -> ResourceT m a #

catchError :: ResourceT m a -> (e -> ResourceT m a) -> ResourceT m a #

MonadReader r m => MonadReader r (ResourceT m) Source # 

Methods

ask :: ResourceT m r #

local :: (r -> r) -> ResourceT m a -> ResourceT m a #

reader :: (r -> a) -> ResourceT m a #

MonadState s m => MonadState s (ResourceT m) Source # 

Methods

get :: ResourceT m s #

put :: s -> ResourceT m () #

state :: (s -> (a, s)) -> ResourceT m a #

MonadWriter w m => MonadWriter w (ResourceT m) Source # 

Methods

writer :: (a, w) -> ResourceT m a #

tell :: w -> ResourceT m () #

listen :: ResourceT m a -> ResourceT m (a, w) #

pass :: ResourceT m (a, w -> w) -> ResourceT m a #

Monad m => Monad (ResourceT m) Source # 

Methods

(>>=) :: ResourceT m a -> (a -> ResourceT m b) -> ResourceT m b #

(>>) :: ResourceT m a -> ResourceT m b -> ResourceT m b #

return :: a -> ResourceT m a #

fail :: String -> ResourceT m a #

Functor m => Functor (ResourceT m) Source # 

Methods

fmap :: (a -> b) -> ResourceT m a -> ResourceT m b #

(<$) :: a -> ResourceT m b -> ResourceT m a #

MonadFix m => MonadFix (ResourceT m) Source #

Since: 1.1.8

Methods

mfix :: (a -> ResourceT m a) -> ResourceT m a #

Applicative m => Applicative (ResourceT m) Source # 

Methods

pure :: a -> ResourceT m a #

(<*>) :: ResourceT m (a -> b) -> ResourceT m a -> ResourceT m b #

(*>) :: ResourceT m a -> ResourceT m b -> ResourceT m b #

(<*) :: ResourceT m a -> ResourceT m b -> ResourceT m a #

MonadIO m => MonadIO (ResourceT m) Source # 

Methods

liftIO :: IO a -> ResourceT m a #

Alternative m => Alternative (ResourceT m) Source #

Since 1.1.5

Methods

empty :: ResourceT m a #

(<|>) :: ResourceT m a -> ResourceT m a -> ResourceT m a #

some :: ResourceT m a -> ResourceT m [a] #

many :: ResourceT m a -> ResourceT m [a] #

MonadPlus m => MonadPlus (ResourceT m) Source #

Since 1.1.5

Methods

mzero :: ResourceT m a #

mplus :: ResourceT m a -> ResourceT m a -> ResourceT m a #

MonadThrow m => MonadThrow (ResourceT m) Source # 

Methods

throwM :: Exception e => e -> ResourceT m a #

MonadCatch m => MonadCatch (ResourceT m) Source # 

Methods

catch :: Exception e => ResourceT m a -> (e -> ResourceT m a) -> ResourceT m a #

MonadMask m => MonadMask (ResourceT m) Source # 

Methods

mask :: ((forall a. ResourceT m a -> ResourceT m a) -> ResourceT m b) -> ResourceT m b #

uninterruptibleMask :: ((forall a. ResourceT m a -> ResourceT m a) -> ResourceT m b) -> ResourceT m b #

MonadCont m => MonadCont (ResourceT m) Source # 

Methods

callCC :: ((a -> ResourceT m b) -> ResourceT m a) -> ResourceT m a #

(MonadThrow m, MonadBase IO m, MonadIO m, Applicative m) => MonadResource (ResourceT m) Source # 
type StT ResourceT a Source # 
type StT ResourceT a = a
type StM (ResourceT m) a Source # 
type StM (ResourceT m) a = StM m a

type ResIO a = ResourceT IO a Source #

Convenient alias for ResourceT IO.

data ReleaseKey Source #

A lookup key for a specific release action. This value is returned by register and allocate, and is passed to release.

Since 0.3.0

Unwrap

runResourceT :: MonadBaseControl IO m => ResourceT m a -> m a Source #

Unwrap a ResourceT transformer, and call all registered release actions.

Note that there is some reference counting involved due to resourceForkIO. If multiple threads are sharing the same collection of resources, only the last call to runResourceT will deallocate the resources.

Since 0.3.0

Special actions

resourceForkIO :: MonadBaseControl IO m => ResourceT m () -> ResourceT m ThreadId Source #

Introduce a reference-counting scheme to allow a resource context to be shared by multiple threads. Once the last thread exits, all remaining resources will be released.

Note that abuse of this function will greatly delay the deallocation of registered resources. This function should be used with care. A general guideline:

If you are allocating a resource that should be shared by multiple threads, and will be held for a long time, you should allocate it at the beginning of a new ResourceT block and then call resourceForkIO from there.

Since 0.3.0

Monad transformation

transResourceT :: (m a -> n b) -> ResourceT m a -> ResourceT n b Source #

Transform the monad a ResourceT lives in. This is most often used to strip or add new transformers to a stack, e.g. to run a ReaderT.

Note that this function is a slight generalization of hoist.

Since 0.3.0

joinResourceT :: ResourceT (ResourceT m) a -> ResourceT m a Source #

This function mirrors join at the transformer level: it will collapse two levels of ResourceT into a single ResourceT.

Since 0.4.6

Registering/releasing

allocate Source #

Arguments

:: MonadResource m 
=> IO a

allocate

-> (a -> IO ())

free resource

-> m (ReleaseKey, a) 

Perform some allocation, and automatically register a cleanup action.

This is almost identical to calling the allocation and then registering the release action, but this properly handles masking of asynchronous exceptions.

Since 0.3.0

register :: MonadResource m => IO () -> m ReleaseKey Source #

Register some action that will be called precisely once, either when runResourceT is called, or when the ReleaseKey is passed to release.

Since 0.3.0

release :: MonadIO m => ReleaseKey -> m () Source #

Call a release action early, and deregister it from the list of cleanup actions to be performed.

Since 0.3.0

unprotect :: MonadIO m => ReleaseKey -> m (Maybe (IO ())) Source #

Unprotect resource from cleanup actions, this allowes you to send resource into another resourcet process and reregister it there. It returns an release action that should be run in order to clean resource or Nothing in case if resource is already freed.

Since 0.4.5

resourceMask :: MonadResource m => ((forall a. ResourceT IO a -> ResourceT IO a) -> ResourceT IO b) -> m b Source #

Perform asynchronous exception masking.

This is more general then Control.Exception.mask, yet more efficient than Control.Exception.Lifted.mask.

Since 0.3.0

Type class/associated types

class (MonadThrow m, MonadIO m, Applicative m, MonadBase IO m) => MonadResource m where Source #

A Monad which allows for safe resource allocation. In theory, any monad transformer stack which includes a ResourceT can be an instance of MonadResource.

Note: runResourceT has a requirement for a MonadBaseControl IO m monad, which allows control operations to be lifted. A MonadResource does not have this requirement. This means that transformers such as ContT can be an instance of MonadResource. However, the ContT wrapper will need to be unwrapped before calling runResourceT.

Since 0.3.0

Minimal complete definition

liftResourceT

Methods

liftResourceT :: ResourceT IO a -> m a Source #

Lift a ResourceT IO action into the current Monad.

Since 0.4.0

Instances

MonadResource m => MonadResource (ListT m) Source # 

Methods

liftResourceT :: ResourceT IO a -> ListT m a Source #

MonadResource m => MonadResource (MaybeT m) Source # 
(MonadThrow m, MonadBase IO m, MonadIO m, Applicative m) => MonadResource (ResourceT m) Source # 
MonadResource m => MonadResource (ExceptT e m) Source # 

Methods

liftResourceT :: ResourceT IO a -> ExceptT e m a Source #

(Error e, MonadResource m) => MonadResource (ErrorT e m) Source # 

Methods

liftResourceT :: ResourceT IO a -> ErrorT e m a Source #

MonadResource m => MonadResource (StateT s m) Source # 

Methods

liftResourceT :: ResourceT IO a -> StateT s m a Source #

MonadResource m => MonadResource (StateT s m) Source # 

Methods

liftResourceT :: ResourceT IO a -> StateT s m a Source #

(Monoid w, MonadResource m) => MonadResource (WriterT w m) Source # 

Methods

liftResourceT :: ResourceT IO a -> WriterT w m a Source #

(Monoid w, MonadResource m) => MonadResource (WriterT w m) Source # 

Methods

liftResourceT :: ResourceT IO a -> WriterT w m a Source #

MonadResource m => MonadResource (IdentityT * m) Source # 
MonadResource m => MonadResource (ContT * r m) Source # 

Methods

liftResourceT :: ResourceT IO a -> ContT * r m a Source #

MonadResource m => MonadResource (ReaderT * r m) Source # 

Methods

liftResourceT :: ResourceT IO a -> ReaderT * r m a Source #

(Monoid w, MonadResource m) => MonadResource (RWST r w s m) Source # 

Methods

liftResourceT :: ResourceT IO a -> RWST r w s m a Source #

(Monoid w, MonadResource m) => MonadResource (RWST r w s m) Source # 

Methods

liftResourceT :: ResourceT IO a -> RWST r w s m a Source #

type MonadResourceBase m = (MonadBaseControl IO m, MonadThrow m, MonadBase IO m, MonadIO m, Applicative m) Source #

A Monad which can be used as a base for a ResourceT.

A ResourceT has some restrictions on its base monad:

  • runResourceT requires an instance of MonadBaseControl IO.
  • MonadResource requires an instance of MonadThrow, MonadIO, and Applicative.

While any instance of MonadBaseControl IO should be an instance of the other classes, this is not guaranteed by the type system (e.g., you may have a transformer in your stack with does not implement MonadThrow). Ideally, we would like to simply create an alias for the five type classes listed, but this is not possible with GHC currently.

Instead, this typeclass acts as a proxy for the other five. Its only purpose is to make your type signatures shorter.

Note that earlier versions of conduit had a typeclass ResourceIO. This fulfills much the same role.

Since 0.3.2

Low-level

data InvalidAccess Source #

Indicates either an error in the library, or misuse of it (e.g., a ResourceT's state is accessed after being released).

Since 0.3.0

Constructors

InvalidAccess 

Fields

Re-exports

class MonadBase b m => MonadBaseControl b m | m -> b #

Minimal complete definition

liftBaseWith, restoreM

Instances

MonadBaseControl [] [] 

Associated Types

type StM ([] :: * -> *) a :: * #

Methods

liftBaseWith :: (RunInBase [] [] -> [a]) -> [a] #

restoreM :: StM [] a -> [a] #

MonadBaseControl Maybe Maybe 

Associated Types

type StM (Maybe :: * -> *) a :: * #

MonadBaseControl IO IO 

Associated Types

type StM (IO :: * -> *) a :: * #

Methods

liftBaseWith :: (RunInBase IO IO -> IO a) -> IO a #

restoreM :: StM IO a -> IO a #

MonadBaseControl Identity Identity 

Associated Types

type StM (Identity :: * -> *) a :: * #

MonadBaseControl STM STM 

Associated Types

type StM (STM :: * -> *) a :: * #

Methods

liftBaseWith :: (RunInBase STM STM -> STM a) -> STM a #

restoreM :: StM STM a -> STM a #

MonadBaseControl b m => MonadBaseControl b (MaybeT m) 

Associated Types

type StM (MaybeT m :: * -> *) a :: * #

Methods

liftBaseWith :: (RunInBase (MaybeT m) b -> b a) -> MaybeT m a #

restoreM :: StM (MaybeT m) a -> MaybeT m a #

MonadBaseControl b m => MonadBaseControl b (ListT m) 

Associated Types

type StM (ListT m :: * -> *) a :: * #

Methods

liftBaseWith :: (RunInBase (ListT m) b -> b a) -> ListT m a #

restoreM :: StM (ListT m) a -> ListT m a #

MonadBaseControl b m => MonadBaseControl b (ResourceT m) # 

Associated Types

type StM (ResourceT m :: * -> *) a :: * #

Methods

liftBaseWith :: (RunInBase (ResourceT m) b -> b a) -> ResourceT m a #

restoreM :: StM (ResourceT m) a -> ResourceT m a #

(Monoid w, MonadBaseControl b m) => MonadBaseControl b (WriterT w m) 

Associated Types

type StM (WriterT w m :: * -> *) a :: * #

Methods

liftBaseWith :: (RunInBase (WriterT w m) b -> b a) -> WriterT w m a #

restoreM :: StM (WriterT w m) a -> WriterT w m a #

(Monoid w, MonadBaseControl b m) => MonadBaseControl b (WriterT w m) 

Associated Types

type StM (WriterT w m :: * -> *) a :: * #

Methods

liftBaseWith :: (RunInBase (WriterT w m) b -> b a) -> WriterT w m a #

restoreM :: StM (WriterT w m) a -> WriterT w m a #

MonadBaseControl b m => MonadBaseControl b (StateT s m) 

Associated Types

type StM (StateT s m :: * -> *) a :: * #

Methods

liftBaseWith :: (RunInBase (StateT s m) b -> b a) -> StateT s m a #

restoreM :: StM (StateT s m) a -> StateT s m a #

MonadBaseControl b m => MonadBaseControl b (StateT s m) 

Associated Types

type StM (StateT s m :: * -> *) a :: * #

Methods

liftBaseWith :: (RunInBase (StateT s m) b -> b a) -> StateT s m a #

restoreM :: StM (StateT s m) a -> StateT s m a #

MonadBaseControl b m => MonadBaseControl b (IdentityT * m) 

Associated Types

type StM (IdentityT * m :: * -> *) a :: * #

Methods

liftBaseWith :: (RunInBase (IdentityT * m) b -> b a) -> IdentityT * m a #

restoreM :: StM (IdentityT * m) a -> IdentityT * m a #

MonadBaseControl b m => MonadBaseControl b (ExceptT e m) 

Associated Types

type StM (ExceptT e m :: * -> *) a :: * #

Methods

liftBaseWith :: (RunInBase (ExceptT e m) b -> b a) -> ExceptT e m a #

restoreM :: StM (ExceptT e m) a -> ExceptT e m a #

(Error e, MonadBaseControl b m) => MonadBaseControl b (ErrorT e m) 

Associated Types

type StM (ErrorT e m :: * -> *) a :: * #

Methods

liftBaseWith :: (RunInBase (ErrorT e m) b -> b a) -> ErrorT e m a #

restoreM :: StM (ErrorT e m) a -> ErrorT e m a #

MonadBaseControl b m => MonadBaseControl b (ReaderT * r m) 

Associated Types

type StM (ReaderT * r m :: * -> *) a :: * #

Methods

liftBaseWith :: (RunInBase (ReaderT * r m) b -> b a) -> ReaderT * r m a #

restoreM :: StM (ReaderT * r m) a -> ReaderT * r m a #

(Monoid w, MonadBaseControl b m) => MonadBaseControl b (RWST r w s m) 

Associated Types

type StM (RWST r w s m :: * -> *) a :: * #

Methods

liftBaseWith :: (RunInBase (RWST r w s m) b -> b a) -> RWST r w s m a #

restoreM :: StM (RWST r w s m) a -> RWST r w s m a #

(Monoid w, MonadBaseControl b m) => MonadBaseControl b (RWST r w s m) 

Associated Types

type StM (RWST r w s m :: * -> *) a :: * #

Methods

liftBaseWith :: (RunInBase (RWST r w s m) b -> b a) -> RWST r w s m a #

restoreM :: StM (RWST r w s m) a -> RWST r w s m a #

MonadBaseControl ((->) r) ((->) r) 

Associated Types

type StM ((->) r :: * -> *) a :: * #

Methods

liftBaseWith :: (RunInBase ((->) r) ((->) r) -> r -> a) -> r -> a #

restoreM :: StM ((->) r) a -> r -> a #

MonadBaseControl (Either e) (Either e) 

Associated Types

type StM (Either e :: * -> *) a :: * #

Methods

liftBaseWith :: (RunInBase (Either e) (Either e) -> Either e a) -> Either e a #

restoreM :: StM (Either e) a -> Either e a #

MonadBaseControl (ST s) (ST s) 

Associated Types

type StM (ST s :: * -> *) a :: * #

Methods

liftBaseWith :: (RunInBase (ST s) (ST s) -> ST s a) -> ST s a #

restoreM :: StM (ST s) a -> ST s a #

MonadBaseControl (ST s) (ST s) 

Associated Types

type StM (ST s :: * -> *) a :: * #

Methods

liftBaseWith :: (RunInBase (ST s) (ST s) -> ST s a) -> ST s a #

restoreM :: StM (ST s) a -> ST s a #

Internal state

A ResourceT internally is a modified ReaderT monad transformer holding onto a mutable reference to all of the release actions still remaining to be performed. If you are building up a custom application monad, it may be more efficient to embed this ReaderT functionality directly in your own monad instead of wrapping around ResourceT itself. This section provides you the means of doing so.

type InternalState = IORef ReleaseMap Source #

The internal state held by a ResourceT transformer.

Since 0.4.6

getInternalState :: Monad m => ResourceT m InternalState Source #

Get the internal state of the current ResourceT.

Since 0.4.6

runInternalState :: ResourceT m a -> InternalState -> m a Source #

Unwrap a ResourceT using the given InternalState.

Since 0.4.6

withInternalState :: (InternalState -> m a) -> ResourceT m a Source #

Run an action in the underlying monad, providing it the InternalState.

Since 0.4.6

createInternalState :: MonadBase IO m => m InternalState Source #

Create a new internal state. This state must be closed with closeInternalState. It is your responsibility to ensure exception safety. Caveat emptor!

Since 0.4.9

closeInternalState :: MonadBase IO m => InternalState -> m () Source #

Close an internal state created by createInternalState.

Since 0.4.9

Backwards compatibility

type ExceptionT = CatchT Source #

For backwards compatibility.

runExceptionT :: ExceptionT m a -> m (Either SomeException a) Source #

For backwards compatibility.

runExceptionT_ :: Monad m => ExceptionT m a -> m a Source #

Same as runExceptionT, but immediately throw any exception returned.

Since 0.3.0

runException :: ExceptionT Identity a -> Either SomeException a Source #

Run an ExceptionT Identity stack.

Since 0.4.2

runException_ :: ExceptionT Identity a -> a Source #

Run an ExceptionT Identity stack, but immediately throw any exception returned.

Since 0.4.2

class Monad m => MonadThrow m where #

A class for monads in which exceptions may be thrown.

Instances should obey the following law:

throwM e >> x = throwM e

In other words, throwing an exception short-circuits the rest of the monadic computation.

Instances

MonadThrow [] 

Methods

throwM :: Exception e => e -> [a] #

MonadThrow Maybe 

Methods

throwM :: Exception e => e -> Maybe a #

MonadThrow IO 

Methods

throwM :: Exception e => e -> IO a #

MonadThrow Q 

Methods

throwM :: Exception e => e -> Q a #

MonadThrow STM 

Methods

throwM :: Exception e => e -> STM a #

(~) * e SomeException => MonadThrow (Either e) 

Methods

throwM :: Exception e => e -> Either e a #

Monad m => MonadThrow (CatchT m) 

Methods

throwM :: Exception e => e -> CatchT m a #

MonadThrow m => MonadThrow (ListT m) 

Methods

throwM :: Exception e => e -> ListT m a #

MonadThrow m => MonadThrow (MaybeT m)

Throws exceptions into the base monad.

Methods

throwM :: Exception e => e -> MaybeT m a #

MonadThrow m => MonadThrow (ResourceT m) # 

Methods

throwM :: Exception e => e -> ResourceT m a #

MonadThrow m => MonadThrow (ExceptT e m)

Throws exceptions into the base monad.

Methods

throwM :: Exception e => e -> ExceptT e m a #

(Error e, MonadThrow m) => MonadThrow (ErrorT e m)

Throws exceptions into the base monad.

Methods

throwM :: Exception e => e -> ErrorT e m a #

MonadThrow m => MonadThrow (StateT s m) 

Methods

throwM :: Exception e => e -> StateT s m a #

MonadThrow m => MonadThrow (StateT s m) 

Methods

throwM :: Exception e => e -> StateT s m a #

(MonadThrow m, Monoid w) => MonadThrow (WriterT w m) 

Methods

throwM :: Exception e => e -> WriterT w m a #

(MonadThrow m, Monoid w) => MonadThrow (WriterT w m) 

Methods

throwM :: Exception e => e -> WriterT w m a #

MonadThrow m => MonadThrow (IdentityT * m) 

Methods

throwM :: Exception e => e -> IdentityT * m a #

MonadThrow m => MonadThrow (ContT * r m) 

Methods

throwM :: Exception e => e -> ContT * r m a #

MonadThrow m => MonadThrow (ReaderT * r m) 

Methods

throwM :: Exception e => e -> ReaderT * r m a #

(MonadThrow m, Monoid w) => MonadThrow (RWST r w s m) 

Methods

throwM :: Exception e => e -> RWST r w s m a #

(MonadThrow m, Monoid w) => MonadThrow (RWST r w s m) 

Methods

throwM :: Exception e => e -> RWST r w s m a #

monadThrow :: (Exception e, MonadThrow m) => e -> m a Source #

Backwards compatibility