Safe Haskell  None 

Language  Haskell98 
Allocate resources which are guaranteed to be released.
For more information, see https://www.fpcomplete.com/user/snoyberg/librarydocumentation/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.
 data ResourceT m a
 type ResIO = ResourceT IO
 data ReleaseKey
 runResourceT :: MonadUnliftIO m => ResourceT m a > m a
 runResourceTChecked :: MonadUnliftIO m => ResourceT m a > m a
 data ResourceCleanupException = ResourceCleanupException {}
 resourceForkWith :: MonadUnliftIO m => (IO () > IO a) > ResourceT m () > ResourceT m a
 resourceForkIO :: MonadUnliftIO m => ResourceT m () > ResourceT m ThreadId
 transResourceT :: (m a > n b) > ResourceT m a > ResourceT n b
 joinResourceT :: ResourceT (ResourceT m) a > ResourceT m a
 allocate :: MonadResource m => IO a > (a > IO ()) > m (ReleaseKey, a)
 register :: MonadResource m => IO () > m ReleaseKey
 release :: MonadIO m => ReleaseKey > m ()
 unprotect :: MonadIO m => ReleaseKey > m (Maybe (IO ()))
 resourceMask :: MonadResource m => ((forall a. ResourceT IO a > ResourceT IO a) > ResourceT IO b) > m b
 class MonadIO m => MonadResource m where
 type MonadResourceBase = MonadUnliftIO
 data InvalidAccess = InvalidAccess {}
 class MonadIO m => MonadUnliftIO (m :: * > *)
 type InternalState = IORef ReleaseMap
 getInternalState :: Monad m => ResourceT m InternalState
 runInternalState :: ResourceT m a > InternalState > m a
 withInternalState :: (InternalState > m a) > ResourceT m a
 createInternalState :: MonadIO m => m InternalState
 closeInternalState :: MonadIO m => InternalState > m ()
 class Monad m => MonadThrow (m :: * > *) where
Data types
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
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 :: MonadUnliftIO 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.
NOTE Since version 1.2.0, this function will throw a
ResourceCleanupException
if any of the cleanup functions throw an
exception.
Since: 0.3.0
Check cleanup exceptions
runResourceTChecked :: MonadUnliftIO m => ResourceT m a > m a Source #
Backwards compatible alias for runResourceT
.
Since: 1.1.11
data ResourceCleanupException Source #
Thrown when one or more cleanup functions themselves throw an exception during cleanup.
Since: 1.1.11
ResourceCleanupException  

Special actions
resourceForkWith :: MonadUnliftIO m => (IO () > IO a) > ResourceT m () > ResourceT m a Source #
Introduce a referencecounting scheme to allow a resource context to be shared by multiple threads. Once the last thread exits, all remaining resources will be released.
The first parameter is a function which will be used to create the
thread, such as forkIO
or async
.
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 resourceForkWith
from there.
Since: 1.1.9
resourceForkIO :: MonadUnliftIO m => ResourceT m () > ResourceT m ThreadId Source #
Launch a new reference counted resource context using forkIO
.
This is defined as resourceForkWith forkIO
.
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
:: 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
register
ing 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 allows 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 MonadIO 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 MonadUnliftIO 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
liftResourceT :: ResourceT IO a > m a Source #
Lift a ResourceT IO
action into the current Monad
.
Since 0.4.0
MonadResource m => MonadResource (ListT m) Source #  
MonadResource m => MonadResource (MaybeT m) Source #  
MonadIO m => MonadResource (ResourceT m) Source #  
MonadResource m => MonadResource (ExceptT e m) Source #  
MonadResource m => MonadResource (StateT s m) Source #  
MonadResource m => MonadResource (StateT s m) Source #  
(Monoid w, MonadResource m) => MonadResource (WriterT w m) Source #  
(Monoid w, MonadResource m) => MonadResource (WriterT w m) Source #  
MonadResource m => MonadResource (IdentityT * m) Source #  
MonadResource m => MonadResource (ContT * r m) Source #  
MonadResource m => MonadResource (ReaderT * r m) Source #  
(Monoid w, MonadResource m) => MonadResource (RWST r w s m) Source #  
(Monoid w, MonadResource m) => MonadResource (RWST r w s m) Source #  
type MonadResourceBase = MonadUnliftIO Source #
Deprecated: Use MonadUnliftIO directly instead
Just use MonadUnliftIO
directly now, legacy explanation continues:
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 ofMonadUnliftIO
.MonadResource
requires an instance ofMonadIO
Note that earlier versions of conduit
had a typeclass ResourceIO
. This
fulfills much the same role.
Since 0.3.2
Lowlevel
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
Reexports
class MonadIO m => MonadUnliftIO (m :: * > *) #
Monads which allow their actions to be run in IO
.
While MonadIO
allows an IO
action to be lifted into another
monad, this class captures the opposite concept: allowing you to
capture the monadic context. Note that, in order to meet the laws
given below, the intuition is that a monad must have no monadic
state, but may have monadic context. This essentially limits
MonadUnliftIO
to ReaderT
and IdentityT
transformers on top of
IO
.
Laws. For any value u
returned by askUnliftIO
, it must meet the
monad transformer laws as reformulated for MonadUnliftIO
:
unliftIO u . return = return
unliftIO u (m >>= f) = unliftIO u m >>= unliftIO u . f
The third is a currently nameless law which ensures that the current context is preserved.
askUnliftIO >>= (u > liftIO (unliftIO u m)) = m
If you have a name for this, please submit it in a pull request for great glory.
Since: 0.1.0.0
MonadUnliftIO IO  
MonadUnliftIO m => MonadUnliftIO (ResourceT m) #  Since: 1.1.10 
MonadUnliftIO m => MonadUnliftIO (IdentityT * m)  
MonadUnliftIO m => MonadUnliftIO (ReaderT * r m)  
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 :: MonadIO 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 :: MonadIO m => InternalState > m () Source #
Close an internal state created by createInternalState
.
Since 0.4.9
Reexport
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 shortcircuits the rest of the monadic computation.
throwM :: Exception e => e > m a #
Throw an exception. Note that this throws when this action is run in
the monad m
, not when it is applied. It is a generalization of
Control.Exception's throwIO
.
Should satisfy the law:
throwM e >> f = throwM e
MonadThrow []  
MonadThrow Maybe  
MonadThrow IO  
MonadThrow Q  
MonadThrow STM  
(~) * e SomeException => MonadThrow (Either e)  
MonadThrow m => MonadThrow (ListT m)  
MonadThrow m => MonadThrow (MaybeT m)  Throws exceptions into the base monad. 
MonadThrow m => MonadThrow (ResourceT m) #  
(Error e, MonadThrow m) => MonadThrow (ErrorT e m)  Throws exceptions into the base monad. 
MonadThrow m => MonadThrow (ExceptT e m)  Throws exceptions into the base monad. 
MonadThrow m => MonadThrow (StateT s m)  
MonadThrow m => MonadThrow (StateT s m)  
(MonadThrow m, Monoid w) => MonadThrow (WriterT w m)  
(MonadThrow m, Monoid w) => MonadThrow (WriterT w m)  
MonadThrow m => MonadThrow (IdentityT * m)  
MonadThrow m => MonadThrow (ContT * r m)  
MonadThrow m => MonadThrow (ReaderT * r m)  
(MonadThrow m, Monoid w) => MonadThrow (RWST r w s m)  
(MonadThrow m, Monoid w) => MonadThrow (RWST r w s m)  