|
| 1 | +% |
| 2 | +% This file is part of AtomVM. |
| 3 | +% |
| 4 | +% Copyright 2025 Paul Guyot <[email protected]> |
| 5 | +% |
| 6 | +% Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +% you may not use this file except in compliance with the License. |
| 8 | +% You may obtain a copy of the License at |
| 9 | +% |
| 10 | +% http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +% |
| 12 | +% Unless required by applicable law or agreed to in writing, software |
| 13 | +% distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +% See the License for the specific language governing permissions and |
| 16 | +% limitations under the License. |
| 17 | +% |
| 18 | +% SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later |
| 19 | +% |
| 20 | + |
| 21 | +%%----------------------------------------------------------------------------- |
| 22 | +%% This module implements common code in gen_* modules, following what |
| 23 | +%% Erlang/OTP does with gen module. However, none of the functions exported |
| 24 | +%% here are public interface. |
| 25 | +%%----------------------------------------------------------------------------- |
| 26 | + |
| 27 | +-module(gen). |
| 28 | +-moduledoc false. |
| 29 | + |
| 30 | +-export([ |
| 31 | + call/4, |
| 32 | + cast/2, |
| 33 | + reply/2 |
| 34 | +]). |
| 35 | + |
| 36 | +-type server_ref() :: atom() | pid(). |
| 37 | +-type from() :: {pid(), reference()}. |
| 38 | + |
| 39 | +%% @private |
| 40 | +-spec call(ServerRef :: server_ref(), Label :: atom(), Request :: term(), Timeout :: timeout()) -> |
| 41 | + Reply :: term() | {error, Reason :: term()}. |
| 42 | +call(ServerRef, Label, Request, Timeout) -> |
| 43 | + MonitorRef = monitor(process, ServerRef), |
| 44 | + ok = |
| 45 | + try |
| 46 | + ServerRef ! {Label, {self(), MonitorRef}, Request}, |
| 47 | + ok |
| 48 | + catch |
| 49 | + error:badarg -> |
| 50 | + % Process no longer exists, monitor will send a message |
| 51 | + ok |
| 52 | + end, |
| 53 | + receive |
| 54 | + {'DOWN', MonitorRef, process, _, {E, []} = _Reason} -> |
| 55 | + exit(E); |
| 56 | + {'DOWN', MonitorRef, process, _, {_E, _L} = Reason} -> |
| 57 | + exit(Reason); |
| 58 | + {'DOWN', MonitorRef, process, _, Atom} when is_atom(Atom) -> |
| 59 | + exit(Atom); |
| 60 | + {MonitorRef, Reply} -> |
| 61 | + demonitor(MonitorRef, [flush]), |
| 62 | + Reply |
| 63 | + after Timeout -> |
| 64 | + % If Timeout is small enough (0), the error message might be timeout |
| 65 | + % instead of noproc as there could be a race condition with the monitor. |
| 66 | + demonitor(MonitorRef, [flush]), |
| 67 | + exit(timeout) |
| 68 | + end. |
| 69 | + |
| 70 | +%% @private |
| 71 | +-spec cast(ServerRef :: server_ref(), Message :: any()) -> ok. |
| 72 | +cast(ServerRef, Message) -> |
| 73 | + try |
| 74 | + ServerRef ! {'$gen_cast', Message}, |
| 75 | + ok |
| 76 | + catch |
| 77 | + error:_ -> |
| 78 | + % Process does not exist, ignore error |
| 79 | + ok |
| 80 | + end. |
| 81 | + |
| 82 | +%% @private |
| 83 | +-spec reply(From :: from(), Reply :: any()) -> ok. |
| 84 | +reply({Pid, Ref}, Reply) -> |
| 85 | + try |
| 86 | + Pid ! {Ref, Reply}, |
| 87 | + ok |
| 88 | + catch |
| 89 | + _:_ -> |
| 90 | + ok |
| 91 | + end. |
0 commit comments