diff --git a/c_src/sema_nif.cpp b/c_src/sema_nif.cpp index b5d0c46..786d0f3 100644 --- a/c_src/sema_nif.cpp +++ b/c_src/sema_nif.cpp @@ -195,7 +195,7 @@ struct sema { pids.erase(pid); } - ERL_NIF_TERM vacate(ErlNifEnv *env, const ErlNifPid& pid, unsigned n) { + ERL_NIF_TERM release(ErlNifEnv *env, const ErlNifPid& pid, unsigned n) { if (del_pid(env, pid, n)) { // process found, decrement and return new count return unsigned_result(env, @@ -286,7 +286,7 @@ info(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) { } static ERL_NIF_TERM -occupy(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) { +acquire(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) { if (argc < 1 || argc > 2) return enif_make_badarg(env); @@ -316,7 +316,7 @@ occupy(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) { } static ERL_NIF_TERM -vacate(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) { +release(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) { if (argc < 1 || argc > 3) return enif_make_badarg(env); @@ -353,17 +353,17 @@ vacate(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) { return enif_make_badarg(env); } - return res->vacate(env, pid, n); + return res->release(env, pid, n); } static ErlNifFunc nif_funcs[] = { {"create", 1, create}, {"info", 1, info}, - {"occupy", 1, occupy}, - {"occupy", 2, occupy}, - {"vacate", 1, vacate}, - {"vacate", 2, vacate}, - {"vacate", 3, vacate} + {"acquire", 1, acquire}, + {"acquire", 2, acquire}, + {"release", 1, release}, + {"release", 2, release}, + {"release", 3, release} }; ERL_NIF_INIT(sema_nif, nif_funcs, &load, nullptr, nullptr, nullptr); diff --git a/src/sema_nif.erl b/src/sema_nif.erl index 7b80da8..65c6f09 100644 --- a/src/sema_nif.erl +++ b/src/sema_nif.erl @@ -3,24 +3,24 @@ -export([ create/1, info/1, - occupy/1, - occupy/2, - vacate/1, - vacate/2, - vacate/3 + acquire/1, + acquire/2, + release/1, + release/2, + release/3 ]). -nifs([ create/1, info/1, - occupy/1, - occupy/2, - vacate/1, - vacate/2, - vacate/3 + acquire/1, + acquire/2, + release/1, + release/2, + release/3 ]). --export_type([sema_ref/0, occupy_ret/0, vacate_ret/0]). +-export_type([sema_ref/0, acquire_ret/0, release_ret/0]). -on_load(init/0). @@ -44,37 +44,37 @@ create(_) -> not_loaded(?LINE). }. info(_) -> not_loaded(?LINE). --type occupy_ret() :: +-type acquire_ret() :: % process got resource unit, return number of units acquired so far {ok, N :: pos_integer()} % no resource units available | {error, backlog_full}. % acquire resource unit for calling process, monitor process --spec occupy(Semaphore :: sema_ref()) -> Ret :: occupy_ret(). -occupy(_) -> not_loaded(?LINE). +-spec acquire(Semaphore :: sema_ref()) -> Ret :: acquire_ret(). +acquire(_) -> not_loaded(?LINE). % acquire resource Cnt units for calling process, monitor process --spec occupy(Semaphore :: sema_ref(), Cnt :: pos_integer()) -> Ret :: occupy_ret(). -occupy(_, _) -> not_loaded(?LINE). +-spec acquire(Semaphore :: sema_ref(), Cnt :: pos_integer()) -> Ret :: acquire_ret(). +acquire(_, _) -> not_loaded(?LINE). --type vacate_ret() :: +-type release_ret() :: % process freed resource unit, return number of units acquired after that {ok, N :: pos_integer()} % no process that holds resource found | {error, not_found}. % release resource unit acquired by calling process --spec vacate(Semaphore :: sema_ref()) -> Ret :: vacate_ret(). -vacate(_) -> not_loaded(?LINE). +-spec release(Semaphore :: sema_ref()) -> Ret :: release_ret(). +release(_) -> not_loaded(?LINE). % release resource unit(s) acquired by calling/another process --spec vacate(Semaphore :: sema_ref(), Arg :: pos_integer() | pid()) -> Ret :: vacate_ret(). -vacate(_, _) -> not_loaded(?LINE). +-spec release(Semaphore :: sema_ref(), Arg :: pos_integer() | pid()) -> Ret :: release_ret(). +release(_, _) -> not_loaded(?LINE). % release resource units acquired by another process --spec vacate(Semaphore :: sema_ref(), Cnt :: pos_integer(), Pid :: pid()) -> Ret :: vacate_ret(). -vacate(_, _, _) -> not_loaded(?LINE). +-spec release(Semaphore :: sema_ref(), Cnt :: pos_integer(), Pid :: pid()) -> Ret :: release_ret(). +release(_, _, _) -> not_loaded(?LINE). % internal init() -> diff --git a/test/sema_tests.erl b/test/sema_tests.erl index 857aca9..601f76d 100644 --- a/test/sema_tests.erl +++ b/test/sema_tests.erl @@ -8,19 +8,19 @@ basic_api_test() -> S = sema_nif:create(3), ?assertEqual(#{cnt => 0, dead => 0, max => 3}, sema_nif:info(S)), - ?assertEqual({ok, 1}, sema_nif:occupy(S)), + ?assertEqual({ok, 1}, sema_nif:acquire(S)), ?assertEqual(#{cnt => 1, dead => 0, max => 3}, sema_nif:info(S)), - ?assertEqual({ok, 2}, sema_nif:occupy(S)), + ?assertEqual({ok, 2}, sema_nif:acquire(S)), ?assertEqual(#{cnt => 2, dead => 0, max => 3}, sema_nif:info(S)), - ?assertEqual({ok, 3}, sema_nif:occupy(S)), + ?assertEqual({ok, 3}, sema_nif:acquire(S)), ?assertEqual(#{cnt => 3, dead => 0, max => 3}, sema_nif:info(S)), - ?assertEqual({error, backlog_full}, sema_nif:occupy(S)), + ?assertEqual({error, backlog_full}, sema_nif:acquire(S)), ?assertEqual(#{cnt => 3, dead => 0, max => 3}, sema_nif:info(S)), - ?assertEqual({ok, 2}, sema_nif:vacate(S)), + ?assertEqual({ok, 2}, sema_nif:release(S)), ?assertEqual(#{cnt => 2, dead => 0, max => 3}, sema_nif:info(S)), Pid = spawn(fun() -> @@ -28,41 +28,41 @@ basic_api_test() -> stop -> ok end end), - ?assertEqual({error, not_found}, sema_nif:vacate(S, Pid)), + ?assertEqual({error, not_found}, sema_nif:release(S, Pid)), ?assertEqual(#{cnt => 2, dead => 0, max => 3}, sema_nif:info(S)), Pid ! stop, - ?assertEqual({ok, 1}, sema_nif:vacate(S)), + ?assertEqual({ok, 1}, sema_nif:release(S)), ?assertEqual(#{cnt => 1, dead => 0, max => 3}, sema_nif:info(S)), - ?assertEqual({ok, 0}, sema_nif:vacate(S)), + ?assertEqual({ok, 0}, sema_nif:release(S)), ?assertEqual(#{cnt => 0, dead => 0, max => 3}, sema_nif:info(S)), - ?assertEqual({error, not_found}, sema_nif:vacate(S)), + ?assertEqual({error, not_found}, sema_nif:release(S)), ?assertEqual(#{cnt => 0, dead => 0, max => 3}, sema_nif:info(S)), - ?assertEqual({ok, 1}, sema_nif:occupy(S)), + ?assertEqual({ok, 1}, sema_nif:acquire(S)), ?assertEqual(#{cnt => 1, dead => 0, max => 3}, sema_nif:info(S)), - ?assertEqual({ok, 0}, sema_nif:vacate(S, self())), + ?assertEqual({ok, 0}, sema_nif:release(S, self())), ?assertEqual(#{cnt => 0, dead => 0, max => 3}, sema_nif:info(S)), - ?assertEqual({ok, 3}, sema_nif:occupy(S, 3)), + ?assertEqual({ok, 3}, sema_nif:acquire(S, 3)), ?assertEqual(#{cnt => 3, dead => 0, max => 3}, sema_nif:info(S)), - ?assertEqual({ok, 2}, sema_nif:vacate(S, self())), + ?assertEqual({ok, 2}, sema_nif:release(S, self())), ?assertEqual(#{cnt => 2, dead => 0, max => 3}, sema_nif:info(S)), - ?assertEqual({ok, 0}, sema_nif:vacate(S, 2, self())), + ?assertEqual({ok, 0}, sema_nif:release(S, 2, self())), ?assertEqual(#{cnt => 0, dead => 0, max => 3}, sema_nif:info(S)), - ?assertEqual({ok, 3}, sema_nif:occupy(S, 3)), + ?assertEqual({ok, 3}, sema_nif:acquire(S, 3)), ?assertEqual(#{cnt => 3, dead => 0, max => 3}, sema_nif:info(S)), - ?assertEqual({ok, 2}, sema_nif:vacate(S)), + ?assertEqual({ok, 2}, sema_nif:release(S)), ?assertEqual(#{cnt => 2, dead => 0, max => 3}, sema_nif:info(S)), - ?assertEqual({ok, 0}, sema_nif:vacate(S, 2)), + ?assertEqual({ok, 0}, sema_nif:release(S, 2)), ?assertEqual(#{cnt => 0, dead => 0, max => 3}, sema_nif:info(S)), ok. @@ -73,8 +73,8 @@ gc_test() -> Self = self(), {Pid, Mref} = spawn_monitor(fun() -> - ?assertEqual({ok, 1}, sema_nif:occupy(S)), - ?assertEqual({ok, 3}, sema_nif:occupy(S, 2)), + ?assertEqual({ok, 1}, sema_nif:acquire(S)), + ?assertEqual({ok, 3}, sema_nif:acquire(S, 2)), Self ! ready, receive stop -> ok @@ -110,12 +110,12 @@ test_parallel(BacklogSize, ProcessCount) -> Top = self(), F = fun() -> Pid = self(), - case sema_nif:occupy(S) of + case sema_nif:acquire(S) of {ok, N} when is_integer(N), N > 0, N =< BacklogSize -> Top ! {tenant, Pid, N}, receive leave -> - {ok, Left} = sema_nif:vacate(S), + {ok, Left} = sema_nif:release(S), Top ! {left, Pid, Left} end; {error, backlog_full} -> @@ -182,14 +182,14 @@ test_sema_race() -> sema_ops(N) -> S = sema_nif:create(2), {Pid, Mref} = spawn_monitor(fun() -> - sema_nif:occupy(S), + sema_nif:acquire(S), receive x -> x end end), spin(N), exit(Pid, kill), - case sema_nif:occupy(S) of + case sema_nif:acquire(S) of % early kill {ok, 1} -> % occupation by Pid is not done, increase kill delay @@ -203,7 +203,7 @@ sema_ops(N) -> % ensure nif handled monitor ok = wait_dead(1_000_000, S), % ensure only one unit occupied now - {ok, 0} = sema_nif:vacate(S), + {ok, 0} = sema_nif:release(S), true end. diff --git a/test/swarm_tests.erl b/test/swarm_tests.erl index 7e63081..de2816a 100644 --- a/test/swarm_tests.erl +++ b/test/swarm_tests.erl @@ -12,7 +12,7 @@ basic_swarm() -> Self = self(), Pids = [ spawn(fun() -> - ?assertMatch({ok, _}, sema_nif:occupy(S)), + ?assertMatch({ok, _}, sema_nif:acquire(S)), Self ! {pass, self(), S}, receive stop -> ok @@ -33,7 +33,7 @@ loop([Pid | T]) -> {pass, Pid, S} -> spawn(fun() -> timer:sleep(1), - ?assertMatch({ok, _}, sema_nif:vacate(S, Pid)), + ?assertMatch({ok, _}, sema_nif:release(S, Pid)), Pid ! stop end), loop(T) @@ -156,11 +156,11 @@ worker(ets, Max, _, ProcF, ReportF) -> end. nif_worker(S, ProcF, ReportF, N) -> - case sema_nif:occupy(S) of + case sema_nif:acquire(S) of {ok, _} -> ReportF(ok), Pid = self(), - ProcF(fun() -> sema_nif:vacate(S, Pid) end); + ProcF(fun() -> sema_nif:release(S, Pid) end); {error, backlog_full} when N > 1 -> erlang:yield(), nif_worker(S, ProcF, ReportF, N - 1);