ros2_control - rolling
Loading...
Searching...
No Matches
realtime_buffer.hpp
1// Copyright (c) 2013, hiDOF, Inc.
2//
3// Redistribution and use in source and binary forms, with or without
4// modification, are permitted provided that the following conditions are met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8//
9// * Redistributions in binary form must reproduce the above copyright
10// notice, this list of conditions and the following disclaimer in the
11// documentation and/or other materials provided with the distribution.
12//
13// * Neither the name of the hiDOF, Inc. nor the names of its
14// contributors may be used to endorse or promote products derived from
15// this software without specific prior written permission.
16//
17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27// POSSIBILITY OF SUCH DAMAGE.
28
29/*
30 * Author: Wim Meeussen
31 */
32
33#ifndef REALTIME_TOOLS__REALTIME_BUFFER_HPP_
34#define REALTIME_TOOLS__REALTIME_BUFFER_HPP_
35
36#include <chrono>
37#include <mutex>
38#include <thread>
39
40#ifndef SILENCE_DEPRECATION_WARNINGS
41#ifdef _MSC_VER
42#pragma message( \
43 "This header is obsolete, please refer to the guidelines for replacement options: " \
44 "control.ros.org/rolling/doc/realtime_tools/doc/index.html#guidelines")
45#else
46#warning This header is obsolete, please refer to the guidelines for replacement options: \
47control.ros.org/rolling/doc/realtime_tools/doc/index.html#guidelines
48#endif
49#endif
50
51namespace realtime_tools
52{
53template <class T>
54class [[deprecated(
55 "refer to the guidelines for replacement options: "
56 "control.ros.org/rolling/doc/realtime_tools/doc/index.html#guidelines")]] RealtimeBuffer
57{
58public:
59 RealtimeBuffer() : new_data_available_(false)
60 {
61 // allocate memory
62 non_realtime_data_ = new T();
63 realtime_data_ = new T();
64 }
65
71 explicit RealtimeBuffer(const T & data) : new_data_available_(false)
72 {
73 // allocate memory
74 non_realtime_data_ = new T(data);
75 realtime_data_ = new T(data);
76 }
77
79 {
80 if (non_realtime_data_) {
81 delete non_realtime_data_;
82 }
83 if (realtime_data_) {
84 delete realtime_data_;
85 }
86 }
87
88 RealtimeBuffer(const RealtimeBuffer & source)
89 {
90 // allocate memory
91 non_realtime_data_ = new T();
92 realtime_data_ = new T();
93
94 // Copy the data from old RTB to new RTB
95 writeFromNonRT(*source.readFromNonRT());
96 }
97
102 {
103 if (this == &source) {
104 return *this;
105 }
106
107 // Copy the data from old RTB to new RTB
108 writeFromNonRT(*source.readFromNonRT());
109
110 return *this;
111 }
112
113 T * readFromRT()
114 {
115 // Check if the data is currently being written to (is locked)
116 std::unique_lock<std::mutex> guard(mutex_, std::try_to_lock);
117 if (guard.owns_lock()) {
118 // swap pointers
119 if (new_data_available_) {
120 T * tmp = realtime_data_;
121 realtime_data_ = non_realtime_data_;
122 non_realtime_data_ = tmp;
123 new_data_available_ = false;
124 }
125 }
126 return realtime_data_;
127 }
128
129 T * readFromNonRT() const
130 {
131 std::lock_guard<std::mutex> guard(mutex_);
132
133 if (new_data_available_) {
134 return non_realtime_data_;
135 } else {
136 return realtime_data_;
137 }
138 }
139
140 void writeFromNonRT(const T & data)
141 {
142#ifdef NON_POLLING
143 std::lock_guard<std::mutex> guard(mutex_);
144#else
145 std::unique_lock<std::mutex> guard(mutex_, std::defer_lock);
146 while (!guard.try_lock()) {
147 std::this_thread::sleep_for(std::chrono::microseconds(500));
148 }
149#endif
150
151 // copy data into non-realtime buffer
152 *non_realtime_data_ = data;
153 new_data_available_ = true;
154 }
155
156 void initRT(const T & data)
157 {
158 *non_realtime_data_ = data;
159 *realtime_data_ = data;
160 }
161
162 void reset()
163 {
164 // delete the old memory
165 if (non_realtime_data_) {
166 delete non_realtime_data_;
167 }
168 if (realtime_data_) {
169 delete realtime_data_;
170 }
171
172 // allocate memory
173 non_realtime_data_ = new T();
174 realtime_data_ = new T();
175 }
176
177private:
178 T * realtime_data_;
179 T * non_realtime_data_;
180 bool new_data_available_;
181
182 // Set as mutable so that readFromNonRT() can be performed on a const buffer
183 mutable std::mutex mutex_;
184}; // class
185
186} // namespace realtime_tools
187#endif // REALTIME_TOOLS__REALTIME_BUFFER_HPP_
Definition realtime_buffer.hpp:57
RealtimeBuffer & operator=(const RealtimeBuffer &source)
Custom assignment operator.
Definition realtime_buffer.hpp:101
RealtimeBuffer(const T &data)
Constructor for objects that don't have a default constructor.
Definition realtime_buffer.hpp:71
A pthread mutex wrapper that provides a mutex with the priority inheritance protocol and a priority c...
Definition async_function_handler.hpp:42