Skip to main content

ObjectWaiter

Struct ObjectWaiter 

Source
pub struct ObjectWaiter { /* private fields */ }
Expand description

A blocking object that waits for signals on Zircon objects, and that can be interrupted.

InterruptibleEvent and PortEvent wake up a thread when another thread notifies or interrupts it. A thread that must also wake up when a Zircon object is signaled, such as a channel it reads requests from, would have to observe the object through a port, or an async executor. Instead, ObjectWaiter lets the thread wait for the objects directly with zx_object_wait_many: the waiter adds an event of its own to the objects the thread waits for, and interrupt signals that event.

To block using an ObjectWaiter, first call begin_wait. At this point, the waiter is in the “waiting” state, and future calls to interrupt will terminate the wait.

After begin_wait returns, call wait_many on the returned guard to block the current thread until one of the following conditions occur:

  1. One of the given objects asserts one of the signals that it is waited for.
  2. The given deadline expires.
  3. interrupt was called after begin_wait.

It’s safe to call interrupt at any time. However, only one thread can wait on an ObjectWaiter at a time: begin_wait cannot be called again until the guard it returns is consumed by wait_many, or dropped.

ObjectWaiter uses two-phase waiting so that clients can register the waiter with the code that interrupts them, for example in the RunState of a task, and then start blocking. This approach ensures that clients do not miss interruptions that arrive after they register the waiter but before they actually start blocking, while the interruptions of previous waits, which arrive before begin_wait, are ignored.

The event of the waiter is only signaled when a wait is actually interrupted, and only cleared after such a wait, so that a wait that is not interrupted costs no more syscall than zx_object_wait_many itself.

As with InterruptibleEvent and PortEvent, a wait that remains blocked for STACK_CLEAN_DELAY wakes up once to decommit the unused pages of the stack of the thread with starnix_stack::clean_stack, and then blocks again until the deadline.

Implementations§

Source§

impl ObjectWaiter

Source

pub fn new() -> Arc<Self> ⓘ

Source

pub fn begin_wait<'a>(self: &'a Arc<Self>) -> ObjectWaitGuard<'a>

Called to initiate a wait.

Calls to interrupt after this function returns, and before the wait ends, will cause wait_many to return ObjectWaitError::Interrupted. Calls to interrupt prior to calling begin_wait, or after the wait ended, are ignored.

Once called, this function cannot be called again until the returned guard is consumed by wait_many, or dropped. Otherwise, this function will panic.

Source

pub fn interrupt(&self)

Interrupts the wait in progress.

If no wait is in progress, that is, before begin_wait or after the wait ended, the interruption is ignored. Otherwise, wait_many returns ObjectWaitError::Interrupted, whether it is already blocked or not. Calling this function repeatedly has no effect.

Trait Implementations§

Source§

impl Debug for ObjectWaiter

Source§

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

Formats the value using the given formatter. 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
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<T, D> Encode<Ambiguous1, D> for T
where D: ResourceDialect,

§

unsafe fn encode( self, _encoder: &mut Encoder<'_, D>, _offset: usize, _depth: Depth, ) -> Result<(), Error>

Encodes the object into the encoder’s buffers. Any handles stored in the object are swapped for Handle::INVALID. Read more
§

impl<T, D> Encode<Ambiguous2, D> for T
where D: ResourceDialect,

§

unsafe fn encode( self, _encoder: &mut Encoder<'_, D>, _offset: usize, _depth: Depth, ) -> Result<(), Error>

Encodes the object into the encoder’s buffers. Any handles stored in the object are swapped for Handle::INVALID. Read more
§

impl<F, N> FidlIntoNative<Box<N>> for F
where F: FidlIntoNative<N>,

§

fn fidl_into_native(self) -> Box<N>

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
§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

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

Initializes a with the given initializer. Read more
§

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

Dereferences the given pointer. Read more
§

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

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

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

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.