diff --git a/c_src/mongoose_mam_id.cpp b/c_src/mongoose_mam_id.cpp deleted file mode 100644 index 0a0f06e8900..00000000000 --- a/c_src/mongoose_mam_id.cpp +++ /dev/null @@ -1,48 +0,0 @@ -/** - * @file mongoose_mam_id.cpp - * @author konrad.zemek@erlang-solutions.com - * @copyright 2017 Erlang Solutions Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include - -#include -#include - -static std::atomic counter{ 0 }; - -extern "C" { -static ERL_NIF_TERM next_unique(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) -{ - ErlNifUInt64 given_candidate; - if (!enif_get_uint64(env, argv[0], &given_candidate)) - return enif_make_badarg(env); - - std::uint_fast64_t candidate = static_cast(given_candidate); - std::uint_fast64_t current = candidate - 1; - - while (!counter.compare_exchange_weak(current, candidate, std::memory_order_relaxed)) - { - if (current >= candidate) - candidate = current + 1; - } - - return enif_make_uint64(env, candidate); -} - -static ErlNifFunc nif_funcs[] = { { "next_unique", 1, next_unique } }; - -ERL_NIF_INIT(mongoose_mam_id, nif_funcs, nullptr, nullptr, nullptr, nullptr) -} diff --git a/rebar.config b/rebar.config index 1f8df9254af..36ffed1f17f 100644 --- a/rebar.config +++ b/rebar.config @@ -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. diff --git a/src/ejabberd.erl b/src/ejabberd.erl index 1401c1fe338..284943661c3 100644 --- a/src/ejabberd.erl +++ b/src/ejabberd.erl @@ -25,9 +25,7 @@ -module(ejabberd). -author('alexey@process-one.net'). --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(). @@ -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 diff --git a/src/mam/mongoose_mam_id.erl b/src/mam/mongoose_mam_id.erl index 525e304aee4..092976d920c 100644 --- a/src/mam/mongoose_mam_id.erl +++ b/src/mam/mongoose_mam_id.erl @@ -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.