Skip to content

Commit bb618f1

Browse files
committed
Simplify code
1 parent 4ef762b commit bb618f1

File tree

1 file changed

+18
-30
lines changed

1 file changed

+18
-30
lines changed

src/Control/Distributed/Process.hs

Lines changed: 18 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -163,17 +163,11 @@ import Control.Monad.IO.Class (liftIO)
163163
import Control.Applicative ((<$>))
164164
import Control.Monad.Reader (ask)
165165
import Control.Concurrent.MVar
166-
( newEmptyMVar
166+
( MVar
167+
, newEmptyMVar
167168
, takeMVar
168169
, putMVar
169170
)
170-
import Control.Concurrent.STM (atomically)
171-
import Control.Concurrent.STM.TMVar
172-
( TMVar
173-
, newEmptyTMVarIO
174-
, putTMVar
175-
, takeTMVar
176-
)
177171
import Control.Distributed.Static
178172
( Closure
179173
, closure
@@ -413,29 +407,23 @@ spawnChannelLocal proc = do
413407
callLocal ::
414408
Process a -- ^ Process to run
415409
-> Process a -- ^ Value returned
416-
callLocal proc = do
410+
callLocal proc = mask_ $ do
417411
parent <- getSelfPid
418-
mv <- liftIO newEmptyTMVarIO :: Process (TMVar (Either SomeException a))
412+
mv <- liftIO newEmptyMVar :: Process (MVar (Either SomeException a))
419413
child <- spawnLocal $ mask $ \release -> do
420-
link parent
421414
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)
427419
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

Comments
 (0)