Andreas Klebinger pushed to branch wip/andreask/hadrian_race at Glasgow Haskell Compiler / GHC

Commits:

2 changed files:

Changes:

  • libraries/ghc-boot/GHC/Unit/Database.hs
    ... ... @@ -336,11 +336,10 @@ withLockedPackageDb mode file =
    336 336
         (lockPackageDbWith (lock_mode mode) file)
    
    337 337
         unlockPackageDb
    
    338 338
       where
    
    339
    -    bracket_for_mode = 
    
    339
    +   bracket_for_mode =
    
    340 340
           case mode of
    
    341
    -        DbOpenReadOnly  -> bracket
    
    342
    -        DbOpenReadWrite -> bracketOnError
    
    343
    -  where
    
    341
    +        DbOpenReadOnly    -> bracket
    
    342
    +        DbOpenReadWrite{} -> bracketOnError
    
    344 343
        lock_mode :: DbOpenMode m t -> LockMode
    
    345 344
        lock_mode DbOpenReadOnly = SharedLock
    
    346 345
        lock_mode DbOpenReadWrite{} = ExclusiveLock
    
    ... ... @@ -405,6 +404,14 @@ data DbMode = DbReadOnly | DbReadWrite deriving Eq
    405 404
     -- | 'DbOpenMode' holds a value of type @t@ but only in 'DbReadWrite' mode.  So
    
    406 405
     -- it is like 'Maybe' but with a type argument for the mode to enforce that the
    
    407 406
     -- mode is used consistently.
    
    407
    +--
    
    408
    +-- We use the argument to DbOpenReadWrite only when modifying the DB for two uses:
    
    409
    +-- * Storing the Din two cases:
    
    410
    +-- When modifying the DB however the lock is meant to outlive
    
    411
    +-- intermittent read operations. Which is why for writes we typically store the lock in DbOpenMode.
    
    412
    +-- and pass it along the call path.
    
    413
    +--
    
    414
    +-- See Note [ghc-pkg database locking] and withLockedPackageDb
    
    408 415
     data DbOpenMode (mode :: DbMode) t where
    
    409 416
       DbOpenReadOnly  ::      DbOpenMode 'DbReadOnly t
    
    410 417
       DbOpenReadWrite :: t -> DbOpenMode 'DbReadWrite t
    
    ... ... @@ -430,7 +437,7 @@ isDbOpenReadMode = \case
    430 437
     --
    
    431 438
     readPackageDbForGhc :: FilePath -> IO [DbUnitInfo]
    
    432 439
     readPackageDbForGhc file = do
    
    433
    -   hPutStrLn stderr $ "readPackageDbForGhc:" ++ show file
    
    440
    +   -- hPutStrLn stderr $ "readPackageDbForGhc:" ++ show file
    
    434 441
        withLockedPackageDb DbOpenReadOnly file $ \_ -> do
    
    435 442
           decodeFromFile file DbOpenReadOnly getDbForGhc >>= \case
    
    436 443
              (pkgs, DbOpenReadOnly) -> return pkgs
    
    ... ... @@ -450,10 +457,7 @@ readPackageDbForGhc file = do
    450 457
     --
    
    451 458
     -- The incoming mode carries the exclusive lock if we are in R/W mode.
    
    452 459
     --
    
    453
    --- If we open the package db in read only mode, we get its contents. Otherwise
    
    454
    --- we additionally receive a PackageDbLock that represents a lock on the
    
    455
    --- database, so that we can safely update it later.
    
    456
    ---
    
    460
    +-- Returns the lock on the db as-is.
    
    457 461
     readPackageDbForGhcPkg :: Binary pkgs => FilePath -> DbOpenMode mode PackageDbLock ->
    
    458 462
                               IO (pkgs, DbOpenMode mode PackageDbLock)
    
    459 463
     readPackageDbForGhcPkg file mode =
    
    ... ... @@ -545,15 +549,9 @@ headerMagic = BS.Char8.pack "\0ghcpkg\0"
    545 549
     -- which it returns unchanged.
    
    546 550
     decodeFromFile :: FilePath -> DbOpenMode mode PackageDbLock -> Get pkgs ->
    
    547 551
                       IO (pkgs, DbOpenMode mode PackageDbLock)
    
    548
    -decodeFromFile file mode decoder = case mode of
    
    549
    -   -- DB is locked with shared access already, we just do the read.
    
    550
    -  DbOpenReadOnly -> do
    
    551
    -      (, DbOpenReadOnly) <$> decodeFileContents
    
    552
    -  DbOpenReadWrite{} -> do
    
    553
    -    -- When we open the package db in read/write mode, we receive an exclusive lock
    
    554
    -    -- on the database via the mode and return it so we can keep it for the duration of the
    
    555
    -    -- update.
    
    556
    -    -- If an exception is raised the caller releases the lock.
    
    552
    +decodeFromFile file mode decoder =
    
    553
    +   -- Return incoming lock together with the result.
    
    554
    +   -- See Note [ghc-pkg database locking]
    
    557 555
           (, mode) <$> decodeFileContents
    
    558 556
       where
    
    559 557
         decodeFileContents = withBinaryFile file ReadMode $ \hnd ->
    

  • utils/ghc-pkg/Main.hs
    ... ... @@ -885,8 +885,9 @@ database in both modes:
    885 885
       - In read-only mode, we take a *shared* lock before reading and unlock afterwards.
    
    886 886
         Locking is needed to avoid a concurrent invocation of ghc-pkg deleting files from
    
    887 887
         the db before we manage to read them. This occasionally happened in #22870.
    
    888
    -    (Note that, historically, we used to only lock the package db during reads on
    
    889
    -     Windows, but the justification seemed insufficient given #22870. See also #16773.)
    
    888
    +    (Note that, historically, only windows used to lock the package db during reads,
    
    889
    +    but the justification seemed insufficient given #22870. So now we always lock
    
    890
    +    even for reads. See also #16773.)
    
    890 891
     
    
    891 892
       - In read-write mode, we take an **exclusive** lock on the package database.
    
    892 893
         However, we don't automatically release the lock after the inner action completes
    
    ... ... @@ -970,20 +971,13 @@ readParseDatabase verbosity mb_user_conf mode use_cache path
    970 971
                     where
    
    971 972
                         confs = map (path </>) $ filter (".conf" `isSuffixOf`) fs
    
    972 973
     
    
    973
    -                    -- Read the package db, potentially locking the .cache file for r/w mode.
    
    974
    +                    -- Read the package db, using the given lock
    
    974 975
                         ignore_cache :: PackageDbLock -> (FilePath -> IO ()) -> IO (PackageDB mode)
    
    975 976
                         ignore_cache lock checkTime = do
    
    976
    -                        -- If we're opening for modification, we need to acquire a
    
    977
    -                        -- lock even if we don't open the cache now, because we are
    
    978
    -                        -- going to modify it later.
    
    979
    -
    
    980
    -                        -- mode' <- F.mapM (const $ GhcPkg.lockPackageDb cache) mode
    
    981
    -
    
    982 977
                             let doFile f = do checkTime f
    
    983 978
                                               parseSingletonPackageConf verbosity f
    
    984 979
                             pkgs <- mapM doFile confs
    
    985 980
     
    
    986
    -                        -- mkPackageDB pkgs mode'
    
    987 981
                             mkPackageDB pkgs (modeWithLock lock mode)
    
    988 982
     
    
    989 983
                         -- We normally report cache errors for read-only commands,