Class to handle asynchronous function calls. AsyncFunctionHandler is a class that allows the user to have a asynchronous call to the parsed method and be able to set some thread specific parameters.
More...
#include <async_function_handler.hpp>
template<typename T>
class realtime_tools::AsyncFunctionHandler< T >
Class to handle asynchronous function calls. AsyncFunctionHandler is a class that allows the user to have a asynchronous call to the parsed method and be able to set some thread specific parameters.
◆ get_current_callback_period()
Get the current callback period.
- Returns
- The current callback period
◆ get_current_callback_time()
Get the current callback time.
- Returns
- The current callback time
◆ get_last_execution_time()
Get the last execution time of the async callback method.
- Returns
- The last execution time of the async callback method in nanoseconds
◆ get_last_return_value()
Get the last return value of the async callback method.
- Returns
- The last return value of the async callback method
◆ get_thread() [1/2]
Get the async worker thread.
- Returns
- The async callback thread
◆ get_thread() [2/2]
Get the const version of async worker thread.
- Returns
- The async callback thread
◆ init() [1/2]
Initialize the AsyncFunctionHandler with the callback and thread_priority.
- Parameters
-
callback | Function that will be called asynchronously If the AsyncFunctionHandler is already initialized and is running, it will throw a runtime error. If the parsed functions are not valid, it will throw a runtime error. |
◆ init() [2/2]
template<typename T >
void realtime_tools::AsyncFunctionHandler< T >::init |
( |
std::function< T(const rclcpp::Time &, const rclcpp::Duration &)> |
callback, |
|
|
std::function< bool()> |
trigger_predicate, |
|
|
int |
thread_priority = 50 |
|
) |
| |
|
inline |
Initialize the AsyncFunctionHandler with the callback, trigger_predicate and thread_priority.
- Parameters
-
callback | Function that will be called asynchronously. |
trigger_predicate | Predicate function that will be called to check if the async callback method should be triggered or not. |
thread_priority | Priority of the async worker thread. |
- Note
- The parsed trigger_predicate should be free from any concurrency issues. It is expected to be both thread-safe and reentrant.
If the AsyncFunctionHandler is already initialized and is running, it will throw a runtime error. If the parsed functions are not valid, it will throw a runtime error.
◆ is_initialized()
◆ is_running()
Check if the async worker thread is running.
- Returns
- True if the async worker thread is running, false otherwise
◆ is_stopped()
Check if the async callback is triggered to stop the cycle.
- Returns
- True if the async callback is requested to be stopped, false otherwise
◆ is_trigger_cycle_in_progress()
Check if the async callback method is in progress.
- Returns
- True if the async callback method is in progress, false otherwise
◆ join_async_callback_thread()
Join the async callback thread.
If the async method is running, it will join the async thread. If the async method is not running, it will return immediately.
◆ reset_variables()
Resets the internal variables of the AsyncFunctionHandler.
A method to reset the internal variables of the AsyncFunctionHandler. It will reset the async callback return value, exception pointer, and the trigger status.
- Note
- This method should be invoked after catching an exception in the async callback thread, to be able to trigger the async callback method again.
◆ start_thread()
Initializes and starts the callback thread.
If the worker thread is not running, it will start the async callback thread. If the worker thread is already configured and running, does nothing and returns immediately.
◆ stop_thread()
Stops the callback thread.
If the async method is running, it will notify the async thread to stop and then joins the async thread.
◆ trigger_async_callback()
Triggers the async callback method cycle.
- Parameters
-
time | Current time |
period | Current period |
- Returns
- A pair with the first element being a boolean indicating if the async callback method was triggered and the second element being the last return value of the async function. If the AsyncFunctionHandler is not initialized properly, it will throw a runtime error. If the callback method is waiting for the trigger, it will notify the async thread to start the callback. If the async callback method is still running, it will return the last return value from the last trigger cycle.
- Note
- If an exception is caught in the async callback thread, it will be rethrown in the current thread, so in order to have the trigger_async_callback method working again, the exception should be caught and the
reset_variables
method should be invoked.
-
In the case of controllers, The controller manager is responsible for triggering and maintaining the controller's update rate, as it should be only acting as a scheduler. Same applies to the resource manager when handling the hardware components.
◆ wait_for_trigger_cycle_to_finish()
Waits until the current async callback method trigger cycle is finished.
If the async method is running, it will wait for the current async method call to finish.
The documentation for this class was generated from the following file: