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

Safe HaskellNone

Control.Monad.Trans.Resource

Contents

Description

Allocate resources which are guaranteed to be released.

For more information, see http://www.yesodweb.com/book/conduits.

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

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

MonadTrans ResourceT 
MonadTransControl ResourceT 
MMonad ResourceT

Since 0.4.7

MFunctor ResourceT

Since 0.4.7

MonadRWS r w s m => MonadRWS r w s (ResourceT m) 
MonadBase b m => MonadBase b (ResourceT m) 
MonadBaseControl b m => MonadBaseControl b (ResourceT m) 
MonadError e m => MonadError e (ResourceT m) 
MonadWriter w m => MonadWriter w (ResourceT m) 
MonadState s m => MonadState s (ResourceT m) 
MonadReader r m => MonadReader r (ResourceT m) 
Monad m => Monad (ResourceT m) 
Functor m => Functor (ResourceT m) 
Typeable1 m => Typeable1 (ResourceT m) 
Applicative m => Applicative (ResourceT m) 
MonadIO m => MonadIO (ResourceT m) 
MonadCont m => MonadCont (ResourceT m) 
MonadThrow m => MonadThrow (ResourceT m) 
(MonadThrow m, MonadUnsafeIO m, MonadIO m, Applicative m) => MonadResource (ResourceT m) 
(MonadIO m, MonadActive m) => MonadActive (ResourceT m) 

type ResIO a = ResourceT IO a

Convenient alias for ResourceT IO.

data ReleaseKey

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

Instances

Typeable ReleaseKey 

Unwrap

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

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

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

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

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

Since 0.4.6

A specific Exception transformer

newtype ExceptionT m a

The express purpose of this transformer is to allow non-IO-based monad stacks to catch exceptions via the MonadThrow typeclass.

Since 0.3.0

Constructors

ExceptionT 

Fields

runExceptionT :: m (Either SomeException a)
 

Instances

MonadTrans ExceptionT 
MonadTransControl ExceptionT 
MonadRWS r w s m => MonadRWS r w s (ExceptionT m) 
MonadBase b m => MonadBase b (ExceptionT m) 
MonadBaseControl b m => MonadBaseControl b (ExceptionT m) 
MonadError e m => MonadError e (ExceptionT m) 
MonadWriter w m => MonadWriter w (ExceptionT m) 
MonadState s m => MonadState s (ExceptionT m) 
MonadReader r m => MonadReader r (ExceptionT m) 
Monad m => Monad (ExceptionT m) 
Monad m => Functor (ExceptionT m) 
Monad m => Applicative (ExceptionT m) 
MonadIO m => MonadIO (ExceptionT m) 
MonadCont m => MonadCont (ExceptionT m) 
Monad m => MonadThrow (ExceptionT m) 
MonadResource m => MonadResource (ExceptionT m) 

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

Same as runExceptionT, but immediately throw any exception returned.

Since 0.3.0

runException :: ExceptionT Identity a -> Either SomeException a

Run an ExceptionT Identity stack.

Since 0.4.2

runException_ :: ExceptionT Identity a -> a

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

Since 0.4.2

Registering/releasing

allocate

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

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 ()

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 ()))

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

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, MonadUnsafeIO m, MonadIO m, Applicative m) => MonadResource m where

A Monad which allows for safe resource allocation. In theory, any monad transformer stack included 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

Methods

liftResourceT :: ResourceT IO a -> m a

Lift a ResourceT IO action into the current Monad.

Since 0.4.0

Instances

MonadResource m => MonadResource (MaybeT m) 
MonadResource m => MonadResource (ListT m) 
MonadResource m => MonadResource (IdentityT m) 
MonadResource m => MonadResource (ExceptionT m) 
(MonadThrow m, MonadUnsafeIO m, MonadIO m, Applicative m) => MonadResource (ResourceT m) 
(Monoid w, MonadResource m) => MonadResource (WriterT w m) 
(Monoid w, MonadResource m) => MonadResource (WriterT w m) 
MonadResource m => MonadResource (StateT s m) 
MonadResource m => MonadResource (StateT s m) 
MonadResource m => MonadResource (ReaderT r m) 
(Error e, MonadResource m) => MonadResource (ErrorT e m) 
MonadResource m => MonadResource (ContT r m) 
(Monoid w, MonadResource m) => MonadResource (RWST r w s m) 
(Monoid w, MonadResource m) => MonadResource (RWST r w s m) 

class Monad m => MonadUnsafeIO m where

A Monad based on some monad which allows running of some IO actions, via unsafe calls. This applies to IO and ST, for instance.

Since 0.3.0

Methods

unsafeLiftIO :: IO a -> m a

Instances

MonadUnsafeIO IO 
(MonadTrans t, MonadUnsafeIO m, Monad (t m)) => MonadUnsafeIO (t m) 
MonadUnsafeIO (ST s) 
MonadUnsafeIO (ST s) 

class Monad m => MonadThrow m where

A Monad which can throw exceptions. Note that this does not work in a vanilla ST or Identity monad. Instead, you should use the ExceptionT transformer in your stack if you are dealing with a non-IO base monad.

Since 0.3.0

Methods

monadThrow :: Exception e => e -> m a

Instances

