Skip to content

Commit

Permalink
feat: Changed interface of spin_until_future_complete_impl
Browse files Browse the repository at this point in the history
This change allows it to use a second thread to wait for
the future to become ready.

Signed-off-by: Janosch Machowinski <[email protected]>
  • Loading branch information
Janosch Machowinski authored and Janosch Machowinski committed Apr 15, 2024
1 parent 3b8c558 commit 163f727
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
8 changes: 4 additions & 4 deletions rclcpp/include/rclcpp/executor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -355,8 +355,8 @@ class Executor
std::chrono::duration<TimeRepT, TimeT> timeout = std::chrono::duration<TimeRepT, TimeT>(-1))
{
return spin_until_future_complete_impl(std::chrono::duration_cast<std::chrono::nanoseconds>(
timeout), [&future] () {
return future.wait_for(std::chrono::seconds(0));
timeout), [&future] (std::chrono::nanoseconds wait_time) {
return future.wait_for(wait_time);
}
);
}
Expand Down Expand Up @@ -406,17 +406,17 @@ class Executor
/// Spin (blocking) until the get_future_status returns ready, max_duration is reached,
/// or rclcpp is interrupted.
/**
* \param[in] get_future_status A function returning the status of a future that is been waited for.
* \param[in] max_duration Optional duration parameter, which gets passed to Executor::spin_node_once.
* `-1` is block forever, `0` is non-blocking.
* If the time spent inside the blocking loop exceeds this timeout, return a TIMEOUT return
* code.
* \param[in] wait_for_future A function waiting for a future to become ready.
* \return The return code, one of `SUCCESS`, `INTERRUPTED`, or `TIMEOUT`.
*/
RCLCPP_PUBLIC
virtual FutureReturnCode spin_until_future_complete_impl(
std::chrono::nanoseconds max_duration,
const std::function<std::future_status ()> & get_future_status);
const std::function<std::future_status(std::chrono::nanoseconds wait_time)> & wait_for_future);

/// Collect work and execute available work, optionally within a duration.
/**
Expand Down
6 changes: 3 additions & 3 deletions rclcpp/src/rclcpp/executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -272,14 +272,14 @@ Executor::spin_node_once_nanoseconds(

rclcpp::FutureReturnCode Executor::spin_until_future_complete_impl(
std::chrono::nanoseconds timeout,
const std::function<std::future_status ()> & get_future_status)
const std::function<std::future_status(std::chrono::nanoseconds wait_time)> & get_future_status)
{
// TODO(wjwwood): does not work recursively; can't call spin_node_until_future_complete
// inside a callback executed by an executor.

// Check the future before entering the while loop.
// If the future is already complete, don't try to spin.
std::future_status status = get_future_status();
std::future_status status = get_future_status(std::chrono::seconds(0));
if (status == std::future_status::ready) {
return FutureReturnCode::SUCCESS;
}
Expand All @@ -301,7 +301,7 @@ rclcpp::FutureReturnCode Executor::spin_until_future_complete_impl(
spin_once_impl(timeout_left);

// Check if the future is set, return SUCCESS if it is.
status = get_future_status();
status = get_future_status(std::chrono::seconds(0));
if (status == std::future_status::ready) {
return FutureReturnCode::SUCCESS;
}
Expand Down

0 comments on commit 163f727

Please sign in to comment.