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<'_>
impl GuardCondition<'_>
Sourcepub const OWNS_WAKE_STATE: bool = true
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>
impl<'s> GuardCondition<'s>
Sourcepub fn trigger(&self)
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.
Sourcepub fn trigger_from_isr(&self) -> bool
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.
Sourcepub fn is_triggered(&self) -> bool
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.
Sourcepub fn clear(&self) -> bool
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.
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.