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.

Unwrap

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.

Special actions

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.

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.

Type class/associated types

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.

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.

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.