on master help sign in
add BackgroundWriter to ensure that files are fully written
commit249090acaa970a3d3ffca169640d8959e4632369 authorEvan Czaplicki <evancz@users.noreply.github.com> dateTue 2 Jul 2019, 22:09 (7 years ago) parentbe9e78a
2 files changed+43
builder/src/BackgroundWriter.hs+42 elm.cabal+1
builder/src/BackgroundWriter.hsnew file+42
@@ -0,0 +1,42 @@
1+{-# LANGUAGE BangPatterns #-}
2+module BackgroundWriter
3+ ( Scope
4+ , withScope
5+ , writeBinary
6+ )
7+ where
8+
9+
10+import Control.Concurrent (forkIO)
11+import Control.Concurrent.MVar (MVar, newEmptyMVar, newMVar, putMVar, takeMVar)
12+import qualified Data.Binary as Binary
13+import Data.Foldable (traverse_)
14+
15+import qualified File
16+
17+
18+
19+-- BACKGROUND WRITER
20+
21+
22+newtype Scope =
23+ Scope (MVar [MVar ()])
24+
25+
26+withScope :: (Scope -> IO a) -> IO a
27+withScope callback =
28+ do workList <- newMVar []
29+ result <- callback (Scope workList)
30+ mvars <- takeMVar workList
31+ traverse_ takeMVar mvars
32+ return result
33+
34+
35+writeBinary :: (Binary.Binary a) => Scope -> FilePath -> a -> IO ()
36+writeBinary (Scope workList) path value =
37+ do mvar <- newEmptyMVar
38+ _ <- forkIO (File.writeBinary path value >> putMVar mvar ())
39+ oldWork <- takeMVar workList
40+ let !newWork = mvar:oldWork
41+ putMVar workList newWork
42+
elm.cabal+1
@@ -78,6 +78,7 @@ Executable elm
7878
7979 -- from builder/
8080 Build
81+ BackgroundWriter
8182 Deps.Bump
8283 Deps.Diff
8384 Deps.Registry