git.iko.soy/distributed-process
on master help sign in
add withMonitor
commit1b7a844dc642fdf9edce70e7ce9a3d4d30549181 authorSimon Marlow <marlowsd@gmail.com> dateWed 12 Dec 2012, 10:01 (14 years ago) parentd08343d
2 files changed+17
distributed-process/src/Control/Distributed/Process.hs+2 distributed-process/src/Control/Distributed/Process/Internal/Primitives.hs+15
distributed-process/src/Control/Distributed/Process.hs+2
@@ -62,6 +62,7 @@ module Control.Distributed.Process
6262 , monitorNode
6363 , monitorPort
6464 , unmonitor
65+ , withMonitor
6566 , MonitorRef
6667 , ProcessLinkException(..)
6768 , NodeLinkException(..)
@@ -204,6 +205,7 @@ import Control.Distributed.Process.Internal.Primitives
204205 , monitorNode
205206 , monitorPort
206207 , unmonitor
208+ , withMonitor
207209 -- Logging
208210 , say
209211 -- Registry
distributed-process/src/Control/Distributed/Process/Internal/Primitives.hs+15
@@ -36,6 +36,7 @@ module Control.Distributed.Process.Internal.Primitives
3636 , unlink
3737 , monitor
3838 , unmonitor
39+ , withMonitor
3940 -- * Logging
4041 , say
4142 -- * Registry
@@ -436,6 +437,20 @@ link = sendCtrlMsg Nothing . Link . ProcessIdentifier
436437monitor :: ProcessId -> Process MonitorRef
437438monitor = monitor' . ProcessIdentifier
438439
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+
439454-- | Remove a link
440455--
441456-- This is synchronous in the sense that once it returns you are guaranteed