ros2_control - foxy
Loading...
Searching...
No Matches
realtime_buffer.h
1/*
2 * Copyright (c) 2013 hiDOF, Inc.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * * Neither the name of the Willow Garage, 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 OWNER 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/*
31 * Publishing ROS messages is difficult, as the publish function is
32 * not realtime safe. This class provides the proper locking so that
33 * you can call publish in realtime and a separate (non-realtime)
34 * thread will ensure that the message gets published over ROS.
35 *
36 * Author: Wim Meeussen
37 */
38
39#ifndef REALTIME_TOOLS__REALTIME_BUFFER_H_
40#define REALTIME_TOOLS__REALTIME_BUFFER_H_
41
42#include <chrono>
43#include <mutex>
44#include <thread>
45
46namespace realtime_tools
47{
48
49template<class T>
51{
52public:
54 : new_data_available_(false)
55 {
56 // allocate memory
57 non_realtime_data_ = new T();
58 realtime_data_ = new T();
59 }
60
66 explicit RealtimeBuffer(const T & data)
67 : new_data_available_(false)
68 {
69 // allocate memory
70 non_realtime_data_ = new T(data);
71 realtime_data_ = new T(data);
72 }
73
75 {
76 if (non_realtime_data_) {delete non_realtime_data_;}
77 if (realtime_data_) {delete realtime_data_;}
78 }
79
80 RealtimeBuffer(const RealtimeBuffer & source)
81 {
82 // allocate memory
83 non_realtime_data_ = new T();
84 realtime_data_ = new T();
85
86 // Copy the data from old RTB to new RTB
87 writeFromNonRT(*source.readFromNonRT());
88 }
89
94 {
95 if (this == &source) {return *this;}
96
97 // Copy the data from old RTB to new RTB
98 writeFromNonRT(*source.readFromNonRT());
99
100 return *this;
101 }
102
103 T * readFromRT()
104 {
105 // Check if the data is currently being written to (is locked)
106 std::unique_lock<std::mutex> guard(mutex_, std::try_to_lock);
107 if (guard.owns_lock()) {
108 // swap pointers
109 if (new_data_available_) {
110 T * tmp = realtime_data_;
111 realtime_data_ = non_realtime_data_;
112 non_realtime_data_ = tmp;
113 new_data_available_ = false;
114 }
115 }
116 return realtime_data_;
117 }
118
119 T * readFromNonRT() const
120 {
121 std::lock_guard<std::mutex> guard(mutex_);
122
123 if (new_data_available_) {
124 return non_realtime_data_;
125 } else {
126 return realtime_data_;
127 }
128 }
129
130 void writeFromNonRT(const T & data)
131 {
132#ifdef NON_POLLING
133 std::lock_guard<std::mutex> guard(mutex_);
134#else
135 std::unique_lock<std::mutex> guard(mutex_, std::defer_lock);
136 while (!guard.try_lock()) {
137 std::this_thread::sleep_for(std::chrono::microseconds(500));
138 }
139#endif
140
141 // copy data into non-realtime buffer
142 *non_realtime_data_ = data;
143 new_data_available_ = true;
144 }
145
146 void initRT(const T & data)
147 {
148 *non_realtime_data_ = data;
149 *realtime_data_ = data;
150 }
151
152 void reset()
153 {
154 // delete the old memory
155 if (non_realtime_data_) {delete non_realtime_data_;}
156 if (realtime_data_) {delete realtime_data_;}
157
158 // allocate memory
159 non_realtime_data_ = new T();
160 realtime_data_ = new T();
161 }
162
163private:
164 T * realtime_data_;
165 T * non_realtime_data_;
166 bool new_data_available_;
167
168 // Set as mutable so that readFromNonRT() can be performed on a const buffer
169 mutable std::mutex mutex_;
170}; // class
171
172} // namespace realtime_tools
173#endif // REALTIME_TOOLS__REALTIME_BUFFER_H_
Definition realtime_buffer.h:51
RealtimeBuffer & operator=(const RealtimeBuffer &source)
Custom assignment operator.
Definition realtime_buffer.h:93
RealtimeBuffer(const T &data)
Constructor for objects that don't have a default constructor.
Definition realtime_buffer.h:66
Definition realtime_box.h:39