Skip to content

Commit c778f9d

Browse files
committed
refactor: streamline ShakeRestartArgs and enhance database queue access
1 parent e963e61 commit c778f9d

File tree

3 files changed

+21
-18
lines changed

3 files changed

+21
-18
lines changed

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

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,8 @@ import Development.IDE.Graph.Database (ShakeDatabase,
154154
import Development.IDE.Graph.Internal.Action (runActionInDbCb)
155155
import Development.IDE.Graph.Internal.Database (AsyncParentKill (AsyncParentKill))
156156
import Development.IDE.Graph.Internal.Types (DBQue, Step (..),
157-
getShakeStep)
157+
getShakeStep,
158+
shakeDataBaseQueue)
158159
import Development.IDE.Graph.Rule
159160
import Development.IDE.Types.Action
160161
import Development.IDE.Types.Diagnostics
@@ -858,14 +859,13 @@ delayedAction a = do
858859

859860

860861
data ShakeRestartArgs = ShakeRestartArgs
861-
{ sraVfs :: !VFSModified
862-
, sraReason :: !String
863-
, sraActions :: ![DelayedAction ()]
864-
, sraBetweenSessions :: IO [Key]
865-
, sraShakeControlQueue :: !ShakeControlQueue
866-
, sraCount :: !Int
867-
, sraWaitMVars :: ![MVar ()]
862+
{ sraVfs :: !VFSModified
863+
, sraReason :: !String
864+
, sraActions :: ![DelayedAction ()]
865+
, sraBetweenSessions :: IO [Key]
866+
, sraCount :: !Int
868867
-- ^ Just for debugging, how many restarts have been requested so far
868+
, sraWaitMVars :: ![MVar ()]
869869
}
870870

871871
instance Show ShakeRestartArgs where
@@ -881,7 +881,6 @@ instance Semigroup ShakeRestartArgs where
881881
, sraReason = sraReason a ++ "; " ++ sraReason b
882882
, sraActions = sraActions a ++ sraActions b
883883
, sraBetweenSessions = (++) <$> sraBetweenSessions a <*> sraBetweenSessions b
884-
, sraShakeControlQueue = sraShakeControlQueue a
885884
, sraCount = sraCount a + sraCount b
886885
, sraWaitMVars = sraWaitMVars a ++ sraWaitMVars b
887886
}
@@ -895,7 +894,7 @@ shakeRestart rts vfs reason acts ioActionBetweenShakeSession = do
895894
-- submit at the head of the queue,
896895
-- prefer restart request over any pending actions
897896
void $ submitWorkAtHead rts $ Left $
898-
toDyn $ ShakeRestartArgs vfs reason acts ioActionBetweenShakeSession rts 1 [waitMVar]
897+
toDyn $ ShakeRestartArgs vfs reason acts ioActionBetweenShakeSession 1 [waitMVar]
899898
-- Wait until the restart is done
900899
takeMVar waitMVar
901900

@@ -911,21 +910,23 @@ dynShakeRestart dy = case fromDynamic dy of
911910
runRestartTask :: Recorder (WithPriority Log) -> MVar IdeState -> ShakeRestartArgs -> IO ()
912911
runRestartTask recorder ideStateVar shakeRestartArgs = do
913912
IdeState {shakeDb, shakeSession, shakeExtras, shakeDatabaseProfile} <- readMVar ideStateVar
913+
let shakeControlQueue = shakeDataBaseQueue shakeDb
914914
let prepareRestart sra@ShakeRestartArgs {..} = do
915915
keys <- sraBetweenSessions
916916
-- it is every important to update the dirty keys after we enter the critical section
917917
-- see Note [Housekeeping rule cache and dirty key outside of hls-graph]
918918
atomically $ modifyTVar' (dirtyKeys shakeExtras) $ \x -> foldl' (flip insertKeySet) x keys
919+
sleep 0.2
919920
-- Check if there is another restart request pending, if so, we run that one too
920-
readAndGo sra sraShakeControlQueue
921-
readAndGo sra sraShakeControlQueue = do
922-
nextRestartArg <- atomically $ tryReadTaskQueue sraShakeControlQueue
921+
readAndGo sra
922+
readAndGo sra = do
923+
nextRestartArg <- atomically $ tryReadTaskQueue shakeControlQueue
923924
case nextRestartArg of
924925
Nothing -> return sra
925926
Just (Left dy) -> do
926927
res <- prepareRestart $ dynShakeRestart dy
927928
return $ sra <> res
928-
Just (Right _) -> readAndGo sra sraShakeControlQueue
929+
Just (Right _) -> readAndGo sra
929930
withMVar'
930931
shakeSession
931932
( \runner -> do
@@ -1049,7 +1050,7 @@ newSession recorder extras@ShakeExtras{..} vfsMod shakeDb acts reason = do
10491050
parentTid <- myThreadId
10501051
workThread <- asyncWithUnmask $ \x -> do
10511052
childThreadId <- myThreadId
1052-
logWith recorder Info $ LogShakeText ("Starting shake thread: " <> T.pack (show childThreadId) <> " (parent: " <> T.pack (show parentTid) <> ")")
1053+
-- logWith recorder Info $ LogShakeText ("shake thread: " <> T.pack (show childThreadId) <> " (parent: " <> T.pack (show parentTid) <> ")")
10531054
workRun start x
10541055

10551056
-- Cancelling is required to flush the Shake database when either

hls-graph/src/Development/IDE/Graph/Internal/Database.hs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ builderOneCoroutine isSingletonTask db stack id =
127127
builderOneCoroutine' RunFirst isSingletonTask db stack id
128128
where
129129
builderOneCoroutine' :: RunFirst -> IsSingletonTask -> Database -> Stack -> Key -> IO BuildContinue
130-
builderOneCoroutine' rf isSingletonTask db@Database {..} stack id = mask $ \restore -> do
130+
builderOneCoroutine' rf isSingletonTask db@Database {..} stack id = do
131131
traceEvent ("builderOne: " ++ show id) return ()
132132
liftIO $ atomicallyNamed "builder" $ do
133133
-- Spawn the id if needed
@@ -140,7 +140,7 @@ builderOneCoroutine isSingletonTask db stack id =
140140
IsSingleton ->
141141
return $
142142
BCContinue $ fmap (BCStop id) $
143-
restore (refresh db stack id s) `catch` \e@(SomeException _) -> do
143+
refresh db stack id s `catch` \e@(SomeException _) -> do
144144
atomically $ SMap.focus (updateStatus $ Exception current e s) id databaseValues
145145
throw e
146146
NotSingleton -> do
@@ -149,7 +149,6 @@ builderOneCoroutine isSingletonTask db stack id =
149149
\e -> atomically $ SMap.focus (updateStatus $ Exception current e s) id databaseValues
150150
return $ BCContinue $ builderOneCoroutine' RunLater isSingletonTask db stack id
151151
Clean r -> return $ BCStop id r
152-
-- force here might contains async exceptions from previous runs
153152
Running _step _s
154153
| memberStack id stack -> throw $ StackException stack
155154
| otherwise -> if rf == RunFirst

hls-graph/src/Development/IDE/Graph/Internal/Types.hs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,9 @@ data Database = Database {
136136
}
137137

138138

139+
shakeDataBaseQueue :: ShakeDatabase -> DBQue
140+
shakeDataBaseQueue = databaseQueue . (\(ShakeDatabase _ _ db) -> db)
141+
139142
databaseGetActionQueueLength :: Database -> STM Int
140143
databaseGetActionQueueLength db = do
141144
counTaskQueue (databaseQueue db)

0 commit comments

Comments
 (0)