Skip to content

Commit d8bb43d

Browse files
authored
Merge pull request #4412 from esl/mongoose_mam_id
Mongoose mam id rework
2 parents 25fe539 + 761b6ee commit d8bb43d

File tree

4 files changed

+26
-77
lines changed

4 files changed

+26
-77
lines changed

c_src/mongoose_mam_id.cpp

Lines changed: 0 additions & 48 deletions
This file was deleted.

rebar.config

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,6 @@
3737
{httpd_util, integer_to_hexlist, 1}
3838
]}.
3939

40-
{port_specs,
41-
[
42-
{".*", "priv/lib/mongoose_mam_id.so", ["c_src/mongoose_mam_id.cpp"], [{env, [{"CXXFLAGS", "$CXXFLAGS -O3 -std=c++11"}]}]}
43-
]}.
44-
4540
{require_min_otp_vsn, "21"}.
4641

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

197192
{plugins,
198193
[
199-
{pc, "1.15.0"},
200194
{provider_asn1, "0.3.0"},
201195
{rebar3_codecov, "0.7.0"},
202196
{rebar3_lint, "2.0.1"}
203197
]}.
204198

205199
{provider_hooks,
206-
[{pre, [{compile, {asn, compile}}, {compile, {pc, compile}}]},
207-
{post, [{clean, {asn, clean}}, {clean, {pc, clean}}]
200+
[{pre, [{compile, {asn, compile}}]},
201+
{post, [{clean, {asn, clean}}]
208202
}]}.
209203

210204
{overrides,

src/ejabberd.erl

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@
2525

2626
-module(ejabberd).
2727
-author('alexey@process-one.net').
28-
-export([get_pid_file/0,
29-
get_status_file/0,
30-
get_so_path/0]).
28+
-export([get_pid_file/0, get_status_file/0]).
3129

3230
-type lang() :: binary().
3331

@@ -41,20 +39,6 @@
4139

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

44-
-spec get_so_path() -> binary() | string().
45-
get_so_path() ->
46-
case os:getenv("EJABBERD_SO_PATH") of
47-
false ->
48-
case code:priv_dir(mongooseim) of
49-
{error, _} ->
50-
".";
51-
Path ->
52-
filename:join([Path, "lib"])
53-
end;
54-
Path ->
55-
Path
56-
end.
57-
5842
-spec get_pid_file() -> 'false' | nonempty_string().
5943
get_pid_file() ->
6044
case os:getenv("EJABBERD_PID_PATH") of

src/mam/mongoose_mam_id.erl

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,28 @@
2020
-export([next_unique/1]).
2121
-on_load(load/0).
2222

23+
-spec load() -> ok.
2324
load() ->
24-
Path = filename:join(ejabberd:get_so_path(), ?MODULE_STRING),
25-
erlang:load_nif(Path, 0).
25+
case persistent_term:get(?MODULE, undefined) of
26+
undefined ->
27+
Atomic = atomics:new(1, [{signed, false}]),
28+
persistent_term:put(?MODULE, Atomic);
29+
_ ->
30+
ok
31+
end.
2632

27-
next_unique(_Candidate) ->
28-
erlang:nif_error(not_loaded).
33+
-spec next_unique(integer()) -> integer().
34+
next_unique(Candidate) when is_integer(Candidate), 0 < Candidate ->
35+
Atomic = persistent_term:get(?MODULE),
36+
next_unique(Candidate, Candidate - 1, Atomic).
37+
38+
-spec next_unique(integer(), integer(), atomics:atomics_ref()) -> integer().
39+
next_unique(Candidate, Current, Atomic) ->
40+
case atomics:compare_exchange(Atomic, 1, Current, Candidate) of
41+
Int when is_integer(Int), Candidate =< Int ->
42+
next_unique(Int + 1, Int, Atomic);
43+
Int when is_integer(Int) ->
44+
next_unique(Candidate, Int, Atomic);
45+
ok ->
46+
Candidate
47+
end.

0 commit comments

Comments
 (0)