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_server.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_server.erl')
-rw-r--r-- | apps/rexi/src/rexi_server.erl | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/apps/rexi/src/rexi_server.erl b/apps/rexi/src/rexi_server.erl new file mode 100644 index 00000000..c4dc740d --- /dev/null +++ b/apps/rexi/src/rexi_server.erl @@ -0,0 +1,86 @@ +-module(rexi_server). +-behaviour(gen_server). +-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, + code_change/3]). + +-export([start_link/0, init_p/2]). + +-include_lib("eunit/include/eunit.hrl"). + +-record(st, { + workers = ets:new(workers, [private, {keypos,2}]) +}). + +start_link() -> + gen_server:start_link({local, ?MODULE}, ?MODULE, [], []). + +init([]) -> + {ok, #st{}}. + +handle_call(_Request, _From, St) -> + {reply, ignored, St}. + +handle_cast({doit, From, MFA}, #st{workers=Workers} = St) -> + {LocalPid, Ref} = spawn_monitor(?MODULE, init_p, [From, MFA]), + {noreply, St#st{workers = add_worker({LocalPid, Ref, From}, Workers)}}; + +handle_cast({kill, FromRef}, #st{workers=Workers} = St) -> + case find_worker_from(FromRef, Workers) of + {Pid, KeyRef, {_, FromRef}} -> + erlang:demonitor(KeyRef), + exit(Pid, kill), + {noreply, St#st{workers = remove_worker(KeyRef, Workers)}}; + false -> + {noreply, St} + end. + +handle_info({'DOWN', Ref, process, _, normal}, #st{workers=Workers} = St) -> + {noreply, St#st{workers = remove_worker(Ref, Workers)}}; + +handle_info({'DOWN', Ref, process, Pid, Reason}, #st{workers=Workers} = St) -> + case find_worker(Ref, Workers) of + {Pid, Ref, From} -> + notify_caller(From, Reason), + {noreply, St#st{workers = remove_worker(Ref, Workers)}}; + false -> + {noreply, St} + end; + +handle_info(_Info, St) -> + {noreply, St}. + +terminate(_Reason, St) -> + ets:foldl(fun({Pid, _, _}, _) -> exit(Pid,kill) end, nil, St#st.workers), + ok. + +code_change(_OldVsn, St, _Extra) -> + {ok, St}. + +%% @doc initializes a process started by rexi_server. +-spec init_p({pid(), reference()}, {atom(), atom(), list()}) -> any(). +init_p(From, {M,F,A}) -> + put(rexi_from, From), + put(initial_call, {M,F,length(A)}), + try apply(M, F, A) catch _:Reason -> exit(Reason) end. + +%% internal + +add_worker(Worker, Tab) -> + ets:insert(Tab, Worker), Tab. + +remove_worker(Ref, Tab) -> + ets:delete(Tab, Ref), Tab. + +find_worker(Ref, Tab) -> + case ets:lookup(Tab, Ref) of [] -> false; [Worker] -> Worker end. + +find_worker_from(Ref, Tab) -> + case ets:match_object(Tab, {'_', '_', {'_', Ref}}) of + [] -> + false; + [Worker] -> + Worker + end. + +notify_caller({Caller, Ref}, Reason) -> + Caller ! {Ref, {rexi_EXIT, Reason}}. |