include
realtime_tools
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>
50
class
RealtimeBuffer
51
{
52
public
:
53
RealtimeBuffer
()
54
{
55
// allocate memory
56
non_realtime_data_
=
new
T();
57
realtime_data_
=
new
T();
58
}
59
65
RealtimeBuffer
(
const
T& data)
66
{
67
// allocate memory
68
non_realtime_data_
=
new
T(data);
69
realtime_data_
=
new
T(data);
70
}
71
72
~RealtimeBuffer
()
73
{
74
if
(
non_realtime_data_
)
75
delete
non_realtime_data_
;
76
if
(
realtime_data_
)
77
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
93
RealtimeBuffer
&
operator =
(
const
RealtimeBuffer
& source)
94
{
95
if
(
this
== &source)
96
return
*
this
;
97
98
// Copy the data from old RTB to new RTB
99
writeFromNonRT
(*source.
readFromNonRT
());
100
101
return
*
this
;
102
}
103
104
T*
readFromRT
()
105
{
106
// Check if the data is currently being written to (is locked)
107
std::unique_lock<std::mutex> guard(
mutex_
, std::try_to_lock);
108
if
(guard.owns_lock())
109
{
110
// swap pointers
111
if
(
new_data_available_
)
112
{
113
T* tmp =
realtime_data_
;
114
realtime_data_
=
non_realtime_data_
;
115
non_realtime_data_
= tmp;
116
new_data_available_
=
false
;
117
}
118
}
119
return
realtime_data_
;
120
}
121
122
T*
readFromNonRT
()
const
123
{
124
std::lock_guard<std::mutex> guard(
mutex_
);
125
126
if
(
new_data_available_
)
127
return
non_realtime_data_
;
128
else
129
return
realtime_data_
;
130
}
131
132
void
writeFromNonRT
(
const
T& data)
133
{
134
#ifdef NON_POLLING
135
std::lock_guard<std::mutex> guard(
mutex_
);
136
#else
137
std::unique_lock<std::mutex> guard(
mutex_
, std::try_to_lock);
138
while
(!guard.owns_lock()) {
139
std::this_thread::sleep_for(std::chrono::microseconds(500));
140
guard.try_lock();
141
}
142
#endif
143
144
// copy data into non-realtime buffer
145
*
non_realtime_data_
= data;
146
new_data_available_
=
true
;
147
}
148
149
void
initRT
(
const
T& data)
150
{
151
*
non_realtime_data_
= data;
152
*
realtime_data_
= data;
153
}
154
155
private
:
156
157
T*
realtime_data_
;
158
T*
non_realtime_data_
;
159
bool
new_data_available_
{
false
};
160
161
// Set as mutable so that readFromNonRT() can be performed on a const buffer
162
mutable
std::mutex
mutex_
;
163
164
};
// class
165
}
// namespace
166
167
#endif
realtime_tools::RealtimeBuffer
Definition:
realtime_buffer.h:50
realtime_tools::RealtimeBuffer::writeFromNonRT
void writeFromNonRT(const T &data)
Definition:
realtime_buffer.h:132
realtime_tools::RealtimeBuffer::readFromNonRT
T * readFromNonRT() const
Definition:
realtime_buffer.h:122
realtime_tools::RealtimeBuffer::RealtimeBuffer
RealtimeBuffer(const T &data)
Constructor for objects that don't have a default constructor.
Definition:
realtime_buffer.h:65
realtime_tools::RealtimeBuffer::readFromRT
T * readFromRT()
Definition:
realtime_buffer.h:104
realtime_tools::RealtimeBuffer::realtime_data_
T * realtime_data_
Definition:
realtime_buffer.h:157
realtime_tools::RealtimeBuffer::operator=
RealtimeBuffer & operator=(const RealtimeBuffer &source)
Custom assignment operator.
Definition:
realtime_buffer.h:93
realtime_tools::RealtimeBuffer::new_data_available_
bool new_data_available_
Definition:
realtime_buffer.h:159
realtime_tools::RealtimeBuffer::non_realtime_data_
T * non_realtime_data_
Definition:
realtime_buffer.h:158
realtime_tools::RealtimeBuffer::~RealtimeBuffer
~RealtimeBuffer()
Definition:
realtime_buffer.h:72
realtime_tools
Definition:
realtime_box.h:38
realtime_tools::RealtimeBuffer::mutex_
std::mutex mutex_
Definition:
realtime_buffer.h:162
realtime_tools::RealtimeBuffer::RealtimeBuffer
RealtimeBuffer(const RealtimeBuffer &source)
Definition:
realtime_buffer.h:80
realtime_tools::RealtimeBuffer::RealtimeBuffer
RealtimeBuffer()
Definition:
realtime_buffer.h:53
realtime_tools::RealtimeBuffer::initRT
void initRT(const T &data)
Definition:
realtime_buffer.h:149
realtime_tools
Author(s): Stuart Glaser
autogenerated on Mon Jan 8 2024 03:20:12