37#ifndef REALTIME_TOOLS__REALTIME_PUBLISHER_HPP_
38#define REALTIME_TOOLS__REALTIME_PUBLISHER_HPP_
42#include <condition_variable>
49#include "rclcpp/publisher.hpp"
53template <
class MessageT>
59 using PublisherSharedPtr =
typename rclcpp::Publisher<MessageT>::SharedPtr;
61 using PublishedType =
typename rclcpp::TypeAdapter<MessageT>::custom_type;
62 using ROSMessageType =
typename rclcpp::TypeAdapter<MessageT>::ros_message_type;
67 "This variable is deprecated, it is recommended to use the try_publish() method instead.")]]
81#pragma warning(disable : 4996)
83#pragma GCC diagnostic push
84#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
87 : publisher_(publisher), is_running_(false), keep_running_(true), turn_(State::LOOP_NOT_STARTED)
89 thread_ = std::thread(&RealtimePublisher::publishingLoop,
this);
94 while (!thread_.joinable() ||
95 turn_.load(std::memory_order_acquire) == State::LOOP_NOT_STARTED) {
96 std::this_thread::sleep_for(std::chrono::microseconds(100));
103 RCLCPP_DEBUG(rclcpp::get_logger(
"realtime_tools"),
"Waiting for publishing thread to stop....");
105 while (is_running()) {
106 std::this_thread::sleep_for(std::chrono::microseconds(100));
109 rclcpp::get_logger(
"realtime_tools"),
"Publishing thread stopped, joining thread....");
110 if (thread_.joinable()) {
117#pragma GCC diagnostic pop
130 std::unique_lock<std::mutex> lock(msg_mutex_);
131 keep_running_ =
false;
133 updated_cond_.notify_one();
142 std::unique_lock<std::mutex> lock(msg_mutex_, std::try_to_lock);
157 std::unique_lock<std::mutex> lock(msg_mutex_, std::try_to_lock);
160 std::unique_lock<std::mutex> scoped_lock(std::move(lock));
163#pragma warning(disable : 4996)
165#pragma GCC diagnostic push
166#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
172#pragma GCC diagnostic pop
174 turn_.store(State::NON_REALTIME, std::memory_order_release);
176 updated_cond_.notify_one();
197 "This getter method will be removed. It is recommended to use the try_publish() instead of "
198 "accessing the msg_ variable.")]]
199 const MessageT & get_msg()
const
203#pragma warning(disable : 4996)
205#pragma GCC diagnostic push
206#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
212#pragma GCC diagnostic pop
224 const std::mutex &
get_mutex()
const {
return msg_mutex_; }
232 bool can_publish(std::unique_lock<std::mutex> & lock)
const
234 return turn_.load(std::memory_order_acquire) == State::REALTIME && lock.owns_lock();
238 RealtimePublisher(
const RealtimePublisher &) =
delete;
239 RealtimePublisher & operator=(
const RealtimePublisher &) =
delete;
241 bool is_running()
const {
return is_running_; }
254 void publishingLoop()
258 while (keep_running_) {
262 turn_.store(State::REALTIME, std::memory_order_release);
264 std::unique_lock<std::mutex> lock_(msg_mutex_);
265 updated_cond_.wait(lock_, [&] {
return turn_ == State::NON_REALTIME || !keep_running_; });
268#pragma warning(disable : 4996)
270#pragma GCC diagnostic push
271#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
277#pragma GCC diagnostic pop
283 publisher_->publish(outgoing);
289 PublisherSharedPtr publisher_;
290 std::atomic<bool> is_running_;
291 std::atomic<bool> keep_running_;
295 mutable std::mutex msg_mutex_;
296 std::condition_variable updated_cond_;
298 enum class State :
int { REALTIME, NON_REALTIME, LOOP_NOT_STARTED };
299 std::atomic<State> turn_;
302template <
class MessageT>
303using RealtimePublisherSharedPtr = std::shared_ptr<RealtimePublisher<MessageT>>;