MonadThrow [] 
MonadThrow IO 
MonadThrow Maybe 
MonadThrow (Either SomeException) 
MonadThrow m => MonadThrow (MaybeT m) 
MonadThrow m => MonadThrow (ListT m) 
MonadThrow m => MonadThrow (IdentityT m) 
Monad m => MonadThrow (ExceptionT m) 
MonadThrow m => MonadThrow (ResourceT m) 
(Monoid w, MonadThrow m) => MonadThrow (WriterT w m) 
(Monoid w, MonadThrow m) => MonadThrow (WriterT w m) 
MonadThrow m => MonadThrow (StateT s m) 
MonadThrow m => MonadThrow (StateT s m) 
MonadThrow m => MonadThrow (ReaderT r m) 
(Error e, MonadThrow m) => MonadThrow (ErrorT e m) 
MonadThrow m => MonadThrow (ContT r m) 
(Monoid w, MonadThrow m) => MonadThrow (RWST r w s m) 
(Monoid w, MonadThrow m) => MonadThrow (RWST r w s m) 

class Monad m => MonadActive m where

Determine if some monad is still active. This is intended to prevent usage of a monadic state after it has been closed. This is necessary for such cases as lazy I/O, where an unevaluated thunk may still refer to a closed ResourceT.

Since 0.3.0

Methods

monadActive :: m Bool

Instances

MonadActive IO 
MonadActive Identity 
MonadActive (ST s) 
MonadActive (ST s) 
MonadActive m => MonadActive (MaybeT m) 
MonadActive m => MonadActive (ListT m) 
MonadActive m => MonadActive (IdentityT m) 
(MonadIO m, MonadActive m) => MonadActive (ResourceT m) 
(Monoid w, MonadActive m) => MonadActive (WriterT w m) 
(Monoid w, MonadActive m) => MonadActive (WriterT w m) 
MonadActive m => MonadActive (StateT s m) 
MonadActive m => MonadActive (StateT s m) 
MonadActive m => MonadActive (ReaderT r m) 
(Error e, MonadActive m) => MonadActive (ErrorT e m) 
(Monoid w, MonadActive m) => MonadActive (RWST r w s m) 
(Monoid w, MonadActive m) => MonadActive (RWST r w s m) 

type MonadResourceBase m = (MonadBaseControl IO m, MonadThrow m, MonadUnsafeIO m, MonadIO m, Applicative m)

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, MonadUnsafeIO, 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

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

functionName :: String
 

Instances

Show InvalidAccess 
Typeable InvalidAccess 
Exception InvalidAccess 

Re-exports

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

Instances

MonadBaseControl [] [] 
MonadBaseControl IO IO 
MonadBaseControl Maybe Maybe 
MonadBaseControl STM STM 
MonadBaseControl Identity Identity 
MonadBaseControl b m => MonadBaseControl b (ExceptionT m) 
MonadBaseControl b m => MonadBaseControl b (ResourceT m) 
MonadBaseControl b m => MonadBaseControl b (MaybeT m) 
MonadBaseControl b m => MonadBaseControl b (ListT m) 
MonadBaseControl b m => MonadBaseControl b (IdentityT m) 
(Monoid w, MonadBaseControl b m) => MonadBaseControl b (WriterT w m) 
(Monoid w, MonadBaseControl b m) => MonadBaseControl b (WriterT w m) 
MonadBaseControl b m => MonadBaseControl b (StateT s m) 
MonadBaseControl b m => MonadBaseControl b (StateT s m) 
MonadBaseControl b m => MonadBaseControl b (ReaderT r m) 
(Error e, MonadBaseControl b m) => MonadBaseControl b (ErrorT e m) 
(Monoid w, MonadBaseControl b m) => MonadBaseControl b (RWST r w s m) 
(Monoid w, MonadBaseControl b m) => MonadBaseControl b (RWST r w s m) 
MonadBaseControl ((->) r) ((->) r) 
MonadBaseControl (Either e) (Either e) 
MonadBaseControl (ST s) (ST s) 
MonadBaseControl (ST s) (ST s) 

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

The internal state held by a ResourceT transformer.

Since 0.4.6

getInternalState :: Monad m => ResourceT m InternalState

Get the internal state of the current ResourceT.

Since 0.4.6

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

Unwrap a ResourceT using the given InternalState.

Since 0.4.6

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

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

Since 0.4.6

createInternalState :: MonadBase IO m => m InternalState

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 ()

Close an internal state created by createInternalState.

Since 0.4.9

Resource

data Resource a

A method for allocating a scarce resource, providing the means of freeing it when no longer needed. This data type provides Functor@Applicative@Monad instances for composing different resources together. You can allocate these resources using either the bracket pattern (via with) or using ResourceT (via allocateResource).

This concept was originally introduced by Gabriel Gonzalez and described at: http://www.haskellforall.com/2013/06/the-resource-applicative.html. The implementation in this package is slightly different, due to taking a different approach to async exception safety.

Since 0.4.10

Instances

Monad Resource 
Functor Resource 
Typeable1 Resource 
Applicative Resource 
MonadIO Resource 
MonadBase IO Resource 

mkResource

Arguments

:: IO a

allocate the resource

-> (a -> IO ())

free the resource

-> Resource a 

Create a Resource value using the given allocate and free functions.

Since 0.4.10

with :: MonadBaseControl IO m => Resource a -> (a -> m b) -> m b

Allocate the given resource and provide it to the provided function. The resource will be freed as soon as the inner block is exited, whether normally or via an exception. This function is similar in function to bracket.

Since 0.4.10

allocateResource :: MonadResource m => Resource a -> m (ReleaseKey, a)

Allocate a resource and register an action with the MonadResource to free the resource.

Since 0.4.10