88{-# LANGUAGE RecordWildCards #-}
99{-# LANGUAGE TypeFamilies #-}
1010
11- module Development.IDE.Graph.Internal.Database (compute , newDatabase , incDatabase , build , getDirtySet , getKeysAndVisitAge , AsyncParentKill ( .. ) ) where
11+ module Development.IDE.Graph.Internal.Database (compute , newDatabase , incDatabase , build , getDirtySet , getKeysAndVisitAge ) where
1212
1313import Prelude hiding (unzip )
1414
8888 => Database -> Stack -> f key -> IO (f Key , f value )
8989-- build _ st k | traceShow ("build", st, k) False = undefined
9090build db stack keys = do
91- step <- readTVarIO $ databaseStep db
92- ! built <- runAIO step $ builder db stack (fmap newKey keys)
91+ ! built <- runAIO $ builder db stack (fmap newKey keys)
9392 let (ids, vs) = unzip built
9493 pure (ids, fmap (asV . resultValue) vs)
9594 where
@@ -118,13 +117,13 @@ builderOne ba db@Database {..} stack id = UE.mask $ \restore -> do
118117 status <- SMap. lookup id databaseValues
119118 val <-
120119 let refreshRsult s = do
121- let act =
122- case ba of
123- BuildNary -> restore $ asyncWithCleanUp $
124- refresh db stack id s
125- `UE.onException` (UE. uninterruptibleMask_ $ liftIO (atomicallyNamed " builder - onException" (SMap. focus updateDirty id databaseValues)))
126- BuildUnary -> fmap return $ refresh db stack id s
127- -- Mark the key as running
120+ let act = restore $ case ba of
121+ BuildNary ->
122+ asyncWithCleanUp $
123+ refresh db stack id s
124+ `UE.onException` (UE. uninterruptibleMask_ $ liftIO (atomicallyNamed " builder - onException" (SMap. focus updateDirty id databaseValues)))
125+ BuildUnary -> fmap return $ refresh db stack id s
126+ -- Mark the key as running
128127 SMap. focus (updateStatus $ Running current s) id databaseValues
129128 return act
130129 in case viewDirty current $ maybe (Dirty Nothing ) keyStatus status of
@@ -286,23 +285,15 @@ transitiveDirtySet database = flip State.execStateT mempty . traverse_ loop
286285newtype AIO a = AIO { unAIO :: ReaderT (IORef [Async () ]) IO a }
287286 deriving newtype (Applicative , Functor , Monad , MonadIO )
288287
289- data AsyncParentKill = AsyncParentKill ThreadId Step
290- deriving (Show , Eq )
291-
292- instance Exception AsyncParentKill where
293- toException = asyncExceptionToException
294- fromException = asyncExceptionFromException
295-
296288-- | Run the monadic computation, cancelling all the spawned asyncs if an exception arises
297- runAIO :: Step -> AIO a -> IO a
298- runAIO s (AIO act) = do
289+ runAIO :: AIO a -> IO a
290+ runAIO (AIO act) = do
299291 asyncsRef <- newIORef []
300292 -- Log the exact exception (including async exceptions) before cleanup,
301293 -- then rethrow to preserve previous semantics.
302294 runReaderT act asyncsRef `onException` do
303295 asyncs <- atomicModifyIORef' asyncsRef ([] ,)
304- tid <- myThreadId
305- cleanupAsync asyncs tid s
296+ cleanupAsync asyncs
306297
307298-- | Like 'async' but with built-in cancellation.
308299-- Returns an IO action to wait on the result.
@@ -326,12 +317,11 @@ instance MonadUnliftIO AIO where
326317 st <- AIO ask
327318 liftIO $ k (\ aio -> runReaderT (unAIO aio) st)
328319
329- cleanupAsync :: [Async a ] -> ThreadId -> Step -> IO ()
320+ cleanupAsync :: [Async a ] -> IO ()
330321-- mask to make sure we interrupt all the asyncs
331- cleanupAsync asyncs tid step = uninterruptibleMask $ \ unmask -> do
322+ cleanupAsync asyncs = uninterruptibleMask $ \ unmask -> do
332323 -- interrupt all the asyncs without waiting
333- -- mapM_ (\a -> throwTo (asyncThreadId a) AsyncCancelled) asyncs
334- mapM_ (\ a -> throwTo (asyncThreadId a) $ AsyncParentKill tid step) asyncs
324+ mapM_ (\ a -> throwTo (asyncThreadId a) AsyncCancelled ) asyncs
335325 -- Wait until all the asyncs are done
336326 -- But if it takes more than 10 seconds, log to stderr
337327 unless (null asyncs) $ do
0 commit comments