Skip to content

Commit

Permalink
Add Env::is_process_alive and LocalPid::is_alive
Browse files Browse the repository at this point in the history
  • Loading branch information
filmor committed Mar 11, 2024
1 parent f944588 commit cdc075e
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 3 deletions.
13 changes: 13 additions & 0 deletions rustler/src/types/local_pid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ impl LocalPid {
pub fn from_c_arg(erl_nif_pid: ErlNifPid) -> Self {
LocalPid { c: erl_nif_pid }
}

/// Check whether the given process is alive
pub fn is_alive(self, env: Env) -> bool {
env.is_process_alive(self)
}
}

impl<'a> Decoder<'a> for LocalPid {
Expand Down Expand Up @@ -48,4 +53,12 @@ impl<'a> Env<'a> {
c: unsafe { pid.assume_init() },
}
}

/// Checks whether the given process is alive
pub fn is_process_alive(self, pid: LocalPid) -> bool {
let res = unsafe {
rustler_sys::enif_is_process_alive(self.as_c_arg(), pid.as_c_arg())
};
res != 0
}
}
1 change: 1 addition & 0 deletions rustler_tests/lib/rustler_test.ex
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ defmodule RustlerTest do
def send_all(_, _), do: err()
def send(_, _), do: err()
def whereis_pid(_), do: err()
def is_process_alive(_), do: err()
def sublists(_), do: err()

def tuple_echo(_), do: err()
Expand Down
1 change: 1 addition & 0 deletions rustler_tests/native/rustler_test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ rustler::init!(
test_env::send_all,
test_env::send,
test_env::whereis_pid,
test_env::is_process_alive,
test_env::sublists,
test_codegen::tuple_echo,
test_codegen::record_echo,
Expand Down
10 changes: 7 additions & 3 deletions rustler_tests/native/rustler_test/src/test_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,13 @@ pub fn send<'a>(env: Env<'a>, pid: LocalPid, msg: Term<'a>) -> Atom {
}

#[rustler::nif]
pub fn whereis_pid<'a>(env: Env<'a>, term: Term<'a>) -> Term<'a> {
let result = env.whereis_pid(term);
result.encode(env)
pub fn whereis_pid<'a>(env: Env<'a>, term: Term<'a>) -> Option<LocalPid> {
env.whereis_pid(term)
}

#[rustler::nif]
pub fn is_process_alive(env: Env, pid: LocalPid) -> bool {
env.is_process_alive(pid)
}

#[rustler::nif]
Expand Down
18 changes: 18 additions & 0 deletions rustler_tests/test/env_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,24 @@ defmodule RustlerTest.EnvTest do
assert nil == RustlerTest.whereis_pid(:not_a_registered_name)
end

test "is_process_alive" do
assert true == RustlerTest.is_process_alive(self())

task =
Task.async(fn ->
receive do
:exit -> :ok
end
end)

assert true == RustlerTest.is_process_alive(task.pid)

send(task.pid, :exit)
Task.await(task)

assert false == RustlerTest.is_process_alive(task.pid)
end

test "send_error" do
task =
Task.async(fn ->
Expand Down

0 comments on commit cdc075e

Please sign in to comment.