@@ -163,17 +163,11 @@ import Control.Monad.IO.Class (liftIO)
163
163
import Control.Applicative ((<$>) )
164
164
import Control.Monad.Reader (ask )
165
165
import Control.Concurrent.MVar
166
- ( newEmptyMVar
166
+ ( MVar
167
+ , newEmptyMVar
167
168
, takeMVar
168
169
, putMVar
169
170
)
170
- import Control.Concurrent.STM (atomically )
171
- import Control.Concurrent.STM.TMVar
172
- ( TMVar
173
- , newEmptyTMVarIO
174
- , putTMVar
175
- , takeTMVar
176
- )
177
171
import Control.Distributed.Static
178
172
( Closure
179
173
, closure
@@ -413,29 +407,23 @@ spawnChannelLocal proc = do
413
407
callLocal ::
414
408
Process a -- ^ Process to run
415
409
-> Process a -- ^ Value returned
416
- callLocal proc = do
410
+ callLocal proc = mask_ $ do
417
411
parent <- getSelfPid
418
- mv <- liftIO newEmptyTMVarIO :: Process (TMVar (Either SomeException a ))
412
+ mv <- liftIO newEmptyMVar :: Process (MVar (Either SomeException a ))
419
413
child <- spawnLocal $ mask $ \ release -> do
420
- link parent
421
414
ep <- try $ release $ proc
422
- liftIO $ atomically $ putTMVar mv ep
423
- withMonitor child $ do
424
- fetchResult child mv `catch` (\ e -> do exit child (show (e:: SomeException ))
425
- waitForExit child
426
- liftIO $ throwIO e)
415
+ liftIO $ putMVar mv ep
416
+ liftIO (fetchResult mv) `onException`
417
+ (do exit child " exception in parent process"
418
+ waitForExit child)
427
419
where
428
- waitForExit child =
429
- receiveWait
430
- [ matchIf (\ (ProcessMonitorNotification _ ch _) -> child == ch)
431
- (\ _ -> return () )
432
- ]
433
- fetchResult child mv = do
434
- receiveWait
435
- [ matchSTM (takeTMVar mv)
436
- (\ rs -> do waitForExit child -- avoid monitor events leak
437
- liftIO $ either throwIO return rs)
438
- , matchIf (\ (ProcessMonitorNotification _ ch _) -> child == ch)
439
- (\ (ProcessMonitorNotification _ _ reason) ->
440
- fail $ " callLocal: remote process died: " ++ show reason)
441
- ]
420
+ waitForExit child = do
421
+ bracket (monitor child)
422
+ (unmonitor)
423
+ (\ mRef -> receiveWait
424
+ [ matchIf (\ (ProcessMonitorNotification ref _ _) -> ref == mRef)
425
+ (\ _ -> return () )
426
+ ])
427
+ fetchResult mv = do
428
+ rs <- takeMVar mv
429
+ liftIO $ either throwIO return rs
0 commit comments