Created
December 18, 2019 02:14
-
-
Save chris-martin/b26cc9cf87bcc2a3ffe00819beb78184 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Control.Exception.Safe | |
import Control.Concurrent | |
bracketFork | |
:: IO resource | |
-- ^ The first action; will run in the current thread | |
-> (resource -> IO a) | |
-- ^ A final action that runs in the forked thread, with async exceptions masked | |
-> (resource -> IO b) | |
-- ^ Action that runs in the forked thread, with async exceptions unmasked | |
-> IO ThreadId | |
bracketFork open close action = | |
mask_ $ | |
do | |
resource <- open | |
action resource `forkUnmaskedFinally` close resource | |
forkUnmaskedFinally | |
:: IO a | |
-- ^ Action that runs in the forked thread, with async exceptions unmasked | |
-> IO b | |
-- ^ A final action that runs in the forked thread, with async exceptions masked | |
-> IO ThreadId | |
forkUnmaskedFinally action close = | |
mask_ $ forkIOWithUnmask $ \unmask -> | |
do | |
unmask action `onException` close | |
close | |
return () |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment