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;
78 : publisher_(publisher), is_running_(false), keep_running_(true), turn_(State::LOOP_NOT_STARTED)
80 thread_ = std::thread(&RealtimePublisher::publishingLoop,
this);
85 while (!thread_.joinable() ||
86 turn_.load(std::memory_order_acquire) == State::LOOP_NOT_STARTED) {
87 std::this_thread::sleep_for(std::chrono::microseconds(100));
92 "Use constructor with rclcpp::Publisher<T>::SharedPtr instead - this class does not make sense "
93 "without a real publisher")]]
95 : is_running_(false), keep_running_(false), turn_(State::LOOP_NOT_STARTED)
102 RCLCPP_DEBUG(rclcpp::get_logger(
"realtime_tools"),
"Waiting for publishing thread to stop....");
104 while (is_running()) {
105 std::this_thread::sleep_for(std::chrono::microseconds(100));
108 rclcpp::get_logger(
"realtime_tools"),
"Publishing thread stopped, joining thread....");
109 if (thread_.joinable()) {
124 std::unique_lock<std::mutex>
lock(msg_mutex_);
125 keep_running_ =
false;
127 updated_cond_.notify_one();
140 return turn_.load(std::memory_order_acquire) == State::REALTIME && msg_mutex_.try_lock();
150 std::unique_lock<std::mutex>
lock(msg_mutex_, std::try_to_lock);
165 std::unique_lock<std::mutex>
lock(msg_mutex_, std::try_to_lock);
168 std::unique_lock<std::mutex> scoped_lock(std::move(
lock));
170 turn_.store(State::NON_REALTIME, std::memory_order_release);
172 updated_cond_.notify_one();
190 "Use try_publish() method instead of this method. This method may be removed in future "
206 turn_.store(State::NON_REALTIME, std::memory_order_release);
216 void lock() { msg_mutex_.lock(); }
225 updated_cond_.notify_one();
228 std::thread & get_thread() {
return thread_; }
230 const std::thread & get_thread()
const {
return thread_; }
232 const MessageT & get_msg()
const {
return msg_; }
234 std::mutex & get_mutex() {
return msg_mutex_; }
236 const std::mutex & get_mutex()
const {
return msg_mutex_; }
247 return turn_.load(std::memory_order_acquire) == State::REALTIME &&
lock.owns_lock();
254 bool is_running()
const {
return is_running_; }
267 void publishingLoop()
271 while (keep_running_) {
275 turn_.store(State::REALTIME, std::memory_order_release);
277 std::unique_lock<std::mutex> lock_(msg_mutex_);
278 updated_cond_.wait(lock_, [&] {
return turn_ == State::NON_REALTIME || !keep_running_; });
284 publisher_->publish(outgoing);
290 PublisherSharedPtr publisher_;
291 std::atomic<bool> is_running_;
292 std::atomic<bool> keep_running_;
296 mutable std::mutex msg_mutex_;
297 std::condition_variable updated_cond_;
299 enum class State :
int { REALTIME, NON_REALTIME, LOOP_NOT_STARTED };
300 std::atomic<State> turn_;
303template <
class MessageT>
304using RealtimePublisherSharedPtr = std::shared_ptr<RealtimePublisher<MessageT>>;