| Safe Haskell | Safe |
|---|---|
| Language | Haskell2010 |
Control.IO.Region
Description
Exception safe resource management
Examples:
import Control.IO.Region (region)
import qualified Control.IO.Region as R
...
region $ \r -> do
resource <- R.alloc_ r allocate free
use resource
-- resource will be automatically freed here
...
region $ \r -> do
(resource, key) <- R.alloc r allocate free
use resource
if ...
then R.free key -- free it earler
else use resource
...
region $ \r1 -> do
resource <- region $ \r2 -> do
(resource1, key) <- R.alloc r2 allocate free
use resource
resource `R.moveTo` r1 -- transfer ownership to region r1
return resource
doSomethingElse resource
-- resource will be freed here
...
region $ \r1 -> do
(r2, r2Key) <- R.alloc r1 R.open R.close -- region is a resource too
resource <- R.alloc r2 allocate free
use resource
r2Key `R.moveTo` r3 -- move region r2 ownership (and also the resource) to other region
Synopsis
- data Region
- data Key
- data AlreadyClosed = AlreadyClosed
- data AlreadyFreed = AlreadyFreed
- region :: (Region -> IO a) -> IO a
- open :: IO Region
- close :: Region -> IO ()
- alloc :: Region -> IO a -> (a -> IO ()) -> IO (a, Key)
- alloc_ :: Region -> IO a -> (a -> IO ()) -> IO a
- free :: Key -> IO ()
- moveToSTM :: Key -> Region -> STM Key
- moveTo :: Key -> Region -> IO Key
- defer :: Region -> IO () -> IO ()
Documentation
Region owns resources and frees them on close
Each resource is identified by unique key
data AlreadyClosed #
Region already closed
Constructors
| AlreadyClosed |
Instances
| Show AlreadyClosed # | |
Defined in Control.IO.Region Methods showsPrec :: Int -> AlreadyClosed -> ShowS # show :: AlreadyClosed -> String # showList :: [AlreadyClosed] -> ShowS # | |
| Exception AlreadyClosed # | |
Defined in Control.IO.Region Methods toException :: AlreadyClosed -> SomeException # fromException :: SomeException -> Maybe AlreadyClosed # displayException :: AlreadyClosed -> String # | |
data AlreadyFreed #
Resource already freed
Constructors
| AlreadyFreed |
Instances
| Show AlreadyFreed # | |
Defined in Control.IO.Region Methods showsPrec :: Int -> AlreadyFreed -> ShowS # show :: AlreadyFreed -> String # showList :: [AlreadyFreed] -> ShowS # | |
| Exception AlreadyFreed # | |
Defined in Control.IO.Region Methods toException :: AlreadyFreed -> SomeException # fromException :: SomeException -> Maybe AlreadyFreed # displayException :: AlreadyFreed -> String # | |
Close the region. You probably should called it
when async exceptions are masked. Prefer region function.
It is error to close region twice.
In case of exception inside any cleanup handler, other handlers will be
called anyway. The last exception will be rethrown (that matches the
behavior of bracket.)
Arguments
| :: Region | |
| -> IO a | action to allocate resource |
| -> (a -> IO ()) | action to cleanup resource |
| -> IO (a, Key) | the resource and it's key |
Allocate resource inside the region
Free the resource earlier then it's region will be closed. It will be removed from the region immediately. It is error to free resource twice