Safe Haskell | None |
---|
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.
- data ResourceT m a
- type ResIO a = ResourceT IO a
- data ReleaseKey
- runResourceT :: MonadBaseControl IO m => ResourceT m a -> m a
- resourceForkIO :: MonadBaseControl IO m => ResourceT m () -> ResourceT m ThreadId
- transResourceT :: (m a -> n b) -> ResourceT m a -> ResourceT n b
- newtype ExceptionT m a = ExceptionT {
- runExceptionT :: m (Either SomeException a)
- runExceptionT_ :: Monad m => ExceptionT m a -> m a
- allocate :: MonadResource m => IO a -> (a -> IO ()) -> m (ReleaseKey, a)
- register :: MonadResource m => IO () -> m ReleaseKey
- release :: MonadResource m => ReleaseKey -> m ()
- resourceMask :: MonadResource m => ((forall a. ResourceT IO a -> ResourceT IO a) -> ResourceT IO b) -> m b
- class (MonadThrow m, MonadUnsafeIO m, MonadIO m, Applicative m) => MonadResource m where
- liftResourceT :: ResourceT IO a -> m a
- class Monad m => MonadUnsafeIO m where
- unsafeLiftIO :: IO a -> m a
- class Monad m => MonadThrow m where
- monadThrow :: Exception e => e -> m a
- class Monad m => MonadActive m where
- monadActive :: m Bool
- type MonadResourceBase m = (MonadBaseControl IO m, MonadThrow m, MonadUnsafeIO m, MonadIO m, Applicative m)
- data InvalidAccess = InvalidAccess {}
- class MonadBase b m => MonadBaseControl b m | m -> b
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
Unwrap
runResourceT :: MonadBaseControl IO m => ResourceT m a -> m aSource
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 ThreadIdSource
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 bSource
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
.
Since 0.3.0
A specific Exception transformer
newtype ExceptionT m a Source
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
ExceptionT | |
|
MonadTrans ExceptionT | |
MonadTransControl ExceptionT | |
(Applicative b, Applicative (ExceptionT m), Monad b, Monad (ExceptionT m), MonadBase b m) => MonadBase b (ExceptionT m) | |
(MonadBase b (ExceptionT m), MonadBaseControl b m) => MonadBaseControl b (ExceptionT m) | |
Monad m => Monad (ExceptionT m) | |
Monad m => Functor (ExceptionT m) | |
(Functor (ExceptionT m), Monad m) => Applicative (ExceptionT m) | |
(Monad (ExceptionT m), Monad m) => MonadThrow (ExceptionT m) |
runExceptionT_ :: Monad m => ExceptionT m a -> m aSource
Same as runExceptionT
, but immediately throw
any exception returned.
Since 0.3.0
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 ReleaseKeySource
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 :: MonadResource 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
resourceMask :: MonadResource m => ((forall a. ResourceT IO a -> ResourceT IO a) -> ResourceT IO b) -> m bSource
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 whereSource
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
liftResourceT :: ResourceT IO a -> m aSource
Lift a ResourceT IO
action into the current Monad
.
Since 0.4.0
class Monad m => MonadUnsafeIO m whereSource
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
unsafeLiftIO :: IO a -> m aSource
MonadUnsafeIO IO | |
(MonadTrans t, MonadUnsafeIO m, Monad (t m)) => MonadUnsafeIO (t m) | |
Monad (ST s) => MonadUnsafeIO (ST s) | |
Monad (ST s) => MonadUnsafeIO (ST s) |
class Monad m => MonadThrow m whereSource
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
monadThrow :: Exception e => e -> m aSource
MonadThrow IO | |
(Monad (ListT m), MonadThrow m) => MonadThrow (ListT m) | |
(Monad (MaybeT m), MonadThrow m) => MonadThrow (MaybeT m) | |
(Monad (IdentityT m), MonadThrow m) => MonadThrow (IdentityT m) | |
(Monad (ExceptionT m), Monad m) => MonadThrow (ExceptionT m) | |
(Monad (ResourceT m), MonadThrow m) => MonadThrow (ResourceT m) | |
(Monad (ContT r m), MonadThrow m) => MonadThrow (ContT r m) | |
(Monad (ErrorT e m), Error e, MonadThrow m) => MonadThrow (ErrorT e m) | |
(Monad (ReaderT r m), MonadThrow m) => MonadThrow (ReaderT r m) | |
(Monad (StateT s m), MonadThrow m) => MonadThrow (StateT s m) | |
(Monad (StateT s m), MonadThrow m) => MonadThrow (StateT s m) | |
(Monad (WriterT w m), Monoid w, MonadThrow m) => MonadThrow (WriterT w m) | |
(Monad (WriterT w m), Monoid w, MonadThrow m) => MonadThrow (WriterT w m) | |
(Monad (RWST r w s m), Monoid w, MonadThrow m) => MonadThrow (RWST r w s m) | |
(Monad (RWST r w s m), Monoid w, MonadThrow m) => MonadThrow (RWST r w s m) |
class Monad m => MonadActive m whereSource
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
monadActive :: m BoolSource
MonadActive IO | |
MonadActive Identity | |
Monad (ST s) => MonadActive (ST s) | |
Monad (ST s) => MonadActive (ST s) | |
(Monad (ListT m), MonadActive m) => MonadActive (ListT m) | |
(Monad (MaybeT m), MonadActive m) => MonadActive (MaybeT m) | |
(Monad (IdentityT m), MonadActive m) => MonadActive (IdentityT m) | |
(Monad (ResourceT m), MonadIO m, MonadActive m) => MonadActive (ResourceT m) | |
(Monad (ErrorT e m), Error e, MonadActive m) => MonadActive (ErrorT e m) | |
(Monad (ReaderT r m), MonadActive m) => MonadActive (ReaderT r m) | |
(Monad (StateT s m), MonadActive m) => MonadActive (StateT s m) | |
(Monad (StateT s m), MonadActive m) => MonadActive (StateT s m) | |
(Monad (WriterT w m), Monoid w, MonadActive m) => MonadActive (WriterT w m) | |
(Monad (WriterT w m), Monoid w, MonadActive m) => MonadActive (WriterT w m) | |
(Monad (RWST r w s m), Monoid w, MonadActive m) => MonadActive (RWST r w s m) | |
(Monad (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)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 ofMonadBaseControl IO
. *MonadResource
requires an instance ofMonadThrow
,MonadUnsafeIO
,MonadIO
, andApplicative
.
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
Re-exports
class MonadBase b m => MonadBaseControl b m | m -> b