realtime_buffer.h
Go to the documentation of this file.
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 
46 namespace realtime_tools
47 {
48 
49 template <class T>
51 {
52  public:
54  : new_data_available_(false)
55  {
56  // allocate memory
57  non_realtime_data_ = new T();
58  realtime_data_ = new T();
59  }
60 
66  RealtimeBuffer(const T& data)
67  {
68  // allocate memory
69  non_realtime_data_ = new T(data);
70  realtime_data_ = new T(data);
71  }
72 
74  {
76  delete non_realtime_data_;
77  if (realtime_data_)
78  delete realtime_data_;
79  }
80 
82  {
83  // allocate memory
84  non_realtime_data_ = new T();
85  realtime_data_ = new T();
86 
87  // Copy the data from old RTB to new RTB
88  writeFromNonRT(*source.readFromNonRT());
89  }
90 
95  {
96  if (this == &source)
97  return *this;
98 
99  // Copy the data from old RTB to new RTB
100  writeFromNonRT(*source.readFromNonRT());
101 
102  return *this;
103  }
104 
106  {
107  // Check if the data is currently being written to (is locked)
108  std::unique_lock<std::mutex> guard(mutex_, std::try_to_lock);
109  if (guard.owns_lock())
110  {
111  // swap pointers
113  {
114  T* tmp = realtime_data_;
116  non_realtime_data_ = tmp;
117  new_data_available_ = false;
118  }
119  }
120  return realtime_data_;
121  }
122 
123  T* readFromNonRT() const
124  {
125  std::lock_guard<std::mutex> guard(mutex_);
126 
128  return non_realtime_data_;
129  else
130  return realtime_data_;
131  }
132 
133  void writeFromNonRT(const T& data)
134  {
135 #ifdef NON_POLLING
136  std::lock_guard<std::mutex> guard(mutex_);
137 #else
138  std::unique_lock<std::mutex> guard(mutex_, std::try_to_lock);
139  while (!guard.owns_lock()) {
140  std::this_thread::sleep_for(std::chrono::microseconds(500));
141  guard.try_lock();
142  }
143 #endif
144 
145  // copy data into non-realtime buffer
146  *non_realtime_data_ = data;
147  new_data_available_ = true;
148  }
149 
150  void initRT(const T& data)
151  {
152  *non_realtime_data_ = data;
153  *realtime_data_ = data;
154  }
155 
156  private:
157 
161 
162  // Set as mutable so that readFromNonRT() can be performed on a const buffer
163  mutable std::mutex mutex_;
164 
165 }; // class
166 }// namespace
167 
168 #endif
void writeFromNonRT(const T &data)
RealtimeBuffer & operator=(const RealtimeBuffer &source)
Custom assignment operator.
RealtimeBuffer(const RealtimeBuffer &source)
RealtimeBuffer(const T &data)
Constructor for objects that don&#39;t have a default constructor.


realtime_tools
Author(s): Stuart Glaser
autogenerated on Mon Feb 28 2022 23:22:45