15 #include "rclcpp/clock.hpp"
17 #include <condition_variable>
21 #include "rclcpp/exceptions.hpp"
22 #include "rclcpp/utilities.hpp"
24 #include "rcpputils/scope_exit.hpp"
25 #include "rcutils/logging_macros.h"
38 exceptions::throw_from_rcl_error(ret,
"failed to initialize rcl clock");
46 RCUTILS_LOG_ERROR(
"Failed to fini rcl clock.");
52 bool stop_sleeping_ =
false;
53 bool shutdown_ =
false;
54 std::condition_variable cv_;
55 std::mutex wait_mutex_;
56 std::mutex clock_mutex_;
59 JumpHandler::JumpHandler(
60 pre_callback_t pre_callback,
61 post_callback_t post_callback,
63 : pre_callback(pre_callback),
64 post_callback(post_callback),
65 notice_threshold(threshold)
76 Time now(0, 0, impl_->rcl_clock_.type);
80 exceptions::throw_from_rcl_error(ret,
"could not get current time stamp");
89 Context::SharedPtr context)
91 if (!context || !context->is_valid()) {
92 throw std::runtime_error(
"context cannot be slept with because it's invalid");
94 const auto this_clock_type = get_clock_type();
96 throw std::runtime_error(
"until's clock type does not match this clock's type");
98 bool time_source_changed =
false;
104 std::unique_lock lock(impl_->wait_mutex_);
105 impl_->shutdown_ =
true;
107 impl_->cv_.notify_one();
110 auto callback_remover = rcpputils::scope_exit(
111 [context, &shutdown_cb_handle]() {
112 context->remove_on_shutdown_callback(shutdown_cb_handle);
118 const std::chrono::steady_clock::time_point chrono_entry = std::chrono::steady_clock::now();
119 const Duration delta_t = until - rcl_entry;
120 const std::chrono::steady_clock::time_point chrono_until =
121 chrono_entry + std::chrono::nanoseconds(delta_t.
nanoseconds());
124 std::unique_lock lock(impl_->wait_mutex_);
125 while (
now() < until && !impl_->stop_sleeping_ && !impl_->shutdown_ && context->is_valid()) {
126 impl_->cv_.wait_until(lock, chrono_until);
128 impl_->stop_sleeping_ =
false;
130 auto system_time = std::chrono::system_clock::time_point(
132 std::chrono::duration_cast<std::chrono::system_clock::duration>(
136 std::unique_lock lock(impl_->wait_mutex_);
137 while (
now() < until && !impl_->stop_sleeping_ && !impl_->shutdown_ && context->is_valid()) {
138 impl_->cv_.wait_until(lock, system_time);
140 impl_->stop_sleeping_ =
false;
154 std::lock_guard<std::mutex> lk(impl_->wait_mutex_);
155 time_source_changed = true;
157 impl_->cv_.notify_one();
162 auto system_time = std::chrono::system_clock::time_point(
164 std::chrono::duration_cast<std::chrono::system_clock::duration>(
168 std::unique_lock lock(impl_->wait_mutex_);
169 while (
now() < until && !impl_->stop_sleeping_ && !impl_->shutdown_ && context->is_valid() &&
170 !time_source_changed)
172 impl_->cv_.wait_until(lock, system_time);
174 impl_->stop_sleeping_ =
false;
179 std::unique_lock lock(impl_->wait_mutex_);
180 while (
now() < until && !impl_->stop_sleeping_ && !impl_->shutdown_ && context->is_valid() &&
181 !time_source_changed)
183 impl_->cv_.wait(lock);
185 impl_->stop_sleeping_ =
false;
189 if (!context->is_valid() || time_source_changed) {
193 return now() >= until;
206 throw std::runtime_error(
"clock is not rcl_clock_valid");
214 if (!context || !context->is_valid()) {
215 throw std::runtime_error(
"context cannot be slept with because it's invalid");
218 throw std::runtime_error(
"clock cannot be waited on as it is not rcl_clock_valid");
232 Context::SharedPtr context,
235 if (!context || !context->is_valid()) {
236 throw std::runtime_error(
"context cannot be slept with because it's invalid");
239 throw std::runtime_error(
"clock cannot be waited on as it is not rcl_clock_valid");
243 Time start = timeout_clock.
now();
247 while (!
started() && context->is_valid()) {
248 if (timeout < wait_tick_ns) {
251 Duration time_left = start + timeout - timeout_clock.
now();
252 if (time_left > wait_tick_ns) {
259 if (timeout_clock.
now() - start > timeout) {
271 RCUTILS_LOG_ERROR(
"ROS time not valid!");
275 bool is_enabled =
false;
278 exceptions::throw_from_rcl_error(
279 ret,
"Failed to check ros_time_override_status");
287 return &impl_->rcl_clock_;
291 Clock::get_clock_type() const noexcept
293 return impl_->rcl_clock_.type;
299 return impl_->clock_mutex_;
308 const auto * handler =
static_cast<JumpHandler *
>(user_data);
309 if (
nullptr == handler) {
312 if (before_jump && handler->pre_callback) {
313 handler->pre_callback();
314 }
else if (!before_jump && handler->post_callback) {
315 handler->post_callback(*time_jump);
319 JumpHandler::SharedPtr
321 JumpHandler::pre_callback_t pre_callback,
322 JumpHandler::post_callback_t post_callback,
326 JumpHandler::UniquePtr handler(
new JumpHandler(pre_callback, post_callback, threshold));
327 if (
nullptr == handler) {
328 throw std::bad_alloc{};
332 std::lock_guard<std::mutex> clock_guard(impl_->clock_mutex_);
335 &impl_->rcl_clock_, threshold, Clock::on_time_jump,
338 exceptions::throw_from_rcl_error(ret,
"Failed to add time jump callback");
342 std::weak_ptr<Clock::Impl> weak_impl = impl_;
345 return JumpHandler::SharedPtr(handler.release(), [weak_impl](
JumpHandler * handler) noexcept {
346 auto shared_impl = weak_impl.lock();
348 std::lock_guard<std::mutex> clock_guard(shared_impl->clock_mutex_);
349 rcl_ret_t ret = rcl_clock_remove_jump_callback(&shared_impl->rcl_clock_,
350 Clock::on_time_jump, handler);
351 if (RCL_RET_OK != ret) {
352 RCUTILS_LOG_ERROR(
"Failed to remove time jump callback");
363 std::condition_variable cv_;
365 rclcpp::Clock::SharedPtr clock_;
366 bool time_source_changed_ =
false;
370 wait_until_system_time(
371 std::unique_lock<std::mutex> & lock,
372 const rclcpp::Time & abs_time,
const std::function<
bool ()> & pred)
374 auto system_time = std::chrono::system_clock::time_point(
376 std::chrono::duration_cast<std::chrono::system_clock::duration>(
377 std::chrono::nanoseconds(abs_time.
nanoseconds())));
379 return cv_.wait_until(lock, system_time, pred);
383 wait_until_steady_time(
384 std::unique_lock<std::mutex> & lock,
385 const rclcpp::Time & abs_time,
const std::function<
bool ()> & pred)
389 const std::chrono::steady_clock::time_point chrono_entry = std::chrono::steady_clock::now();
391 const std::chrono::steady_clock::time_point chrono_until =
392 chrono_entry + std::chrono::nanoseconds(delta_t.
nanoseconds());
394 return cv_.wait_until(lock, chrono_until, pred);
400 std::unique_lock<std::mutex> & lock,
401 const rclcpp::Time & abs_time,
const std::function<
bool ()> & pred)
412 time_source_changed_ =
false;
417 std::lock_guard<std::mutex> lk(*lock.mutex());
418 time_source_changed_ =
true;
428 auto clock_handler = clock_->create_jump_callback(
430 post_time_jump_callback,
433 if (!clock_->ros_time_is_active()) {
434 auto system_time = std::chrono::system_clock::time_point(
436 std::chrono::duration_cast<std::chrono::system_clock::duration>(
437 std::chrono::nanoseconds(abs_time.
nanoseconds())));
439 return cv_.wait_until(lock, system_time, [
this, &pred] () {
440 return time_source_changed_ || pred();
447 cv_.wait(lock, [
this, &pred, &abs_time] () {
448 return clock_->now() >= abs_time || time_source_changed_ || pred();
451 return clock_->now() < abs_time;
462 std::unique_lock<std::mutex> & lock,
463 const rclcpp::Time & abs_time,
const std::function<
bool ()> & pred)
465 switch(clock_->get_clock_type()) {
467 throw std::runtime_error(
"Error, wait on uninitialized clock called");
469 return wait_until_ros_time(lock, abs_time, pred);
472 return wait_until_steady_time(lock, abs_time, pred);
475 return wait_until_system_time(lock, abs_time, pred);
489 ClockWaiter::ClockWaiter(
const rclcpp::Clock::SharedPtr & clock)
494 ClockWaiter::~ClockWaiter() =
default;
498 std::unique_lock<std::mutex> & lock,
499 const rclcpp::Time & abs_time,
const std::function<
bool ()> & pred)
501 return impl_->wait_until(lock, abs_time, pred);
512 std::mutex pred_mutex_;
513 bool shutdown_ =
false;
514 rclcpp::Context::SharedPtr context_;
516 ClockWaiter::UniquePtr clock_;
519 Impl(
const rclcpp::Clock::SharedPtr & clock, rclcpp::Context::SharedPtr context)
521 clock_(std::make_unique<ClockWaiter>(clock))
523 if (!context_ || !context_->is_valid()) {
524 throw std::runtime_error(
"context cannot be slept with because it's invalid");
527 shutdown_cb_handle_ = context_->add_on_shutdown_callback(
530 std::unique_lock lock(pred_mutex_);
533 clock_->notify_one();
539 context_->remove_on_shutdown_callback(shutdown_cb_handle_);
544 std::unique_lock<std::mutex> & lock,
rclcpp::Time until,
545 const std::function<
bool ()> & pred)
547 if(lock.mutex() != &pred_mutex_) {
548 throw std::runtime_error(
549 "ClockConditionalVariable::wait_until: Internal error, given lock does not use"
550 " mutex returned by this->mutex()");
553 clock_->wait_until(lock, until, [
this, &pred] () ->
bool {
554 return shutdown_ || pred();
562 clock_->notify_one();
572 ClockConditionalVariable::ClockConditionalVariable(
573 const rclcpp::Clock::SharedPtr & clock,
574 rclcpp::Context::SharedPtr context)
575 :impl_(std::make_unique<
Impl>(clock, context))
579 ClockConditionalVariable::~ClockConditionalVariable() =
default;
589 std::unique_lock<std::mutex> & lock,
rclcpp::Time until,
590 const std::function<
bool ()> & pred)
592 return impl_->wait_until(lock, until, pred);
598 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, 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 bool wait_until_started(Context::SharedPtr context=contexts::get_global_default_context())
RCLCPP_PUBLIC Time now() const
RCLCPP_PUBLIC bool started()
RCLCPP_PUBLIC JumpHandler::SharedPtr create_jump_callback(JumpHandler::pre_callback_t pre_callback, 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 Clock(rcl_clock_type_t clock_type=RCL_SYSTEM_TIME)
Default c'tor.
RCLCPP_PUBLIC bool sleep_until(Time until, Context::SharedPtr context=contexts::get_global_default_context())
RCLCPP_PUBLIC std::mutex & get_clock_mutex() noexcept
Get the clock's mutex.
RCLCPP_PUBLIC bool sleep_for(Duration rel_time, 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.