Skip to content

Commit 6951638

Browse files
committed
Extract atomically to call sites
1 parent c99446c commit 6951638

File tree

1 file changed

+18
-17
lines changed

1 file changed

+18
-17
lines changed

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

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -426,9 +426,9 @@ setValues :: IdeRule k v
426426
-> NormalizedFilePath
427427
-> Value v
428428
-> Vector FileDiagnostic
429-
-> IO ()
429+
-> STM ()
430430
setValues state key file val diags =
431-
atomically $ STM.insert (ValueWithDiagnostics (fmap toDyn val) diags) (toKey key file) state
431+
STM.insert (ValueWithDiagnostics (fmap toDyn val) diags) (toKey key file) state
432432

433433

434434
-- | Delete the value stored for a given ide build key
@@ -460,16 +460,17 @@ getValues ::
460460
Values ->
461461
k ->
462462
NormalizedFilePath ->
463-
IO (Maybe (Value v, Vector FileDiagnostic))
463+
STM (Maybe (Value v, Vector FileDiagnostic))
464464
getValues state key file = do
465-
atomically (STM.lookup (toKey key file) state) >>= \case
465+
STM.lookup (toKey key file) state >>= \case
466466
Nothing -> pure Nothing
467467
Just (ValueWithDiagnostics v diagsV) -> do
468-
let r = fmap (fromJust . fromDynamic @v) v
468+
let !r = seqValue $ fmap (fromJust . fromDynamic @v) v
469+
!res = (r,diagsV)
469470
-- Force to make sure we do not retain a reference to the HashMap
470471
-- and we blow up immediately if the fromJust should fail
471472
-- (which would be an internal error).
472-
evaluate (r `seqValue` Just (r, diagsV))
473+
return $ Just res
473474

474475
-- | Get all the files in the project
475476
knownTargets :: Action (Hashed KnownTargets)
@@ -480,11 +481,11 @@ knownTargets = do
480481
-- | Seq the result stored in the Shake value. This only
481482
-- evaluates the value to WHNF not NF. We take care of the latter
482483
-- elsewhere and doing it twice is expensive.
483-
seqValue :: Value v -> b -> b
484-
seqValue v b = case v of
485-
Succeeded ver v -> rnf ver `seq` v `seq` b
486-
Stale d ver v -> rnf d `seq` rnf ver `seq` v `seq` b
487-
Failed _ -> b
484+
seqValue :: Value v -> Value v
485+
seqValue val = case val of
486+
Succeeded ver v -> rnf ver `seq` v `seq` val
487+
Stale d ver v -> rnf d `seq` rnf ver `seq` v `seq` val
488+
Failed _ -> val
488489

489490
-- | Open a 'IdeState', should be shut using 'shakeShut'.
490491
shakeOpen :: Maybe (LSP.LanguageContextEnv Config)
@@ -906,7 +907,7 @@ useWithStaleFast' key file = do
906907
wait <- delayedAction $ mkDelayedAction ("C:" ++ show key ++ ":" ++ fromNormalizedFilePath file) Debug $ use key file
907908

908909
s@ShakeExtras{state} <- askShake
909-
r <- liftIO $ getValues state key file
910+
r <- liftIO $ atomically $ getValues state key file
910911
liftIO $ case r of
911912
-- block for the result if we haven't computed before
912913
Nothing -> do
@@ -1015,7 +1016,7 @@ defineEarlyCutoff' doDiagnostics cmp key file old mode action = do
10151016
(if optSkipProgress options key then id else inProgress progress file) $ do
10161017
val <- case old of
10171018
Just old | mode == RunDependenciesSame -> do
1018-
v <- liftIO $ getValues state key file
1019+
v <- liftIO $ atomically $ getValues state key file
10191020
case v of
10201021
-- No changes in the dependencies and we have
10211022
-- an existing successful result.
@@ -1034,10 +1035,10 @@ defineEarlyCutoff' doDiagnostics cmp key file old mode action = do
10341035
(do v <- action; liftIO $ evaluate $ force v) $
10351036
\(e :: SomeException) -> do
10361037
pure (Nothing, ([ideErrorText file $ T.pack $ show e | not $ isBadDependency e],Nothing))
1037-
modTime <- liftIO $ (currentValue . fst =<<) <$> getValues state GetModificationTime file
1038+
modTime <- liftIO $ (currentValue . fst =<<) <$> atomically (getValues state GetModificationTime file)
10381039
(bs, res) <- case res of
10391040
Nothing -> do
1040-
staleV <- liftIO $ getValues state key file
1041+
staleV <- liftIO $ atomically $ getValues state key file
10411042
pure $ case staleV of
10421043
Nothing -> (toShakeValue ShakeResult bs, Failed False)
10431044
Just v -> case v of
@@ -1048,7 +1049,7 @@ defineEarlyCutoff' doDiagnostics cmp key file old mode action = do
10481049
(Failed b, _) ->
10491050
(toShakeValue ShakeResult bs, Failed b)
10501051
Just v -> pure (maybe ShakeNoCutoff ShakeResult bs, Succeeded (vfsVersion =<< modTime) v)
1051-
liftIO $ setValues state key file res (Vector.fromList diags)
1052+
liftIO $ atomically $ setValues state key file res (Vector.fromList diags)
10521053
doDiagnostics diags
10531054
let eq = case (bs, fmap decodeShakeValue old) of
10541055
(ShakeResult a, Just (ShakeResult b)) -> cmp a b
@@ -1148,7 +1149,7 @@ updateFileDiagnostics :: MonadIO m
11481149
-> [(ShowDiagnostic,Diagnostic)] -- ^ current results
11491150
-> m ()
11501151
updateFileDiagnostics fp k ShakeExtras{logger, diagnostics, hiddenDiagnostics, publishedDiagnostics, state, debouncer, lspEnv} current = liftIO $ do
1151-
modTime <- (currentValue . fst =<<) <$> getValues state GetModificationTime fp
1152+
modTime <- (currentValue . fst =<<) <$> atomically (getValues state GetModificationTime fp)
11521153
let (currentShown, currentHidden) = partition ((== ShowDiag) . fst) current
11531154
uri = filePathToUri' fp
11541155
ver = vfsVersion =<< modTime

0 commit comments

Comments
 (0)