Skip to content

Commit

Permalink
Rename functions to acquire/release (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
saleyn authored Sep 18, 2023
1 parent 4248469 commit 32f56fa
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 60 deletions.
18 changes: 9 additions & 9 deletions c_src/sema_nif.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);
46 changes: 23 additions & 23 deletions src/sema_nif.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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).

Expand All @@ -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() ->
Expand Down
48 changes: 24 additions & 24 deletions test/sema_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -8,61 +8,61 @@ 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() ->
receive
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.
Expand All @@ -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
Expand Down Expand Up @@ -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} ->
Expand Down Expand Up @@ -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
Expand All @@ -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.

Expand Down
8 changes: 4 additions & 4 deletions test/swarm_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 32f56fa

Please sign in to comment.