summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoman Smrž <roman.smrz@seznam.cz>2025-05-17 08:14:46 +0200
committerRoman Smrž <roman.smrz@seznam.cz>2025-05-17 08:14:46 +0200
commitc7930f132d1fff4fba0f8fbee6aa0edb38c40269 (patch)
tree5de429fe82bd0290c7497f6e2d18593a669aa4d4
parent2ee87680556ccf26ef8d415950e7f31034d647c4 (diff)
Output lock for writing without Terminal instance
-rw-r--r--src/Output.hs11
1 files changed, 8 insertions, 3 deletions
diff --git a/src/Output.hs b/src/Output.hs
index 5838342..b4678f5 100644
--- a/src/Output.hs
+++ b/src/Output.hs
@@ -11,6 +11,7 @@ module Output (
outputFootnote,
) where
+import Control.Concurrent.MVar
import Control.Monad
import Control.Monad.Catch
import Control.Monad.IO.Class
@@ -25,7 +26,8 @@ import Terminal
data Output = Output
- { outTerminal :: Maybe TerminalOutput
+ { outLock :: MVar ()
+ , outTerminal :: Maybe TerminalOutput
, outLogs :: [ Handle ]
, outTest :: [ Handle ]
}
@@ -49,7 +51,9 @@ data OutputFootnote = OutputFootnote
withOutput :: [ OutputType ] -> (Output -> IO a) -> IO a
-withOutput types inner = go types (Output Nothing [] [])
+withOutput types inner = do
+ lock <- newMVar ()
+ go types (Output lock Nothing [] [])
where
go (TerminalOutput : ts) out = do
term <- initTerminalOutput
@@ -76,7 +80,8 @@ outStrLn Output {..} h text
| Just tout <- outTerminal, terminalHandle tout == h = do
void $ newLine tout text
| otherwise = do
- T.hPutStrLn h text
+ withMVar outLock $ \_ -> do
+ T.hPutStrLn h text
outputMessage :: MonadIO m => Output -> Text -> m ()
outputMessage out msg = outputEvent out (OutputMessage msg)