Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Env::is_process_alive and LocalPid::is_alive #599

Merged
merged 1 commit into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ versions.

- Map iterators are now [DoubleEndedIterators](https://doc.rust-lang.org/std/iter/trait.DoubleEndedIterator.html)
(#598), thus allowing being iterated in reverse using `.rev()`
- `Env::is_process_alive` and `LocalPid::is_alive` (#599)

### Fixed

Expand Down
11 changes: 11 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,10 @@ 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
Loading