pub struct GuardCondition { /* private fields */ }
Expand description
A waitable entity used for waking up a wait set manually.
If a wait set that is currently waiting on events should be interrupted from a separate thread, this can be done
by adding an Arc<GuardCondition>
to the wait set, and calling trigger()
on the same GuardCondition
while
the wait set is waiting.
The guard condition may be reused multiple times, but like other waitable entities, can not be used in multiple wait sets concurrently.
§Example
let context = Context::default();
let atomic_bool = Arc::new(std::sync::atomic::AtomicBool::new(false));
let atomic_bool_for_closure = Arc::clone(&atomic_bool);
let gc = Arc::new(GuardCondition::new_with_callback(
&context,
move || {
atomic_bool_for_closure.store(true, Ordering::Relaxed);
},
));
let mut ws = WaitSet::new(0, 1, 0, 0, 0, 0, &context)?;
ws.add_guard_condition(Arc::clone(&gc))?;
// Trigger the guard condition, firing the callback and waking the wait set being waited on, if any.
gc.trigger()?;
// The provided callback has now been called.
assert_eq!(atomic_bool.load(Ordering::Relaxed), true);
// The wait call will now immediately return.
ws.wait(Some(std::time::Duration::from_millis(10)))?;
Implementations§
Source§impl GuardCondition
impl GuardCondition
Sourcepub fn new_with_callback<F>(context: &Context, callback: F) -> Self
pub fn new_with_callback<F>(context: &Context, callback: F) -> Self
Creates a new guard condition with a callback.
Sourcepub fn trigger(&self) -> Result<(), RclrsError>
pub fn trigger(&self) -> Result<(), RclrsError>
Triggers this guard condition, activating the wait set, and calling the optionally assigned callback.
Trait Implementations§
Source§impl Drop for GuardCondition
impl Drop for GuardCondition
Source§impl PartialEq for GuardCondition
impl PartialEq for GuardCondition
impl Eq for GuardCondition
Auto Trait Implementations§
impl !Freeze for GuardCondition
impl !RefUnwindSafe for GuardCondition
impl Send for GuardCondition
impl Sync for GuardCondition
impl Unpin for GuardCondition
impl !UnwindSafe for GuardCondition
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more