[Git][ghc/ghc][wip/T17922] Session: Memoize stderrSupportsAnsiColors

Ben Gamari gitlab at gitlab.haskell.org
Mon Mar 30 15:14:53 UTC 2020



Ben Gamari pushed to branch wip/T17922 at Glasgow Haskell Compiler / GHC


Commits:
6bf85e01 by Ben Gamari at 2020-03-30T11:14:43-04:00
Session: Memoize stderrSupportsAnsiColors

Not only is this a reasonable efficiency measure but it avoids making
reentrant calls into ncurses, which is not thread-safe. See #17922.

- - - - -


2 changed files:

- compiler/GHC/Driver/Session.hs
- compiler/main/SysTools/Terminal.hs


Changes:

=====================================
compiler/GHC/Driver/Session.hs
=====================================
@@ -1253,7 +1253,6 @@ initDynFlags dflags = do
                          `catchIOError` \_ -> return False
  ghcNoUnicodeEnv <- lookupEnv "GHC_NO_UNICODE"
  let useUnicode' = isNothing ghcNoUnicodeEnv && canUseUnicode
- canUseColor <- stderrSupportsAnsiColors
  maybeGhcColorsEnv  <- lookupEnv "GHC_COLORS"
  maybeGhcColoursEnv <- lookupEnv "GHC_COLOURS"
  let adjustCols (Just env) = Col.parseScheme env
@@ -1270,7 +1269,7 @@ initDynFlags dflags = do
         nextWrapperNum = wrapperNum,
         useUnicode    = useUnicode',
         useColor      = useColor',
-        canUseColor   = canUseColor,
+        canUseColor   = stderrSupportsAnsiColors,
         colScheme     = colScheme',
         rtldInfo      = refRtldInfo,
         rtccInfo      = refRtccInfo


=====================================
compiler/main/SysTools/Terminal.hs
=====================================
@@ -18,6 +18,8 @@ import qualified Graphics.Win32 as Win32
 import qualified System.Win32 as Win32
 #endif
 
+import System.IO.Unsafe
+
 #if defined(mingw32_HOST_OS) && !defined(WINAPI)
 # if defined(i386_HOST_ARCH)
 #  define WINAPI stdcall
@@ -28,9 +30,15 @@ import qualified System.Win32 as Win32
 # endif
 #endif
 
+-- | Does the controlling terminal support ANSI color sequences?
+-- This memoized to avoid thread-safety issues in ncurses (see #17922).
+stderrSupportsAnsiColors :: Bool
+stderrSupportsAnsiColors = unsafePerformIO stderrSupportsAnsiColors'
+{-# NOINLINE stderrSupportsAnsiColors #-}
+
 -- | Check if ANSI escape sequences can be used to control color in stderr.
-stderrSupportsAnsiColors :: IO Bool
-stderrSupportsAnsiColors = do
+stderrSupportsAnsiColors' :: IO Bool
+stderrSupportsAnsiColors' = do
 #if defined(MIN_VERSION_terminfo)
   queryTerminal stdError `andM` do
     (termSupportsColors <$> setupTermFromEnv)



View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/6bf85e01e9c3baec32e95a0ff129f69c8b0b6230

-- 
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/6bf85e01e9c3baec32e95a0ff129f69c8b0b6230
You're receiving this email because of your account on gitlab.haskell.org.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/ghc-commits/attachments/20200330/ba7f1a41/attachment-0001.html>


More information about the ghc-commits mailing list