Skip to content

Commit

Permalink
Update spin* to take an Arc
Browse files Browse the repository at this point in the history
  • Loading branch information
esteve committed Nov 28, 2022
1 parent 0ea4992 commit 80a437b
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 16 deletions.
6 changes: 3 additions & 3 deletions docs/writing-your-first-rclrs-node.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ Next, add a main function to launch it:
fn main() -> Result<(), rclrs::RclrsError> {
let context = rclrs::Context::new(std::env::args())?;
let republisher = RepublisherNode::new(&context)?;
rclrs::spin(&republisher.node)
rclrs::spin(republisher.node)
}
```

Expand Down Expand Up @@ -190,7 +190,7 @@ fn main() -> Result<(), rclrs::RclrsError> {
republisher.republish()?;
}
});
rclrs::spin(&republisher.node)
rclrs::spin(republisher.node)
}
```

Expand All @@ -212,7 +212,7 @@ fn main() -> Result<(), rclrs::RclrsError> {
republisher_other_thread.republish()?;
}
});
rclrs::spin(&republisher.node)
rclrs::spin(republisher.node)
}
```

Expand Down
5 changes: 3 additions & 2 deletions examples/message_demo/src/message_demo.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::convert::TryInto;
use std::env;
use std::sync::Arc;

use anyhow::{Error, Result};
use rosidl_runtime_rs::{seq, BoundedSequence, Message, Sequence};
Expand Down Expand Up @@ -159,10 +160,10 @@ fn demonstrate_pubsub() -> Result<(), Error> {
)?;
println!("Sending idiomatic message.");
idiomatic_publisher.publish(rclrs_example_msgs::msg::VariousTypes::default())?;
rclrs::spin_once(&node, None)?;
rclrs::spin_once(Arc::clone(&node), None)?;
println!("Sending RMW-native message.");
direct_publisher.publish(rclrs_example_msgs::msg::rmw::VariousTypes::default())?;
rclrs::spin_once(&node, None)?;
rclrs::spin_once(Arc::clone(&node), None)?;

Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion examples/minimal_client_service/src/minimal_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ fn main() -> Result<(), Error> {
std::thread::sleep(std::time::Duration::from_millis(500));

println!("Waiting for response");
rclrs::spin(&node).map_err(|err| err.into())
rclrs::spin(node).map_err(|err| err.into())
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ async fn main() -> Result<(), Error> {

println!("Waiting for response");

let rclrs_spin = tokio::task::spawn_blocking(move || rclrs::spin(&node));
let rclrs_spin = tokio::task::spawn_blocking(move || rclrs::spin(node));

let response = future.await?;
println!(
Expand Down
2 changes: 1 addition & 1 deletion examples/minimal_client_service/src/minimal_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ fn main() -> Result<(), Error> {
.create_service::<example_interfaces::srv::AddTwoInts, _>("add_two_ints", handle_service)?;

println!("Starting server");
rclrs::spin(&node).map_err(|err| err.into())
rclrs::spin(node).map_err(|err| err.into())
}
2 changes: 1 addition & 1 deletion examples/minimal_pub_sub/src/minimal_subscriber.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ fn main() -> Result<(), Error> {
},
)?;

rclrs::spin(&node).map_err(|err| err.into())
rclrs::spin(node).map_err(|err| err.into())
}
2 changes: 1 addition & 1 deletion examples/minimal_pub_sub/src/zero_copy_subscriber.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ fn main() -> Result<(), Error> {
},
)?;

rclrs::spin(&node).map_err(|err| err.into())
rclrs::spin(node).map_err(|err| err.into())
}
14 changes: 8 additions & 6 deletions rclrs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ pub use wait::*;
/// This can usually be ignored.
///
/// [1]: crate::RclReturnCode
pub fn spin_once(node: &Node, timeout: Option<Duration>) -> Result<(), RclrsError> {
let wait_set = WaitSet::new_for_node(node)?;
pub fn spin_once(node: Arc<Node>, timeout: Option<Duration>) -> Result<(), RclrsError> {
let wait_set = WaitSet::new_for_node(&*node)?;
let ready_entities = wait_set.wait(timeout)?;

for ready_subscription in ready_entities.subscriptions {
Expand All @@ -72,19 +72,21 @@ pub fn spin_once(node: &Node, timeout: Option<Duration>) -> Result<(), RclrsErro
/// Convenience function for calling [`spin_once`] in a loop.
///
/// This function additionally checks that the context is still valid.
pub fn spin(node: &Node) -> Result<(), RclrsError> {
pub fn spin(node: Arc<Node>) -> Result<(), RclrsError> {
// The context_is_valid functions exists only to abstract away ROS distro differences
#[cfg(ros_distro = "foxy")]
// SAFETY: No preconditions for this function.
let context_is_valid =
|| unsafe { rcl_context_is_valid(&mut *node.rcl_context_mtx.lock().unwrap()) };
#[cfg(not(ros_distro = "foxy"))]
// SAFETY: No preconditions for this function.
let context_is_valid =
|| unsafe { rcl_context_is_valid(&*node.rcl_context_mtx.lock().unwrap()) };
let context_is_valid = {
let node = Arc::clone(&node);
move || unsafe { rcl_context_is_valid(&*node.rcl_context_mtx.lock().unwrap()) }
};

while context_is_valid() {
match spin_once(node, None) {
match spin_once(Arc::clone(&node), None) {
Ok(_)
| Err(RclrsError::RclError {
code: RclReturnCode::Timeout,
Expand Down

0 comments on commit 80a437b

Please sign in to comment.