add BackgroundWriter to ensure that files are fully written

This commit is contained in:
Evan Czaplicki 2019-07-02 22:09:05 -04:00
parent be9e78a134
commit 249090acaa
2 changed files with 43 additions and 0 deletions

View File

@ -0,0 +1,42 @@
{-# LANGUAGE BangPatterns #-}
module BackgroundWriter
( Scope
, withScope
, writeBinary
)
where
import Control.Concurrent (forkIO)
import Control.Concurrent.MVar (MVar, newEmptyMVar, newMVar, putMVar, takeMVar)
import qualified Data.Binary as Binary
import Data.Foldable (traverse_)
import qualified File
-- BACKGROUND WRITER
newtype Scope =
Scope (MVar [MVar ()])
withScope :: (Scope -> IO a) -> IO a
withScope callback =
do workList <- newMVar []
result <- callback (Scope workList)
mvars <- takeMVar workList
traverse_ takeMVar mvars
return result
writeBinary :: (Binary.Binary a) => Scope -> FilePath -> a -> IO ()
writeBinary (Scope workList) path value =
do mvar <- newEmptyMVar
_ <- forkIO (File.writeBinary path value >> putMVar mvar ())
oldWork <- takeMVar workList
let !newWork = mvar:oldWork
putMVar workList newWork

View File

@ -78,6 +78,7 @@ Executable elm
-- from builder/
Build
BackgroundWriter
Deps.Bump
Deps.Diff
Deps.Registry