git.iko.soy/distributed-process
on master help sign in
add withMonitor
1b7a844Simon Marlow
distributed-process/src/Control/Distributed/Process.hs+2
@@ -62,6 +62,7 @@ module Control.Distributed.Process
62 , monitorNode
62 , monitorNode
63 , monitorPort
63 , monitorPort
64 , unmonitor
64 , unmonitor
65+ , withMonitor
65 , MonitorRef
66 , MonitorRef
66 , ProcessLinkException(..)
67 , ProcessLinkException(..)
67 , NodeLinkException(..)
68 , NodeLinkException(..)
@@ -204,6 +205,7 @@ import Control.Distributed.Process.Internal.Primitives
204 , monitorNode
205 , monitorNode
205 , monitorPort
206 , monitorPort
206 , unmonitor
207 , unmonitor
208+ , withMonitor
207 -- Logging
209 -- Logging
208 , say
210 , say
209 -- Registry
211 -- Registry
distributed-process/src/Control/Distributed/Process/Internal/Primitives.hs+15
@@ -36,6 +36,7 @@ module Control.Distributed.Process.Internal.Primitives
36 , unlink
36 , unlink
37 , monitor
37 , monitor
38 , unmonitor
38 , unmonitor
39+ , withMonitor
39 -- * Logging
40 -- * Logging
40 , say
41 , say
41 -- * Registry
42 -- * Registry
@@ -436,6 +437,20 @@ link = sendCtrlMsg Nothing . Link . ProcessIdentifier
436monitor :: ProcessId -> Process MonitorRef
437monitor :: ProcessId -> Process MonitorRef
437monitor = monitor' . ProcessIdentifier
438monitor = monitor' . ProcessIdentifier
438
439
440+-- | Establishes temporary monitoring of another process.
441+--
442+-- @withMonitor pid code@ sets up monitoring of @pid@ for the duration
443+-- of @code@. Note: although monitoring is no longer active when
444+-- @withMonitor@ returns, there might still be unreceived monitor
445+-- messages in the queue.
446+--
447+withMonitor :: ProcessId -> Process a -> Process a
448+withMonitor pid code = bracket (monitor pid) unmonitor (\_ -> code)
449+ -- unmonitor blocks waiting for the response, so there's a possibility
450+ -- that an exception might interrupt withMonitor before the unmonitor
451+ -- has completed. I think that's better than making the unmonitor
452+ -- uninterruptible.
453+
439-- | Remove a link
454-- | Remove a link
440--
455--
441-- This is synchronous in the sense that once it returns you are guaranteed
456-- This is synchronous in the sense that once it returns you are guaranteed