-
-
Save qnikst/b8ae1bae66e9659a0d4c to your computer and use it in GitHub Desktop.
This file contains 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
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