Add withTimeout
[opengl.git] / src / Language / Haskell / LSP / Test / Compat.hs
1 {-# LANGUAGE CPP #-}
2 -- For some reason ghc warns about not using
3 -- Control.Monad.IO.Class but it's needed for
4 -- MonadIO
5 {-# OPTIONS_GHC -Wunused-imports #-}
6 module Language.Haskell.LSP.Test.Compat where
7
8 import Data.Maybe
9
10 #if MIN_VERSION_process(1,6,3)
11 import System.Process hiding (getPid)
12 import qualified System.Process (getPid)
13 #else
14 import System.Process
15 import System.Process.Internals
16 import Control.Concurrent.MVar
17 #endif
18
19 #ifdef mingw32_HOST_OS
20 import qualified System.Win32.Process
21 #else
22 import qualified System.Posix.Process
23 #endif
24
25
26 getCurrentProcessID :: IO Int
27 #ifdef mingw32_HOST_OS
28 getCurrentProcessID = fromIntegral <$> System.Win32.Process.getCurrentProcessId
29 #else
30 getCurrentProcessID = fromIntegral <$> System.Posix.Process.getProcessID
31 #endif
32
33 getProcessID :: ProcessHandle -> IO Int
34 getProcessID p = fromIntegral . fromJust <$> getProcessID' p
35   where
36 #if MIN_VERSION_process(1,6,3)
37   getProcessID' = System.Process.getPid
38 #else
39   getProcessID' (ProcessHandle mh _ _) = do
40     p_ <- readMVar mh
41     case p_ of
42 #ifdef mingw32_HOST_OS
43       OpenHandle h -> do
44         pid <- System.Win32.Process.getProcessId h
45         return $ Just pid
46 #else
47       OpenHandle pid -> return $ Just pid
48 #endif
49       _ -> return Nothing
50 #endif