ros2_control - iron
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
40namespace realtime_tools
41{
42template <class T>
44{
45public:
46 RealtimeBuffer() : new_data_available_(false)
47 {
48 // allocate memory
49 non_realtime_data_ = new T();
50 realtime_data_ = new T();
51 }
52
58 explicit RealtimeBuffer(const T & data) : new_data_available_(false)
59 {
60 // allocate memory
61 non_realtime_data_ = new T(data);
62 realtime_data_ = new T(data);
63 }
64
66 {
67 if (non_realtime_data_) {
68 delete non_realtime_data_;
69 }
70 if (realtime_data_) {
71 delete realtime_data_;
72 }
73 }
74
75 RealtimeBuffer(const RealtimeBuffer & source)
76 {
77 // allocate memory
78 non_realtime_data_ = new T();
79 realtime_data_ = new T();
80
81 // Copy the data from old RTB to new RTB
82 writeFromNonRT(*source.readFromNonRT());
83 }
84
89 {
90 if (this == &source) {
91 return *this;
92 }
93
94 // Copy the data from old RTB to new RTB
95 writeFromNonRT(*source.readFromNonRT());
96
97 return *this;
98 }
99
100 T * readFromRT()
101 {
102 // Check if the data is currently being written to (is locked)
103 std::unique_lock<std::mutex> guard(mutex_, std::try_to_lock);
104 if (guard.owns_lock()) {
105 // swap pointers
106 if (new_data_available_) {
107 T * tmp = realtime_data_;
108 realtime_data_ = non_realtime_data_;
109 non_realtime_data_ = tmp;
110 new_data_available_ = false;
111 }
112 }
113 return realtime_data_;
114 }
115
116 T * readFromNonRT() const
117 {
118 std::lock_guard<std::mutex> guard(mutex_);
119
120 if (new_data_available_) {
121 return non_realtime_data_;
122 } else {
123 return realtime_data_;
124 }
125 }
126
127 void writeFromNonRT(const T & data)
128 {
129#ifdef NON_POLLING
130 std::lock_guard<std::mutex> guard(mutex_);
131#else
132 std::unique_lock<std::mutex> guard(mutex_, std::defer_lock);
133 while (!guard.try_lock()) {
134 std::this_thread::sleep_for(std::chrono::microseconds(500));
135 }
136#endif
137
138 // copy data into non-realtime buffer
139 *non_realtime_data_ = data;
140 new_data_available_ = true;
141 }
142
143 void initRT(const T & data)
144 {
145 *non_realtime_data_ = data;
146 *realtime_data_ = data;
147 }
148
149 void reset()
150 {
151 // delete the old memory
152 if (non_realtime_data_) {
153 delete non_realtime_data_;
154 }
155 if (realtime_data_) {
156 delete realtime_data_;
157 }
158
159 // allocate memory
160 non_realtime_data_ = new T();
161 realtime_data_ = new T();
162 }
163
164private:
165 T * realtime_data_;
166 T * non_realtime_data_;
167 bool new_data_available_;
168
169 // Set as mutable so that readFromNonRT() can be performed on a const buffer
170 mutable std::mutex mutex_;
171}; // class
172
173} // namespace realtime_tools
174#endif // REALTIME_TOOLS__REALTIME_BUFFER_HPP_
Definition realtime_buffer.hpp:44
RealtimeBuffer & operator=(const RealtimeBuffer &source)
Custom assignment operator.
Definition realtime_buffer.hpp:88
RealtimeBuffer(const T &data)
Constructor for objects that don't have a default constructor.
Definition realtime_buffer.hpp:58
A pthread mutex wrapper that provides a mutex with the priority inheritance protocol and a priority c...
Definition async_function_handler.hpp:38