syn.erl 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. %% ==========================================================================================================
  2. %% Syn - A global process registry.
  3. %%
  4. %% Copyright (C) 2015, Roberto Ostinelli <roberto@ostinelli.net>.
  5. %% All rights reserved.
  6. %%
  7. %% The MIT License (MIT)
  8. %%
  9. %% Copyright (c) 2015 Roberto Ostinelli
  10. %%
  11. %% Permission is hereby granted, free of charge, to any person obtaining a copy
  12. %% of this software and associated documentation files (the "Software"), to deal
  13. %% in the Software without restriction, including without limitation the rights
  14. %% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15. %% copies of the Software, and to permit persons to whom the Software is
  16. %% furnished to do so, subject to the following conditions:
  17. %%
  18. %% The above copyright notice and this permission notice shall be included in
  19. %% all copies or substantial portions of the Software.
  20. %%
  21. %% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. %% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. %% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24. %% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. %% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  26. %% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  27. %% THE SOFTWARE.
  28. %% ==========================================================================================================
  29. -module(syn).
  30. %% API
  31. -export([start/0, stop/0]).
  32. -export([register/2, unregister/1]).
  33. -export([find_by_key/1, find_by_pid/1]).
  34. -export([options/1]).
  35. -export([count/0, count/1]).
  36. %% ===================================================================
  37. %% API
  38. %% ===================================================================
  39. -spec start() -> ok.
  40. start() ->
  41. {ok, _} = application:ensure_all_started(mnesia),
  42. {ok, _} = application:ensure_all_started(syn),
  43. ok.
  44. -spec stop() -> ok.
  45. stop() ->
  46. ok = application:stop(syn).
  47. -spec register(Key :: any(), Pid :: pid()) -> ok | {error, taken}.
  48. register(Key, Pid) ->
  49. syn_backbone:register(Key, Pid).
  50. -spec unregister(Key :: any()) -> ok | {error, undefined}.
  51. unregister(Key) ->
  52. syn_backbone:unregister(Key).
  53. -spec find_by_key(Key :: any()) -> pid() | undefined.
  54. find_by_key(Key) ->
  55. syn_backbone:find_by_key(Key).
  56. -spec find_by_pid(Pid :: pid()) -> Key :: any() | undefined.
  57. find_by_pid(Pid) ->
  58. syn_backbone:find_by_pid(Pid).
  59. -spec options(list()) -> ok.
  60. options(Options) ->
  61. %% send message
  62. case proplists:get_value(netsplit_conflicting_mode, Options) of
  63. undefined ->
  64. ok;
  65. kill ->
  66. syn_netsplits:conflicting_mode(kill);
  67. {send_message, Message} ->
  68. syn_netsplits:conflicting_mode({send_message, Message})
  69. end.
  70. -spec count() -> non_neg_integer().
  71. count() ->
  72. syn_backbone:count().
  73. -spec count(Node :: atom()) -> non_neg_integer().
  74. count(Node) ->
  75. syn_backbone:count(Node).