diff options
author | Adam Kocoloski <adam@cloudant.com> | 2010-08-12 02:27:12 -0400 |
---|---|---|
committer | Adam Kocoloski <adam@cloudant.com> | 2010-08-12 02:27:12 -0400 |
commit | 72d7a60124b4ee7e31912fe7ed3a50bbc5cb9f64 (patch) | |
tree | e62fdcea38de5d70d3fd4f7c860dabab0357f670 /apps/rexi/src/rexi_monitor.erl | |
parent | 4b47d4c5f39a1998c07af07cf7e6b2163ce1eae5 (diff) | |
parent | 85ae6ca690edad17ceb92b856623a0a599924c77 (diff) |
Add 'apps/rexi/' from commit '85ae6ca690edad17ceb92b856623a0a599924c77'
git-subtree-dir: apps/rexi
git-subtree-mainline: 4b47d4c5f39a1998c07af07cf7e6b2163ce1eae5
git-subtree-split: 85ae6ca690edad17ceb92b856623a0a599924c77
Diffstat (limited to 'apps/rexi/src/rexi_monitor.erl')
-rw-r--r-- | apps/rexi/src/rexi_monitor.erl | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/apps/rexi/src/rexi_monitor.erl b/apps/rexi/src/rexi_monitor.erl new file mode 100644 index 00000000..bbff22b3 --- /dev/null +++ b/apps/rexi/src/rexi_monitor.erl @@ -0,0 +1,40 @@ +-module(rexi_monitor). +-export([start/1, stop/1]). + +-include_lib("eunit/include/eunit.hrl"). + +%% @doc spawn_links a process which monitors the supplied list of items and +%% returns the process ID. If a monitored process exits, the caller will +%% receive a {rexi_DOWN, MonitoringPid, DeadPid, Reason} message. +-spec start([pid() | atom() | {atom(),node()}]) -> pid(). +start(Procs) -> + Parent = self(), + spawn_link(fun() -> + [erlang:monitor(process, P) || P <- Procs], + wait_monitors(Parent) + end). + +%% @doc Cleanly shut down the monitoring process and flush all rexi_DOWN +%% messages from our mailbox. +-spec stop(pid()) -> ok. +stop(MonitoringPid) -> + MonitoringPid ! {self(), shutdown}, + flush_down_messages(). + +%% internal functions %% + +wait_monitors(Parent) -> + receive + {'DOWN', _, process, Pid, Reason} -> + Parent ! {rexi_DOWN, self(), Pid, Reason}, + wait_monitors(Parent); + {Parent, shutdown} -> + ok + end. + +flush_down_messages() -> + receive {rexi_DOWN, _, _, _} -> + flush_down_messages() + after 0 -> + ok + end. |