Skip to content

Commit

Permalink
Merge pull request #4412 from esl/mongoose_mam_id
Browse files Browse the repository at this point in the history
Mongoose mam id rework
  • Loading branch information
arcusfelis authored Dec 4, 2024
2 parents 25fe539 + 761b6ee commit d8bb43d
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 77 deletions.
48 changes: 0 additions & 48 deletions c_src/mongoose_mam_id.cpp

This file was deleted.

10 changes: 2 additions & 8 deletions rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,6 @@
{httpd_util, integer_to_hexlist, 1}
]}.

{port_specs,
[
{".*", "priv/lib/mongoose_mam_id.so", ["c_src/mongoose_mam_id.cpp"], [{env, [{"CXXFLAGS", "$CXXFLAGS -O3 -std=c++11"}]}]}
]}.

{require_min_otp_vsn, "21"}.

%% We agreed to use https:// for deps because of possible firewall issues.
Expand Down Expand Up @@ -196,15 +191,14 @@

{plugins,
[
{pc, "1.15.0"},
{provider_asn1, "0.3.0"},
{rebar3_codecov, "0.7.0"},
{rebar3_lint, "2.0.1"}
]}.

{provider_hooks,
[{pre, [{compile, {asn, compile}}, {compile, {pc, compile}}]},
{post, [{clean, {asn, clean}}, {clean, {pc, clean}}]
[{pre, [{compile, {asn, compile}}]},
{post, [{clean, {asn, clean}}]
}]}.

{overrides,
Expand Down
18 changes: 1 addition & 17 deletions src/ejabberd.erl
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@

-module(ejabberd).
-author('[email protected]').
-export([get_pid_file/0,
get_status_file/0,
get_so_path/0]).
-export([get_pid_file/0, get_status_file/0]).

-type lang() :: binary().

Expand All @@ -41,20 +39,6 @@

-export_type([lang/0, xml_stream_item/0]).

-spec get_so_path() -> binary() | string().
get_so_path() ->
case os:getenv("EJABBERD_SO_PATH") of
false ->
case code:priv_dir(mongooseim) of
{error, _} ->
".";
Path ->
filename:join([Path, "lib"])
end;
Path ->
Path
end.

-spec get_pid_file() -> 'false' | nonempty_string().
get_pid_file() ->
case os:getenv("EJABBERD_PID_PATH") of
Expand Down
27 changes: 23 additions & 4 deletions src/mam/mongoose_mam_id.erl
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,28 @@
-export([next_unique/1]).
-on_load(load/0).

-spec load() -> ok.
load() ->
Path = filename:join(ejabberd:get_so_path(), ?MODULE_STRING),
erlang:load_nif(Path, 0).
case persistent_term:get(?MODULE, undefined) of
undefined ->
Atomic = atomics:new(1, [{signed, false}]),
persistent_term:put(?MODULE, Atomic);
_ ->
ok
end.

next_unique(_Candidate) ->
erlang:nif_error(not_loaded).
-spec next_unique(integer()) -> integer().
next_unique(Candidate) when is_integer(Candidate), 0 < Candidate ->
Atomic = persistent_term:get(?MODULE),
next_unique(Candidate, Candidate - 1, Atomic).

-spec next_unique(integer(), integer(), atomics:atomics_ref()) -> integer().
next_unique(Candidate, Current, Atomic) ->
case atomics:compare_exchange(Atomic, 1, Current, Candidate) of
Int when is_integer(Int), Candidate =< Int ->
next_unique(Int + 1, Int, Atomic);
Int when is_integer(Int) ->
next_unique(Candidate, Int, Atomic);
ok ->
Candidate
end.

0 comments on commit d8bb43d

Please sign in to comment.