Skip to main content

HandleGuard

Struct HandleGuard 

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

Per-handle quiescing core. Lives inline inside each handle struct. try_enter returns a guard that prevents _free from completing until dropped; begin_free quiesces in-flight ops and prevents new ones.

Implementations§

Source§

impl HandleGuard

Source

pub const fn new() -> Self

Construct an empty guard. Use as a const initializer when possible.

Source

pub fn try_enter(&self) -> Option<HandleOp<'_>>

Try to enter an FFI operation against this handle.

Increments active_ops first so a concurrent begin_free is forced to observe the increment OR to set freeing first (they synchronize via SeqCst). After the increment, we re-check freeing: if free is in progress, the op cannot proceed and we decrement back out. Otherwise we return a guard whose Drop decrements.

Returns None if _free has already started — the caller must surface a typed “shutting down / freed” error code and MUST NOT touch any fields of the handle except this HandleGuard (which lives in still-valid leaked memory).

Source

pub fn begin_free(&self, deadline: Duration) -> bool

Mark the handle as freeing and wait for in-flight ops to drain. Returns true if THIS call won the race to flip freeing AND in-flight ops drained within FFI_HANDLE_FREE_DEADLINE. Returns false on timeout OR if a prior caller already flipped freeing.

Single-winner contract. Only ONE caller across the lifetime of this guard ever sees true. That winning caller is the one that owns the right to take the inner out of ManuallyDrop exactly once. Subsequent callers (whether concurrent or strictly after) see false and MUST NOT touch the inner — the winner has it (or had it, and dropped it).

This is what makes _free idempotent: a second _free call gates the ManuallyDrop::take behind this method’s true return, so it bails before the double-take that would UAF the inner allocation.

On timeout (winner observed freeing=false→true but drain didn’t complete), the caller must NOT take the inner — concurrent ops may still be holding it. Leak inner along with the box.

Future try_enter calls will see freeing=true and bail, regardless of whether the winner’s drain succeeded, timed out, or this caller is the loser. “No NEW ops will start” is set as soon as the winner flips the flag.

Trait Implementations§

Source§

impl Default for HandleGuard

Source§

fn default() -> Self

Returns the “default value” for a 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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> 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 = 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<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more