ROS 2 rclcpp + rcl - rolling
rolling-a919a6e5
ROS 2 C++ Client Library with ROS Client Library
|
This class provides a way for storing and executing timer objects. It provides APIs to suit the needs of different applications and execution models. All public APIs provided by this class are thread-safe. More...
#include <rclcpp/experimental/timers_manager.hpp>
Public Member Functions | |
RCLCPP_PUBLIC | TimersManager (std::shared_ptr< rclcpp::Context > context, std::function< void(const rclcpp::TimerBase *, const std::shared_ptr< void > &)> on_ready_callback=nullptr) |
Construct a new TimersManager object. More... | |
RCLCPP_PUBLIC | ~TimersManager () |
Destruct the TimersManager object making sure to stop thread and release memory. | |
RCLCPP_PUBLIC void | add_timer (rclcpp::TimerBase::SharedPtr timer) |
Adds a new timer to the storage, maintaining weak ownership of it. Function is thread safe and it can be called regardless of the state of the timers thread. More... | |
RCLCPP_PUBLIC void | remove_timer (rclcpp::TimerBase::SharedPtr timer) |
Remove a single timer from the object storage. Will do nothing if the timer was not being stored here. Function is thread safe and it can be called regardless of the state of the timers thread. More... | |
RCLCPP_PUBLIC void | clear () |
Remove all the timers stored in the object. Function is thread safe and it can be called regardless of the state of the timers thread. | |
RCLCPP_PUBLIC void | start () |
Starts a thread that takes care of executing the timers stored in this object. Function will throw an error if the timers thread was already running. | |
RCLCPP_PUBLIC void | stop () |
Stops the timers thread. Will do nothing if the timer thread was not running. | |
RCLCPP_PUBLIC size_t | get_number_ready_timers () |
Get the number of timers that are currently ready. This function is thread safe. More... | |
RCLCPP_PUBLIC bool | execute_head_timer () |
Executes head timer if ready. This function is thread safe. This function will try to execute the timer callback regardless of whether the TimersManager on_ready_callback was passed during construction. More... | |
RCLCPP_PUBLIC void | execute_ready_timer (const rclcpp::TimerBase *timer_id, const std::shared_ptr< void > &data) |
Executes timer identified by its ID. This function is thread safe. This function will try to execute the timer callback regardless of whether the TimersManager on_ready_callback was passed during construction. More... | |
RCLCPP_PUBLIC std::optional< std::chrono::nanoseconds > | get_head_timeout () |
Get the amount of time before the next timer triggers. This function is thread safe. More... | |
This class provides a way for storing and executing timer objects. It provides APIs to suit the needs of different applications and execution models. All public APIs provided by this class are thread-safe.
Timers management This class provides APIs to add/remove timers to/from an internal storage. It keeps a list of weak pointers from added timers, and locks them only when they need to be executed or modified. Timers are kept ordered in a binary-heap priority queue. Calls to add/remove APIs will temporarily block the execution of the timers and will require to reorder the internal priority queue. Because of this, they have a not-negligible impact on the performance.
Timers execution The most efficient use of this class consists in letting a TimersManager object to spawn a thread where timers are monitored and optionally executed. This can be controlled via the start
and stop
methods. Ready timers can either be executed or an on_ready_callback can be used to notify other entities that they are ready and need to be executed. Other APIs allow to directly execute a given timer.
This class assumes that the execute_callback()
API of the stored timers is never called by other entities, but it can only be called from here. If this assumption is not respected, the heap property may be invalidated, so timers may be executed out of order, without this object noticing it.
Definition at line 65 of file timers_manager.hpp.
TimersManager::TimersManager | ( | std::shared_ptr< rclcpp::Context > | context, |
std::function< void(const rclcpp::TimerBase *, const std::shared_ptr< void > &)> | on_ready_callback = nullptr |
||
) |
Construct a new TimersManager object.
context | custom context to be used. Shared ownership of the context is held until destruction. |
on_ready_callback | The timers on ready callback. The presence of this function indicates what to do when the TimersManager is running and a timer becomes ready. The TimersManager is considered "running" when the start method has been called. If it's callable, it will be invoked instead of the timer callback. If it's not callable, then the TimersManager will directly execute timers when they are ready. All the methods that execute a given timer (e.g. execute_head_timer or execute_ready_timer ) without the TimersManager being running , i.e. without actually explicitly waiting for the timer to become ready, will ignore this callback. |
Definition at line 28 of file timers_manager.cpp.
void TimersManager::add_timer | ( | rclcpp::TimerBase::SharedPtr | timer | ) |
Adds a new timer to the storage, maintaining weak ownership of it. Function is thread safe and it can be called regardless of the state of the timers thread.
timer | the timer to add. |
std::invalid_argument | if timer is a nullptr. |
Definition at line 45 of file timers_manager.cpp.
bool TimersManager::execute_head_timer | ( | ) |
Executes head timer if ready. This function is thread safe. This function will try to execute the timer callback regardless of whether the TimersManager on_ready_callback was passed during construction.
std::runtime_error | if the timers thread was already running. |
Definition at line 128 of file timers_manager.cpp.
void TimersManager::execute_ready_timer | ( | const rclcpp::TimerBase * | timer_id, |
const std::shared_ptr< void > & | data | ||
) |
Executes timer identified by its ID. This function is thread safe. This function will try to execute the timer callback regardless of whether the TimersManager on_ready_callback was passed during construction.
timer_id | the timer ID of the timer to execute |
data | internal data of the timer |
Definition at line 164 of file timers_manager.cpp.
std::optional< std::chrono::nanoseconds > TimersManager::get_head_timeout | ( | ) |
Get the amount of time before the next timer triggers. This function is thread safe.
std::runtime_error | if the timers thread was already running. |
Definition at line 103 of file timers_manager.cpp.
size_t TimersManager::get_number_ready_timers | ( | ) |
Get the number of timers that are currently ready. This function is thread safe.
std::runtime_error | if the timers thread was already running. |
Definition at line 115 of file timers_manager.cpp.
void TimersManager::remove_timer | ( | rclcpp::TimerBase::SharedPtr | timer | ) |
Remove a single timer from the object storage. Will do nothing if the timer was not being stored here. Function is thread safe and it can be called regardless of the state of the timers thread.
timer | the timer to remove. |
Definition at line 314 of file timers_manager.cpp.