@@ -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