Skip to content

Instantly share code, notes, and snippets.

@chris-martin
Created December 18, 2019 02:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chris-martin/b26cc9cf87bcc2a3ffe00819beb78184 to your computer and use it in GitHub Desktop.
Save chris-martin/b26cc9cf87bcc2a3ffe00819beb78184 to your computer and use it in GitHub Desktop.
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