You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

75 rivejä
2.2 KiB

5 vuotta sitten
5 vuotta sitten
5 vuotta sitten
5 vuotta sitten
5 vuotta sitten
5 vuotta sitten
5 vuotta sitten
5 vuotta sitten
5 vuotta sitten
  1. -module(agAgencyPoolMgrExm).
  2. -export([
  3. start_link/3
  4. , init_it/3
  5. , system_code_change/4
  6. , system_continue/3
  7. , system_get_state/1
  8. , system_terminate/4
  9. ]).
  10. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% genExm start %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  11. -spec start_link(module(), term(), [proc_lib:spawn_option()]) -> {ok, pid()}.
  12. start_link(Name, Args, SpawnOpts) ->
  13. proc_lib:start_link(?MODULE, init_it, [Name, self(), Args], infinity, SpawnOpts).
  14. init_it(Name, Parent, Args) ->
  15. case safeRegister(Name) of
  16. true ->
  17. process_flag(trap_exit, true),
  18. moduleInit(Parent, Args);
  19. {false, Pid} ->
  20. proc_lib:init_ack(Parent, {error, {already_started, Pid}})
  21. end.
  22. -spec system_code_change(term(), module(), undefined | term(), term()) -> {ok, term()}.
  23. system_code_change(State, _Module, _OldVsn, _Extra) ->
  24. {ok, State}.
  25. -spec system_continue(pid(), [], {module(), atom(), pid(), term()}) -> ok.
  26. system_continue(_Parent, _Debug, {Parent, State}) ->
  27. loop(Parent, State).
  28. -spec system_get_state(term()) -> {ok, term()}.
  29. system_get_state(State) ->
  30. {ok, State}.
  31. -spec system_terminate(term(), pid(), [], term()) -> none().
  32. system_terminate(Reason, _Parent, _Debug, _State) ->
  33. exit(Reason).
  34. safeRegister(Name) ->
  35. try register(Name, self()) of
  36. true -> true
  37. catch
  38. _:_ -> {false, whereis(Name)}
  39. end.
  40. moduleInit(Parent, Args) ->
  41. case agAgencyPoolMgrIns:init(Args) of
  42. {ok, State} ->
  43. proc_lib:init_ack(Parent, {ok, self()}),
  44. loop(Parent, State);
  45. {stop, Reason} ->
  46. proc_lib:init_ack(Parent, {error, Reason}),
  47. exit(Reason)
  48. end.
  49. loop(Parent, State) ->
  50. receive
  51. {system, From, Request} ->
  52. sys:handle_system_msg(Request, From, Parent, ?MODULE, [], {Parent, State});
  53. {'EXIT', Parent, Reason} ->
  54. terminate(Reason, State);
  55. Msg ->
  56. {ok, NewState} = agAgencyPoolMgrIns:handleMsg(Msg, State),
  57. loop(Parent, NewState)
  58. end.
  59. terminate(Reason, State) ->
  60. agAgencyPoolMgrIns:terminate(Reason, State),
  61. exit(Reason).
  62. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% genExm end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%