15 #include "rclcpp/clock.hpp"
17 #include <condition_variable>
20 #include "rclcpp/exceptions.hpp"
21 #include "rclcpp/utilities.hpp"
23 #include "rcpputils/scope_exit.hpp"
24 #include "rcutils/logging_macros.h"
37 exceptions::throw_from_rcl_error(ret,
"failed to initialize rcl clock");
45 RCUTILS_LOG_ERROR(
"Failed to fini rcl clock.");
51 bool stop_sleeping_ =
false;
52 bool shutdown_ =
false;
53 std::condition_variable cv_;
54 std::mutex wait_mutex_;
55 std::mutex clock_mutex_;
58 JumpHandler::JumpHandler(
59 pre_callback_t pre_callback,
60 post_callback_t post_callback,
62 : pre_callback(std::move(pre_callback)),
63 post_callback(std::move(post_callback)),
64 notice_threshold(threshold)
75 Time now(0, 0, impl_->rcl_clock_.type);
79 exceptions::throw_from_rcl_error(ret,
"could not get current time stamp");
88 const Context::SharedPtr & context)
90 if (!context || !context->is_valid()) {
91 throw std::runtime_error(
"context cannot be slept with because it's invalid");
93 const auto this_clock_type = get_clock_type();
95 throw std::runtime_error(
"until's clock type does not match this clock's type");
97 bool time_source_changed =
false;
103 std::unique_lock lock(impl_->wait_mutex_);
104 impl_->shutdown_ =
true;
106 impl_->cv_.notify_one();
109 auto callback_remover = rcpputils::scope_exit(
110 [context, &shutdown_cb_handle]() {
111 context->remove_on_shutdown_callback(shutdown_cb_handle);
117 const std::chrono::steady_clock::time_point chrono_entry = std::chrono::steady_clock::now();
118 const Duration delta_t = until - rcl_entry;
119 const std::chrono::steady_clock::time_point chrono_until =
120 chrono_entry + std::chrono::nanoseconds(delta_t.
nanoseconds());
123 std::unique_lock lock(impl_->wait_mutex_);
124 while (
now() < until && !impl_->stop_sleeping_ && !impl_->shutdown_ && context->is_valid()) {
125 impl_->cv_.wait_until(lock, chrono_until);
127 impl_->stop_sleeping_ =
false;
129 auto system_time = std::chrono::system_clock::time_point(
131 std::chrono::duration_cast<std::chrono::system_clock::duration>(
135 std::unique_lock lock(impl_->wait_mutex_);
136 while (
now() < until && !impl_->stop_sleeping_ && !impl_->shutdown_ && context->is_valid()) {
137 impl_->cv_.wait_until(lock, system_time);
139 impl_->stop_sleeping_ =
false;
153 std::lock_guard<std::mutex> lk(impl_->wait_mutex_);
154 time_source_changed = true;
156 impl_->cv_.notify_one();
161 auto system_time = std::chrono::system_clock::time_point(
163 std::chrono::duration_cast<std::chrono::system_clock::duration>(
167 std::unique_lock lock(impl_->wait_mutex_);
168 while (
now() < until && !impl_->stop_sleeping_ && !impl_->shutdown_ && context->is_valid() &&
169 !time_source_changed)
171 impl_->cv_.wait_until(lock, system_time);
173 impl_->stop_sleeping_ =
false;
178 std::unique_lock lock(impl_->wait_mutex_);
179 while (
now() < until && !impl_->stop_sleeping_ && !impl_->shutdown_ && context->is_valid() &&
180 !time_source_changed)
182 impl_->cv_.wait(lock);
184 impl_->stop_sleeping_ =
false;
188 if (!context->is_valid() || time_source_changed) {
192 return now() >= until;
205 throw std::runtime_error(
"clock is not rcl_clock_valid");
213 if (!context || !context->is_valid()) {
214 throw std::runtime_error(
"context cannot be slept with because it's invalid");
217 throw std::runtime_error(
"clock cannot be waited on as it is not rcl_clock_valid");
231 const Context::SharedPtr & context,
234 if (!context || !context->is_valid()) {
235 throw std::runtime_error(
"context cannot be slept with because it's invalid");
238 throw std::runtime_error(
"clock cannot be waited on as it is not rcl_clock_valid");
242 Time start = timeout_clock.
now();
246 while (!
started() && context->is_valid()) {
247 if (timeout < wait_tick_ns) {
250 Duration time_left = start + timeout - timeout_clock.
now();
251 if (time_left > wait_tick_ns) {
258 if (timeout_clock.
now() - start > timeout) {
270 RCUTILS_LOG_ERROR(
"ROS time not valid!");
274 bool is_enabled =
false;
277 exceptions::throw_from_rcl_error(
278 ret,
"Failed to check ros_time_override_status");
286 return &impl_->rcl_clock_;
290 Clock::get_clock_type() const noexcept
292 return impl_->rcl_clock_.type;
298 return impl_->clock_mutex_;
307 const auto * handler =
static_cast<JumpHandler *
>(user_data);
308 if (
nullptr == handler) {
311 if (before_jump && handler->pre_callback) {
312 handler->pre_callback();
313 }
else if (!before_jump && handler->post_callback) {
314 handler->post_callback(*time_jump);
318 JumpHandler::SharedPtr
320 const JumpHandler::pre_callback_t & pre_callback,
321 const JumpHandler::post_callback_t & post_callback,
325 JumpHandler::UniquePtr handler(
new JumpHandler(pre_callback, post_callback, threshold));
326 if (
nullptr == handler) {
327 throw std::bad_alloc{};
331 std::lock_guard<std::mutex> clock_guard(impl_->clock_mutex_);
334 &impl_->rcl_clock_, threshold, Clock::on_time_jump,
337 exceptions::throw_from_rcl_error(ret,
"Failed to add time jump callback");
341 std::weak_ptr<Clock::Impl> weak_impl = impl_;
344 return JumpHandler::SharedPtr(handler.release(), [weak_impl](
JumpHandler * handler) noexcept {
345 auto shared_impl = weak_impl.lock();
347 std::lock_guard<std::mutex> clock_guard(shared_impl->clock_mutex_);
348 rcl_ret_t ret = rcl_clock_remove_jump_callback(&shared_impl->rcl_clock_,
349 Clock::on_time_jump, handler);
350 if (RCL_RET_OK != ret) {
351 RCUTILS_LOG_ERROR(
"Failed to remove time jump callback");
362 std::condition_variable cv_;
364 rclcpp::Clock::SharedPtr clock_;
365 bool time_source_changed_ =
false;
369 wait_until_system_time(
370 std::unique_lock<std::mutex> & lock,
371 const rclcpp::Time & abs_time,
const std::function<
bool ()> & pred)
373 auto system_time = std::chrono::system_clock::time_point(
375 std::chrono::duration_cast<std::chrono::system_clock::duration>(
376 std::chrono::nanoseconds(abs_time.
nanoseconds())));
378 return cv_.wait_until(lock, system_time, pred);
382 wait_until_steady_time(
383 std::unique_lock<std::mutex> & lock,
384 const rclcpp::Time & abs_time,
const std::function<
bool ()> & pred)
388 const std::chrono::steady_clock::time_point chrono_entry = std::chrono::steady_clock::now();
390 const std::chrono::steady_clock::time_point chrono_until =
391 chrono_entry + std::chrono::nanoseconds(delta_t.
nanoseconds());
393 return cv_.wait_until(lock, chrono_until, pred);
399 std::unique_lock<std::mutex> & lock,
400 const rclcpp::Time & abs_time,
const std::function<
bool ()> & pred)
411 time_source_changed_ =
false;
416 std::lock_guard<std::mutex> lk(*lock.mutex());
417 time_source_changed_ =
true;
427 auto clock_handler = clock_->create_jump_callback(
429 post_time_jump_callback,
432 if (!clock_->ros_time_is_active()) {
433 auto system_time = std::chrono::system_clock::time_point(
435 std::chrono::duration_cast<std::chrono::system_clock::duration>(
436 std::chrono::nanoseconds(abs_time.
nanoseconds())));
438 return cv_.wait_until(lock, system_time, [
this, &pred] () {
439 return time_source_changed_ || pred();
446 cv_.wait(lock, [
this, &pred, &abs_time] () {
447 return clock_->now() >= abs_time || time_source_changed_ || pred();
450 return clock_->now() < abs_time;
461 std::unique_lock<std::mutex> & lock,
462 const rclcpp::Time & abs_time,
const std::function<
bool ()> & pred)
464 switch(clock_->get_clock_type()) {
466 throw std::runtime_error(
"Error, wait on uninitialized clock called");
468 return wait_until_ros_time(lock, abs_time, pred);
471 return wait_until_steady_time(lock, abs_time, pred);
474 return wait_until_system_time(lock, abs_time, pred);
488 ClockWaiter::ClockWaiter(
const rclcpp::Clock::SharedPtr & clock)
493 ClockWaiter::~ClockWaiter() =
default;
497 std::unique_lock<std::mutex> & lock,
498 const rclcpp::Time & abs_time,
const std::function<
bool ()> & pred)
500 return impl_->wait_until(lock, abs_time, pred);
511 std::mutex pred_mutex_;
512 bool shutdown_ =
false;
513 rclcpp::Context::SharedPtr context_;
515 ClockWaiter::UniquePtr clock_;
518 Impl(
const rclcpp::Clock::SharedPtr & clock,
const rclcpp::Context::SharedPtr & context)
520 clock_(std::make_unique<ClockWaiter>(clock))
522 if (!context_ || !context_->is_valid()) {
523 throw std::runtime_error(
"context cannot be slept with because it's invalid");
526 shutdown_cb_handle_ = context_->add_on_shutdown_callback(
529 std::unique_lock lock(pred_mutex_);
532 clock_->notify_one();
538 context_->remove_on_shutdown_callback(shutdown_cb_handle_);
543 std::unique_lock<std::mutex> & lock,
const rclcpp::Time & until,
544 const std::function<
bool ()> & pred)
546 if(lock.mutex() != &pred_mutex_) {
547 throw std::runtime_error(
548 "ClockConditionalVariable::wait_until: Internal error, given lock does not use"
549 " mutex returned by this->mutex()");
552 clock_->wait_until(lock, until, [
this, &pred] () ->
bool {
553 return shutdown_ || pred();
561 clock_->notify_one();
571 ClockConditionalVariable::ClockConditionalVariable(
572 const rclcpp::Clock::SharedPtr & clock,
573 const rclcpp::Context::SharedPtr & context)
574 :impl_(std::make_unique<
Impl>(clock, context))
578 ClockConditionalVariable::~ClockConditionalVariable() =
default;
588 std::unique_lock<std::mutex> & lock,
const rclcpp::Time & until,
589 const std::function<
bool ()> & pred)
591 return impl_->wait_until(lock, until, pred);
597 return impl_->mutex();
#define rcl_get_default_allocator
Return a properly initialized rcl_allocator_t with default values.
rcutils_allocator_t rcl_allocator_t
Encapsulation of an allocator.
RCLCPP_PUBLIC void notify_one()
RCLCPP_PUBLIC std::mutex & mutex()
RCLCPP_PUBLIC bool wait_until(std::unique_lock< std::mutex > &lock, const rclcpp::Time &until, const std::function< bool()> &pred)
RCLCPP_PUBLIC bool wait_until(std::unique_lock< std::mutex > &lock, const rclcpp::Time &abs_time, const std::function< bool()> &pred)
RCLCPP_PUBLIC void notify_one()
RCLCPP_PUBLIC rcl_clock_t * get_clock_handle() noexcept
Return the rcl_clock_t clock handle.
RCLCPP_PUBLIC bool ros_time_is_active()
RCLCPP_PUBLIC Time now() const
RCLCPP_PUBLIC bool started()
RCLCPP_PUBLIC JumpHandler::SharedPtr create_jump_callback(const JumpHandler::pre_callback_t &pre_callback, const JumpHandler::post_callback_t &post_callback, const rcl_jump_threshold_t &threshold)
Add a callback to invoke if the jump threshold is exceeded.
RCLCPP_PUBLIC bool sleep_for(const Duration &rel_time, const Context::SharedPtr &context=contexts::get_global_default_context())
RCLCPP_PUBLIC bool wait_until_started(const Context::SharedPtr &context=contexts::get_global_default_context())
RCLCPP_PUBLIC Clock(rcl_clock_type_t clock_type=RCL_SYSTEM_TIME)
Default c'tor.
RCLCPP_PUBLIC std::mutex & get_clock_mutex() noexcept
Get the clock's mutex.
RCLCPP_PUBLIC bool sleep_until(const Time &until, const Context::SharedPtr &context=contexts::get_global_default_context())
rcl_duration_value_t nanoseconds() const
Get duration in nanosecods.
RCLCPP_PUBLIC rcl_time_point_value_t nanoseconds() const
Get the nanoseconds since epoch.
RCLCPP_PUBLIC rcl_clock_type_t get_clock_type() const
Get the clock type.
Versions of rosidl_typesupport_cpp::get_message_type_support_handle that handle adapted types.
Encapsulation of a time source.
rcl_duration_value_t nanoseconds
Duration in nanoseconds and its source.
Describe the prerequisites for calling a time jump callback.
rcl_duration_t min_forward
bool on_clock_change
True to call callback when the clock type changes.
rcl_duration_t min_backward
Struct to describe a jump in time.
rcl_clock_change_t clock_change
Indicate whether or not the source of time changed.
rcl_time_point_value_t nanoseconds
Nanoseconds of the point in time.
enum rcl_clock_type_e rcl_clock_type_t
Time source type, used to indicate the source of a time measurement.
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_clock_get_now(rcl_clock_t *clock, rcl_time_point_value_t *time_point_value)
Fill the time point value with the current value of the associated clock.
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_clock_fini(rcl_clock_t *clock)
Finalize a clock.
@ RCL_ROS_TIME_NO_CHANGE
The source before and after the jump is ROS_TIME.
RCL_PUBLIC RCL_WARN_UNUSED bool rcl_clock_time_started(rcl_clock_t *clock)
Check if the clock has started.
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_clock_add_jump_callback(rcl_clock_t *clock, rcl_jump_threshold_t threshold, rcl_jump_callback_t callback, void *user_data)
Add a callback to be called when a time jump exceeds a threshold.
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_is_enabled_ros_time_override(rcl_clock_t *clock, bool *is_enabled)
Check if the RCL_ROS_TIME time source has the override enabled.
@ RCL_ROS_TIME
Use ROS time.
@ RCL_SYSTEM_TIME
Use system time.
@ RCL_CLOCK_UNINITIALIZED
Clock uninitialized.
@ RCL_STEADY_TIME
Use a steady clock time.
RCL_PUBLIC RCL_WARN_UNUSED rcl_ret_t rcl_clock_init(rcl_clock_type_t clock_type, rcl_clock_t *clock, rcl_allocator_t *allocator)
Initialize a clock based on the passed type.
RCL_PUBLIC RCL_WARN_UNUSED bool rcl_clock_valid(rcl_clock_t *clock)
Check if the clock has valid values.
#define RCL_RET_OK
Success return code.
rmw_ret_t rcl_ret_t
The type that holds an rcl return code.