Andrea Vezzosi pushed to branch wip/romes/async-rpc at Glasgow Haskell Compiler / GHC

Commits:

1 changed file:

Changes:

  • compiler/GHC/Runtime/Utils.hs
    1
    +{-# LANGUAGE CPP #-}
    
    2
    +
    
    1 3
     module GHC.Runtime.Utils
    
    2 4
       ( runWithPipes
    
    3 5
       )
    
    ... ... @@ -10,6 +12,13 @@ import System.Process
    10 12
     import System.Process.CommunicationHandle
    
    11 13
     import System.IO
    
    12 14
     
    
    15
    +#if defined(mingw32_HOST_OS)
    
    16
    +import Control.Concurrent (rtsSupportsBoundThreads)
    
    17
    +import GHC.IO.Handle.Windows (handleToHANDLE)
    
    18
    +import GHC.IO.SubSystem (isWindowsNativeIO)
    
    19
    +import qualified System.Win32 as Win32
    
    20
    +#endif
    
    21
    +
    
    13 22
     -- | Spawn a process connected to us by two pipes. Important to close our copy
    
    14 23
     -- of the endpoints passed to the child so each side only keeps open copies it
    
    15 24
     -- needs. If we don't do that, then closing one end wouldn't interrupt reads on
    
    ... ... @@ -17,8 +26,8 @@ import System.IO
    17 26
     runWithPipes :: (CreateProcess -> IO ProcessHandle)
    
    18 27
                  -> FilePath -> [String] -> [String] -> IO (ProcessHandle, Handle, Handle)
    
    19 28
     runWithPipes createProc prog pre_opts opts = do
    
    20
    -    (rh, chTheyWrite) <- createWeReadTheyWritePipe
    
    21
    -    (chTheyRead, wh)  <- createTheyReadWeWritePipe
    
    29
    +    (rh, chTheyWrite) <- fixedCreateWeReadTheyWritePipe
    
    30
    +    (chTheyRead, wh)  <- fixedCreateTheyReadWeWritePipe
    
    22 31
         let args = pre_opts ++ (show chTheyWrite : show chTheyRead : opts)
    
    23 32
             closeTheirs = do
    
    24 33
               closeCommunicationHandle chTheyWrite
    
    ... ... @@ -28,3 +37,40 @@ runWithPipes createProc prog pre_opts opts = do
    28 37
                 `onException` (closeTheirs >> hClose rh >> hClose wh)
    
    29 38
         closeTheirs
    
    30 39
         return (ph, rh, wh)
    
    40
    +
    
    41
    +-- | 'createWeReadTheyWritePipe', except in a non-threaded runtime on Windows
    
    42
    +-- without WinIO. There process gives us a synchronous native 'Handle', and
    
    43
    +-- reading from it is a safe call to @ReadFile@, which blocks every Haskell
    
    44
    +-- thread until data arrives. As 'GHCi.Message.Async' always has a thread
    
    45
    +-- waiting to read the pipe, no other thread would get to run. So in that case
    
    46
    +-- we swap our end for a file descriptor 'Handle', whose reads only block the
    
    47
    +-- reading thread.
    
    48
    +fixedCreateWeReadTheyWritePipe :: IO (Handle, CommunicationHandle)
    
    49
    +fixedCreateWeReadTheyWritePipe = do
    
    50
    +    (h, ch) <- createWeReadTheyWritePipe
    
    51
    +    h' <- toFdHandleIfNeeded h
    
    52
    +    return (h', ch)
    
    53
    +
    
    54
    +-- | 'createTheyReadWeWritePipe', with the same exception as
    
    55
    +-- 'fixedCreateWeReadTheyWritePipe'.
    
    56
    +fixedCreateTheyReadWeWritePipe :: IO (CommunicationHandle, Handle)
    
    57
    +fixedCreateTheyReadWeWritePipe = do
    
    58
    +    (ch, h) <- createTheyReadWeWritePipe
    
    59
    +    h' <- toFdHandleIfNeeded h
    
    60
    +    return (ch, h')
    
    61
    +
    
    62
    +toFdHandleIfNeeded :: Handle -> IO Handle
    
    63
    +#if defined(mingw32_HOST_OS)
    
    64
    +-- The native 'Handle' has a finalizer that closes its @HANDLE@, so we wrap a
    
    65
    +-- duplicate and close the original.
    
    66
    +toFdHandleIfNeeded h
    
    67
    +  | not rtsSupportsBoundThreads && not isWindowsNativeIO = do
    
    68
    +      me <- Win32.getCurrentProcess
    
    69
    +      hh <- handleToHANDLE h
    
    70
    +      dup <- Win32.duplicateHandle me hh me 0 False Win32.dUPLICATE_SAME_ACCESS
    
    71
    +      hClose h
    
    72
    +      Win32.hANDLEToHandle dup
    
    73
    +  | otherwise = return h
    
    74
    +#else
    
    75
    +toFdHandleIfNeeded = return
    
    76
    +#endif