Skip to content

Commit 7722761

Browse files
Split new functions into with/without callback, and rcl_context versions
1 parent 2a8999d commit 7722761

File tree

3 files changed

+57
-26
lines changed

3 files changed

+57
-26
lines changed

rclrs/src/node.rs

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ impl Node {
191191
Ok(client)
192192
}
193193

194-
/// Creates a [`GuardCondition`][1].
194+
/// Creates a [`GuardCondition`][1] with no callback.
195195
///
196196
/// A weak pointer to the `GuardCondition` is stored within this node.
197197
/// When this node is added to a wait set (e.g. when calling `spin_once`[2]
@@ -200,12 +200,33 @@ impl Node {
200200
///
201201
/// [1]: crate::GuardCondition
202202
/// [2]: crate::spin_once
203-
pub fn create_guard_condition(
204-
&mut self,
205-
context: &Context,
206-
callback: Option<Box<dyn Fn() + Send + Sync>>,
207-
) -> Arc<GuardCondition> {
208-
let guard_condition = Arc::new(GuardCondition::new(context, callback));
203+
pub fn create_guard_condition(&mut self) -> Arc<GuardCondition> {
204+
let guard_condition = Arc::new(GuardCondition::new_with_rcl_context(
205+
&mut self.rcl_context_mtx.lock().unwrap(),
206+
None::<fn()>,
207+
));
208+
self.guard_conditions
209+
.push(Arc::downgrade(&guard_condition) as Weak<GuardCondition>);
210+
guard_condition
211+
}
212+
213+
/// Creates a [`GuardCondition`][1] with a callback.
214+
///
215+
/// A weak pointer to the `GuardCondition` is stored within this node.
216+
/// When this node is added to a wait set (e.g. when calling `spin_once`[2]
217+
/// with this node as an argument), the guard condition can be used to
218+
/// interrupt the wait.
219+
///
220+
/// [1]: crate::GuardCondition
221+
/// [2]: crate::spin_once
222+
pub fn create_guard_condition_with_callback<F>(&mut self, callback: F) -> Arc<GuardCondition>
223+
where
224+
F: Fn() + Send + Sync + 'static,
225+
{
226+
let guard_condition = Arc::new(GuardCondition::new_with_rcl_context(
227+
&mut self.rcl_context_mtx.lock().unwrap(),
228+
Some(callback),
229+
));
209230
self.guard_conditions
210231
.push(Arc::downgrade(&guard_condition) as Weak<GuardCondition>);
211232
guard_condition

rclrs/src/wait.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ mod tests {
369369
fn guard_condition_in_wait_set_readies() -> Result<(), RclrsError> {
370370
let context = Context::new([])?;
371371

372-
let guard_condition = Arc::new(GuardCondition::new(&context, None));
372+
let guard_condition = Arc::new(GuardCondition::new(&context));
373373

374374
let mut wait_set = WaitSet::new(0, 1, 0, 0, 0, 0, &context)?;
375375
wait_set.add_guard_condition(Arc::clone(&guard_condition))?;

rclrs/src/wait/guard_condition.rs

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ use std::sync::{atomic::AtomicBool, Arc, Mutex};
2222
/// let atomic_bool = Arc::new(std::sync::atomic::AtomicBool::new(false));
2323
/// let atomic_bool_for_closure = Arc::clone(&atomic_bool);
2424
///
25-
/// let gc = Arc::new(GuardCondition::new(
25+
/// let gc = Arc::new(GuardCondition::new_with_callback(
2626
/// &context,
27-
/// Some(Box::new(move || {
27+
/// move || {
2828
/// atomic_bool_for_closure.store(true, Ordering::Relaxed);
29-
/// })),
29+
/// },
3030
/// ));
3131
///
3232
/// let mut ws = WaitSet::new(0, 1, 0, 0, 0, 0, &context)?;
@@ -78,8 +78,24 @@ impl Eq for GuardCondition {}
7878
unsafe impl Send for rcl_guard_condition_t {}
7979

8080
impl GuardCondition {
81-
/// Creates a new guard condition.
82-
pub fn new<F>(context: &Context, callback: Option<F>) -> Self
81+
/// Creates a new guard condition with no callback.
82+
pub fn new(context: &Context) -> Self {
83+
Self::new_with_rcl_context(&mut context.rcl_context_mtx.lock().unwrap(), None::<fn()>)
84+
}
85+
86+
/// Creates a new guard condition with a callback.
87+
pub fn new_with_callback<F>(context: &Context, callback: F) -> Self
88+
where
89+
F: Fn() + Send + Sync + 'static,
90+
{
91+
Self::new_with_rcl_context(&mut context.rcl_context_mtx.lock().unwrap(), Some(callback))
92+
}
93+
94+
/// Creates a new guard condition by providing the rcl_context_t and an optional callback.
95+
/// Note this function enables calling `Node::create_guard_condition`[1] without providing the Context separately
96+
///
97+
/// [1]: Node::create_guard_condition
98+
pub(crate) fn new_with_rcl_context<F>(context: &mut rcl_context_t, callback: Option<F>) -> Self
8399
where
84100
F: Fn() + Send + Sync + 'static,
85101
{
@@ -89,7 +105,7 @@ impl GuardCondition {
89105
// SAFETY: The context must be valid, and the guard condition must be zero-initialized
90106
rcl_guard_condition_init(
91107
&mut guard_condition,
92-
&mut *context.rcl_context_mtx.lock().unwrap(),
108+
context,
93109
rcl_guard_condition_get_default_options(),
94110
);
95111
}
@@ -127,12 +143,9 @@ mod tests {
127143
let atomic_bool = Arc::new(std::sync::atomic::AtomicBool::new(false));
128144
let atomic_bool_for_closure = Arc::clone(&atomic_bool);
129145

130-
let guard_condition = GuardCondition::new(
131-
&context,
132-
Some(Box::new(move || {
133-
atomic_bool_for_closure.store(true, Ordering::Relaxed);
134-
})),
135-
);
146+
let guard_condition = GuardCondition::new_with_callback(&context, move || {
147+
atomic_bool_for_closure.store(true, Ordering::Relaxed);
148+
});
136149

137150
guard_condition.trigger()?;
138151

@@ -148,12 +161,9 @@ mod tests {
148161
let atomic_bool = Arc::new(std::sync::atomic::AtomicBool::new(false));
149162
let atomic_bool_for_closure = Arc::clone(&atomic_bool);
150163

151-
let guard_condition = Arc::new(GuardCondition::new(
152-
&context,
153-
Some(Box::new(move || {
154-
atomic_bool_for_closure.store(true, Ordering::Relaxed);
155-
})),
156-
));
164+
let guard_condition = Arc::new(GuardCondition::new_with_callback(&context, move || {
165+
atomic_bool_for_closure.store(true, Ordering::Relaxed);
166+
}));
157167

158168
let mut wait_set = WaitSet::new(0, 1, 0, 0, 0, 0, &context)?;
159169
wait_set.add_guard_condition(Arc::clone(&guard_condition))?;

0 commit comments

Comments
 (0)