Skip to main content

GuardCondition

Struct GuardCondition 

Source
pub struct GuardCondition<'s> { /* private fields */ }
Expand description

Handle for triggering a guard condition from outside the executor.

Obtained from Executor::register_guard_condition. Safe to use from any thread. It borrows the executor’s backing ('s, the executor’s own lifetime parameter), so it cannot outlive the memory its flag lives in; and it holds its own reference to the executor’s wake state, so a trigger after the executor is dropped wakes nothing and touches nothing freed.

A handle cannot outlive the backing its executor was built over:

ⓘ
use core::mem::MaybeUninit;
use nros_node::executor::{Executor, ExecutorConfig, ExecutorSizing};
let config = ExecutorConfig::new("");
let sizing = ExecutorSizing::DEFAULT;
let guard;
{
    let mut backing = vec![MaybeUninit::<u64>::uninit(); sizing.u64_len()];
    // SAFETY: `backing` is `sizing.u64_len()` words, used by this executor alone.
    let mut executor = unsafe { Executor::open_in(&config, &mut backing, sizing) }.unwrap();
    guard = executor.register_guard_condition(|| {}).unwrap().1;
}
guard.trigger();

The same code with the handle used while the backing lives compiles, so the refusal above is the lifetime’s and not an unrelated error:

use core::mem::MaybeUninit;
use nros_node::executor::{Executor, ExecutorConfig, ExecutorSizing};
let config = ExecutorConfig::new("");
let sizing = ExecutorSizing::DEFAULT;
{
    let mut backing = vec![MaybeUninit::<u64>::uninit(); sizing.u64_len()];
    // SAFETY: `backing` is `sizing.u64_len()` words, used by this executor alone.
    let mut executor = unsafe { Executor::open_in(&config, &mut backing, sizing) }.unwrap();
    let guard = executor.register_guard_condition(|| {}).unwrap().1;
    guard.trigger();
}

Implementations§

Source§

impl GuardCondition<'_>

Source

pub const OWNS_WAKE_STATE: bool = true

Whether a handle holds a reference to its executor’s wake state, so that dropping it releases one: true in a build with both alloc and rmw-cffi, where the handle wakes the executor, and false otherwise. A binding that drops the handle from foreign storage can state what that drop does from this, and core::mem::needs_drop agrees with it.

Source§

impl<'s> GuardCondition<'s>

Source

pub fn trigger(&self)

Trigger the guard condition.

The executor will invoke the associated callback on the next spin iteration. On std + rmw-cffi builds, trigger also signals the executor’s wake condvar so a blocked spin_once resumes immediately.

From THREAD context only. Its wake is the port’s thread signal (xSemaphoreGive on FreeRTOS, which must not run in an interrupt handler). From an interrupt handler, or a POSIX signal handler, call trigger_from_isr.

Source

pub fn trigger_from_isr(&self) -> bool

Trigger the guard condition from an interrupt handler, or on POSIX from a signal handler.

The same flag as trigger, then the interrupt-safe wake: the port’s nros_platform_wake_signal_from_isr (xSemaphoreGiveFromISR and a yield on FreeRTOS, k_sem_give on Zephyr, the semaphore put on ThreadX, sem_post and a pipe write on POSIX), and each band worker’s wake_from_isr. Bounded and non-blocking.

Returns whether the executor’s wake object took the signal. false means the flag is set but nothing was woken: a build with no wake callback, or a port with no tested interrupt-safe signal: the hosted ThreadX Linux port refuses, and so does a NuttX build without CONFIG_SCHED_LPWORK for a wake object that parks on handles (it says so once, at the first handle park). The executor then sees the trigger at its next deadline.

Source

pub fn is_triggered(&self) -> bool

Is this guard condition triggered and not yet dispatched?

The POLLING entity-API tier: a task that owns its own loop and never reaches a callback still needs to see the flag another thread or an ISR set. rcl has no such reader — a wait set is the only way to observe an rcl guard condition — so this is ours, and Rust and C++ got the reader C already had.

Bounded claim: the executor CONSUMES the flag when it dispatches (guard_try_process swaps it false and then calls the callback), so this answers “set and not yet dispatched”. A poller that also spins the executor is racing itself, and should read the flag OR take the callback, not both.

Source

pub fn clear(&self) -> bool

Clear the flag without dispatching — the other half of is_triggered, for a polling owner that has just handled the event itself.

Returns what the flag WAS, so a poller can take-and-clear in one step without a read/clear race against another thread’s trigger.

Trait Implementations§

Source§

impl Clone for GuardCondition<'_>

phase-467 Row 8 — a SECOND handle to the same arena flag.

Sound by construction: every field is Copy, the flag is a &'static reference into an arena that outlives every handle, and trigger() is a store plus a wake — there is no ownership to duplicate and nothing to free. is_triggered/clear are already documented as racing an executor that dispatches, so a second handle adds no contract.

Written rather than derived so the reason is here: the graph-change edge needs one handle in the CALLER’s rcl_guard_condition_t and one the EXECUTOR owns for the backend to fire (issue 1385’s shape). Not Copy — an implicit copy of a trigger handle reads as a value and behaves as an alias, which is exactly the confusion the 1385 fix is about.

Source§

fn clone(&self) -> GuardCondition<'_>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Send for GuardCondition<'_>

Source§

impl Sync for GuardCondition<'_>

Auto Trait Implementations§

§

impl<'s> Freeze for GuardCondition<'s>

§

impl<'s> RefUnwindSafe for GuardCondition<'s>

§

impl<'s> Unpin for GuardCondition<'s>

§

impl<'s> UnsafeUnpin for GuardCondition<'s>

§

impl<'s> UnwindSafe for GuardCondition<'s>

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. 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.