Skip to content

Instantly share code, notes, and snippets.

@qnikst
Created January 22, 2016 16:51
Show Gist options
  • Save qnikst/b8ae1bae66e9659a0d4c to your computer and use it in GitHub Desktop.
Save qnikst/b8ae1bae66e9659a0d4c to your computer and use it in GitHub Desktop.
data ExceptionInfo = ExceptionIf ThreadId SomeException -- constructor should be hidden
throwToEnclosed :: Exception e => ThreadId -> e -> IO ()
throwToEnclosed to ex =
throwTo =<< ExceptionInfo <$> myThreadId <*> pure (SomeException ex)
enclosed :: [ThreadId] -> IO a -> IO a
enclosed allowed f = mask $ \release -> do
result <- newEmptyMVar
internal <- forkIO $ release $ try f >>= result -- HA HA HA I'm hidden
er <- fix $ \loop -> takeMVar result
handles [ Handle $ (ExceptionInfo tid ex) -> do
if tid `elem` allowed
then throwTo internal ex >> loop
else putStrLn "MUAHAHAHAHAHAHHA" >> loop
, SomeException e -> putStrLn "I'm protected by GODS!" >> loop
]
either throw return er
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment