Skip to content

Commit f6f7891

Browse files
committed
split report and state update in separate threads
1 parent 8710341 commit f6f7891

File tree

1 file changed

+25
-24
lines changed

1 file changed

+25
-24
lines changed

ghcide/src/Development/IDE/Core/Compile.hs

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -924,18 +924,17 @@ indexHieFile se mod_summary srcPath !hash hf = do
924924
-- If the hash in the pending list doesn't match the current hash, then skip
925925
Just pendingHash -> pendingHash /= hash
926926
unless newerScheduled $ do
927-
-- Using `bracket`, so even if an exception happen during withHieDb call,
928-
-- the the ending part of `reportProgress` (which clean the progress indicator) will still be called.
929-
indexDoneB <- liftIO newBarrier
930-
bracket_ (liftIO $ atomically $ writeTQueue indexProgressThread (reportProgress indexDoneB optProgressStyle)) (signalBarrier indexDoneB ()) $
927+
-- Using bracket, so even if an exception happen during withHieDb call,
928+
-- the `post` (which clean the progress indicator) will still be called.
929+
bracket_ (pre optProgressStyle) post $
931930
withHieDb (\db -> HieDb.addRefsFromLoaded db targetPath (HieDb.RealFile $ fromNormalizedFilePath srcPath) hash hf')
932931
where
933932
mod_location = ms_location mod_summary
934933
targetPath = Compat.ml_hie_file mod_location
935934
HieDbWriter{..} = hiedbWriter se
936935

937936
-- Get a progress token to report progress and update it for the current file
938-
reportProgress indexDoneB style = do
937+
pre style = atomically $ writeTQueue indexProgressThread $ do
939938
tok <- modifyVar indexProgressToken $ fmap dupe . \case
940939
x@(Just _) -> pure x
941940
-- Create a token if we don't already have one
@@ -990,9 +989,8 @@ indexHieFile se mod_summary srcPath !hash hf = do
990989
, _message = Nothing
991990
, _percentage = Nothing
992991
}
993-
-- Report the progress once we are done indexing this file
994-
liftIO $ waitBarrier indexDoneB
995-
992+
-- Report the progress once we are done indexing this file
993+
post = do
996994
mdone <- atomically $ do
997995
-- Remove current element from pending
998996
pending <- stateTVar indexPending $
@@ -1001,22 +999,25 @@ indexHieFile se mod_summary srcPath !hash hf = do
1001999
-- If we are done, report and reset completed
10021000
whenMaybe (HashMap.null pending) $
10031001
swapTVar indexCompleted 0
1004-
whenJust (lspEnv se) $ \env -> LSP.runLspT env $
1005-
when (coerce $ ideTesting se) $
1006-
LSP.sendNotification (LSP.SMethod_CustomMethod (Proxy @"ghcide/reference/ready")) $
1007-
toJSON $ fromNormalizedFilePath srcPath
1008-
whenJust mdone $ \done ->
1009-
modifyVar_ indexProgressToken $ \tok -> do
1010-
whenJust (lspEnv se) $ \env -> LSP.runLspT env $
1011-
whenJust tok $ \token ->
1012-
LSP.sendNotification LSP.SMethod_Progress $ LSP.ProgressParams token $
1013-
toJSON $
1014-
LSP.WorkDoneProgressEnd
1015-
{ _kind = LSP.AString @"end"
1016-
, _message = Just $ "Finished indexing " <> T.pack (show done) <> " files"
1017-
}
1018-
-- We are done with the current indexing cycle, so destroy the token
1019-
pure Nothing
1002+
1003+
atomically $ do
1004+
writeTQueue indexProgressThread $ do
1005+
whenJust (lspEnv se) $ \env -> LSP.runLspT env $
1006+
when (coerce $ ideTesting se) $
1007+
LSP.sendNotification (LSP.SMethod_CustomMethod (Proxy @"ghcide/reference/ready")) $
1008+
toJSON $ fromNormalizedFilePath srcPath
1009+
whenJust mdone $ \done ->
1010+
modifyVar_ indexProgressToken $ \tok -> do
1011+
whenJust (lspEnv se) $ \env -> LSP.runLspT env $
1012+
whenJust tok $ \token ->
1013+
LSP.sendNotification LSP.SMethod_Progress $ LSP.ProgressParams token $
1014+
toJSON $
1015+
LSP.WorkDoneProgressEnd
1016+
{ _kind = LSP.AString @"end"
1017+
, _message = Just $ "Finished indexing " <> T.pack (show done) <> " files"
1018+
}
1019+
-- We are done with the current indexing cycle, so destroy the token
1020+
pure Nothing
10201021

10211022
writeAndIndexHieFile :: HscEnv -> ShakeExtras -> ModSummary -> NormalizedFilePath -> [GHC.AvailInfo] -> HieASTs Type -> BS.ByteString -> IO [FileDiagnostic]
10221023
writeAndIndexHieFile hscEnv se mod_summary srcPath exports ast source =

0 commit comments

Comments
 (0)