@@ -137,7 +137,7 @@ import qualified System.Process as P
137
137
import System.IO (hClose )
138
138
import System.IO.Error (isPermissionError )
139
139
import Control.Concurrent (threadDelay )
140
- import Control.Concurrent.Async (asyncWithUnmask )
140
+ import Control.Concurrent.Async (Async , asyncWithUnmask )
141
141
import qualified Control.Concurrent.Async as Async
142
142
import Control.Concurrent.STM (newEmptyTMVarIO , atomically , putTMVar , TMVar , readTMVar , tryReadTMVar , STM , throwSTM , catchSTM )
143
143
import System.Exit (ExitCode (ExitSuccess , ExitFailure ))
@@ -168,7 +168,7 @@ data Process stdin stdout stderr = Process
168
168
, pStdout :: ! stdout
169
169
, pStderr :: ! stderr
170
170
, pHandle :: ! P. ProcessHandle
171
- , pExitCode :: ! (TMVar ExitCode )
171
+ , pExitCode :: ! (Async ExitCode )
172
172
}
173
173
instance Show (Process stdin stdout stderr ) where
174
174
show p = " Running process: " ++ show (pConfig p)
@@ -222,8 +222,7 @@ startProcess pConfig'@ProcessConfig {..} = liftIO $ do
222
222
<*> ssCreate pcStdout pConfig moutH
223
223
<*> ssCreate pcStderr pConfig merrH
224
224
225
- pExitCode <- newEmptyTMVarIO
226
- waitingThread <- asyncWithUnmask $ \ unmask -> do
225
+ pExitCode <- asyncWithUnmask $ \ unmask -> do
227
226
ec <- unmask $ -- make sure the masking state from a bracket isn't inherited
228
227
if multiThreadedRuntime
229
228
then P. waitForProcess pHandle
@@ -239,12 +238,11 @@ startProcess pConfig'@ProcessConfig {..} = liftIO $ do
239
238
Nothing -> loop $ min maxDelay (delay * 2 )
240
239
Just ec -> pure ec
241
240
loop minDelay
242
- atomically $ putTMVar pExitCode ec
243
241
return ec
244
242
245
- let waitForProcess = Async. wait waitingThread :: IO ExitCode
243
+ let waitForProcess = Async. wait pExitCode :: IO ExitCode
246
244
let pCleanup = pCleanup1 `finally` do
247
- _ :: ExitCode <- Async. poll waitingThread >>= \ case
245
+ _ :: ExitCode <- Async. poll pExitCode >>= \ case
248
246
-- Process already exited, nothing to do
249
247
Just r -> either throwIO return r
250
248
@@ -596,7 +594,7 @@ waitExitCode = liftIO . atomically . waitExitCodeSTM
596
594
--
597
595
-- @since 0.1.0.0
598
596
waitExitCodeSTM :: Process stdin stdout stderr -> STM ExitCode
599
- waitExitCodeSTM = readTMVar . pExitCode
597
+ waitExitCodeSTM = Async. waitSTM . pExitCode
600
598
601
599
-- | Check if a process has exited and, if so, return its 'ExitCode'.
602
600
--
@@ -608,7 +606,9 @@ getExitCode = liftIO . atomically . getExitCodeSTM
608
606
--
609
607
-- @since 0.1.0.0
610
608
getExitCodeSTM :: Process stdin stdout stderr -> STM (Maybe ExitCode )
611
- getExitCodeSTM = tryReadTMVar . pExitCode
609
+ getExitCodeSTM p = Async. pollSTM (pExitCode p) >>= \ case
610
+ Nothing -> return Nothing
611
+ Just er -> either throwSTM (return . Just ) er
612
612
613
613
-- | Wait for a process to exit, and ensure that it exited
614
614
-- successfully. If not, throws an 'ExitCodeException'.
@@ -625,7 +625,7 @@ checkExitCode = liftIO . atomically . checkExitCodeSTM
625
625
-- @since 0.1.0.0
626
626
checkExitCodeSTM :: Process stdin stdout stderr -> STM ()
627
627
checkExitCodeSTM p = do
628
- ec <- readTMVar (pExitCode p)
628
+ ec <- Async. waitSTM (pExitCode p)
629
629
case ec of
630
630
ExitSuccess -> return ()
631
631
_ -> throwSTM ExitCodeException
0 commit comments