summaryrefslogtreecommitdiff
path: root/src/AtomicPrint.hs
blob: 82a85529826ff5c9a2962223bd85fb8d47e4d3e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
{-# OPTIONS_GHC -fno-full-laziness -fno-cse #-}
module AtomicPrint (
  atomicPrint, atomicPrintS,
  atomicPrintNoWait, atomicPrintNoWaitS,
) where

import Control.Concurrent
import Control.Monad (void)
import Data.Text qualified as T
import Data.Text.IO.Utf8 qualified as T
import Data.Text (Text)
import System.IO
import System.IO.Unsafe (unsafePerformIO)


{-# NOINLINE mutex #-}
mutex :: MVar ()
mutex = unsafePerformIO (newMVar ())

atomicPrintS :: String -> IO ()
atomicPrintS = atomicPrint . T.pack

atomicPrint :: Text -> IO ()
atomicPrint text =
  withMVar mutex $ \() ->
    T.putStrLn text >> hFlush stdout

atomicPrintNoWaitS :: String -> IO ()
atomicPrintNoWaitS = atomicPrintNoWait . T.pack

-- | Does not block, so if you've masked exceptions, nothing will come through here
atomicPrintNoWait :: Text -> IO ()
atomicPrintNoWait text = void $ forkIO $ atomicPrint text