PageRenderTime 103ms CodeModel.GetById 60ms app.highlight 5ms RepoModel.GetById 35ms app.codeStats 0ms

/lib/ce/src/ce_rec.erl

https://github.com/gebi/jungerl
Erlang | 118 lines | 37 code | 14 blank | 67 comment | 0 complexity | bbfcd0b86778c7b866bb3739d29f62ad MD5 | raw file
  1%%% BEGIN ce_rec.erl %%%
  2%%%
  3%%% ce - Miscellaneous Programming Support Libraries for Erlang/OTP
  4%%% Copyright (c)2003 Cat's Eye Technologies.  All rights reserved.
  5%%%
  6%%% Redistribution and use in source and binary forms, with or without
  7%%% modification, are permitted provided that the following conditions
  8%%% are met:
  9%%%
 10%%%   Redistributions of source code must retain the above copyright
 11%%%   notice, this list of conditions and the following disclaimer.
 12%%%
 13%%%   Redistributions in binary form must reproduce the above copyright
 14%%%   notice, this list of conditions and the following disclaimer in
 15%%%   the documentation and/or other materials provided with the
 16%%%   distribution.
 17%%%
 18%%%   Neither the name of Cat's Eye Technologies nor the names of its
 19%%%   contributors may be used to endorse or promote products derived
 20%%%   from this software without specific prior written permission.
 21%%%
 22%%% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
 23%%% CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
 24%%% INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 25%%% MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 26%%% DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
 27%%% LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
 28%%% OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 29%%% PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
 30%%% OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 31%%% ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 32%%% OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 33%%% OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 34%%% POSSIBILITY OF SUCH DAMAGE. 
 35
 36%% @doc Remote Execution Contexts (<code>rec</code>).
 37%%
 38%% <p>'Remote Execution Context' is basically a
 39%% fancy way of saying 'workaround for <code>rpc</code>'.</p>
 40%%
 41%% <p>Some operations, such as <code>file:open/2</code>, return an
 42%% object which is owned by the process that created it.  When the
 43%% parent process dies, that object is expunged.</p>
 44%%
 45%% <p><code>rpc:call/4</code> is not useful for such an operation,
 46%% because it executes the given function with a short-lived process.</p>
 47%% 
 48%% <p>A viable workaround is to instead use <code>ce_rec:start/1</code>
 49%% to create a long-lived process ('execution context') on a remote node,
 50%% <code>ce_rec:rpc/2</code> to execute such an operation in that context,
 51%% and finally <code>ce_rec:stop/1</code> when the objects owned by the
 52%% remote execution context are no longer needed.</p>
 53%%
 54%% @end
 55
 56-module(ce_rec).
 57-vsn('JUNGERL').
 58-author('catseye@catseye.mb.ca').
 59-copyright('Copyright (c)2003 Cat`s Eye Technologies. All rights reserved.').
 60
 61-export([start/1, stop/1]).
 62-export([rpc/2, rpc/3]).
 63
 64%% @spec start(node()) -> rec()
 65%%         rec() = pid()
 66%% @doc Starts an execution context on the given node.
 67
 68start(Node) ->
 69  spawn_link(Node, fun() -> execution_context() end).
 70
 71%% @spec stop(rec()) -> ok | {error, Reason}
 72%% @doc Shuts down the given execution context.
 73
 74stop(Pid) ->
 75  Pid ! {self(), stop},
 76  receive
 77    {Pid, stopped} ->
 78      ok
 79    after 10000 ->
 80      {error, timeout}
 81  end.
 82
 83%% @spec rpc(pid(), fun()) -> term()
 84%% @equiv rpc(Pid, Fun, 10000)
 85
 86rpc(Pid, Fun) ->
 87  rpc(Pid, Fun, 10000).
 88
 89%% @spec rpc(pid(), fun(), Timeout::integer()) -> term()
 90%% @doc Executes a <code>fun/0</code> in a given remote execution context.
 91%% Returns the result of the fun.  May exit with <code>timeout</code> if
 92%% the call lasts more than <code>Timeout</code> milliseconds.
 93
 94rpc(Pid, Fun, Timeout) ->
 95  Pid ! {self(), do, Fun},
 96  receive
 97    {Pid, done, Result} ->
 98      Result
 99    after Timeout ->
100      exit(timeout)
101  end.
102
103%% @spec execution_context() -> never_returns()
104%% @doc Driver loop for each remote execution context.
105
106execution_context() ->
107  receive
108    {Pid, do, Fun} ->
109      Pid ! {self(), done, Fun()},
110      execution_context();
111    {Pid, stop} ->
112      Pid ! {self(), stopped},
113      ok;
114    _ ->
115      execution_context()    
116  end.
117
118%%% END of ce_rec.erl %%%