module Utils where

import Control.Concurrent
import Control.Exception

forkWait :: IO a -> IO (IO a)
forkWait :: forall a. IO a -> IO (IO a)
forkWait IO a
a = do
  res <- IO (MVar (Either SomeException a))
forall a. IO (MVar a)
newEmptyMVar
  _ <- mask $ \forall a. IO a -> IO a
restore -> IO () -> IO ThreadId
forkIO (IO () -> IO ThreadId) -> IO () -> IO ThreadId
forall a b. (a -> b) -> a -> b
$ IO a -> IO (Either SomeException a)
forall e a. Exception e => IO a -> IO (Either e a)
try (IO a -> IO a
forall a. IO a -> IO a
restore IO a
a) IO (Either SomeException a)
-> (Either SomeException a -> IO ()) -> IO ()
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= MVar (Either SomeException a) -> Either SomeException a -> IO ()
forall a. MVar a -> a -> IO ()
putMVar MVar (Either SomeException a)
res
  return (takeMVar res >>= either (\SomeException
ex -> SomeException -> IO a
forall e a. (HasCallStack, Exception e) => e -> IO a
throwIO (SomeException
ex :: SomeException)) return)