Struct shadow_rs::utility::childpid_watcher::ChildPidWatcher

source ·
pub struct ChildPidWatcher { /* private fields */ }
Expand description

Utility for monitoring a set of child pid’s, calling registered callbacks when one exits or is killed. Starts a background thread, which is shut down when the object is dropped.

Implementations§

source§

impl ChildPidWatcher

source

pub fn new() -> Self

Create a ChildPidWatcher. Spawns a background thread, which is joined when the object is dropped.

source

pub unsafe fn fork_watchable( &self, child_fn: impl FnOnce(), ) -> Result<Pid, Errno>

Fork a child and register it. Uses fork internally; it vfork is desired, use register_pid instead.

Panics if child_fn returns. TODO: change the type to FnOnce() -> ! once that’s stabilized in Rust. https://github.com/rust-lang/rust/issues/35121

§Safety

As for fork in Rust in general. Probably, mostly, safe, since the child process gets its own copy of the address space and OS resources etc. Still, there may be some dragons here. Best to call exec before too long in the child.

source

pub fn register_pid(&self, pid: Pid)

Register interest in pid.

Will succeed even if pid is already dead, in which case callbacks registered for this pid will immediately be scheduled to run.

pid must refer to some process, but that process may be a zombie (dead but not yet reaped). Panics if pid doesn’t exist at all. The caller should ensure the process has not been reaped before calling this function both to avoid such panics, and to avoid accidentally watching an unrelated process with a recycled pid.

source

pub fn unregister_pid(&self, pid: Pid)

Unregister the pid. After unregistration, no more callbacks may be registered for the given pid. Already-registered callbacks will still be called if and when the pid exits unless individually unregistered.

Safe to call multiple times.

source

pub fn register_callback( &self, pid: Pid, callback: impl Send + FnOnce(Pid) + 'static, ) -> WatchHandle

Call callback from another thread after the child pid has exited, including if it has already exited. Does not reap the child itself.

The returned handle is guaranteed to be non-zero.

Panics if pid isn’t registered.

source

pub fn unregister_callback(&self, pid: Pid, handle: WatchHandle)

Unregisters a callback. After returning, the corresponding callback is guaranteed either to have already run, or to never run. i.e. it’s safe to free data that the callback might otherwise access.

No-op if pid isn’t registered.

Trait Implementations§

source§

impl Debug for ChildPidWatcher

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for ChildPidWatcher

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl Drop for ChildPidWatcher

source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> IntoEither for T

source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

impl<T> NoTypeInference for T

source§

type This = T

source§

impl<T> Pointable for T

source§

const ALIGN: usize = _

The alignment of pointer.
source§

type Init = T

The type for initializers.
source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

source§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

source§

fn vzip(self) -> V

source§

impl<T> Host for T
where T: Debug + Send,

source§

impl<T> Host for T
where T: Debug + Send + 'static,

source§

impl<T> Host for T
where T: Host + Host,