@@ -62,6 +62,7 @@ import Data.Either
6262import qualified Crypto.Hash.SHA1 as H
6363import qualified Data.ByteString.Char8 as B
6464import Data.ByteString.Base16 (encode )
65+ import Control.Concurrent.Async
6566
6667import DynFlags (gopt_set , gopt_unset ,
6768 updOptLevel )
@@ -346,7 +347,6 @@ loadSession dir = liftIO $ do
346347 return res
347348
348349 lock <- newLock
349- cradle_lock <- newLock
350350
351351 -- This caches the mapping from hie.yaml + Mod.hs -> [String]
352352 sessionOpts <- return $ \ (hieYaml, file) -> do
@@ -373,17 +373,39 @@ loadSession dir = liftIO $ do
373373 finished_barrier <- newBarrier
374374 -- fork a new thread here which won't be killed by shake
375375 -- throwing an async exception
376- void $ forkIO $ withLock cradle_lock $ do
377- putStrLn $ " Shelling out to cabal " <> show file
376+ void $ forkIO $ do
377+ putStrLn $ " Consulting the cradle for " <> show file
378378 cradle <- maybe (loadImplicitCradle $ addTrailingPathSeparator dir) loadCradle hieYaml
379379 opts <- cradleToSessionOpts cradle cfp
380380 print opts
381381 res <- fst <$> session (hieYaml, toNormalizedFilePath' cfp, opts)
382382 signalBarrier finished_barrier res
383383 waitBarrier finished_barrier
384- return $ \ file -> liftIO $ mask_ $ withLock lock $ do
385- hieYaml <- cradleLoc file
386- sessionOpts (hieYaml, file)
384+
385+ dummyAs <- async $ return (error " Uninitialised" )
386+ runningCradle <- newIORef dummyAs
387+ -- The main function which gets options for a file. We only want one of these running
388+ -- at a time.
389+ let getOptions file = do
390+ hieYaml <- cradleLoc file
391+ sessionOpts (hieYaml, file)
392+ -- The lock is on the `runningCradle` resource
393+ return $ \ file -> liftIO $ withLock lock $ do
394+ as <- readIORef runningCradle
395+ finished <- poll as
396+ case finished of
397+ Just {} -> do
398+ as <- async $ getOptions file
399+ writeIORef runningCradle as
400+ wait as
401+ -- If it's not finished then wait and then get options, this could of course be killed still
402+ Nothing -> do
403+ _ <- wait as
404+ getOptions file
405+
406+
407+
408+
387409
388410checkDependencyInfo :: Map. Map FilePath (Maybe UTCTime ) -> IO Bool
389411checkDependencyInfo old_di = do
0 commit comments