19
19
20
20
#include < iomanip>
21
21
#include < iostream>
22
+ #include < mutex>
22
23
#include < string>
23
24
#include < unordered_map>
24
25
@@ -41,6 +42,7 @@ class SpanContext : public opentracing::SpanContext {
41
42
, _spanID(0 )
42
43
, _parentID(0 )
43
44
, _flags(0 )
45
+ , _mutex()
44
46
{
45
47
}
46
48
@@ -56,6 +58,7 @@ class SpanContext : public opentracing::SpanContext {
56
58
, _flags(flags)
57
59
, _baggage(baggage)
58
60
, _debugID(debugID)
61
+ , _mutex()
59
62
{
60
63
}
61
64
@@ -98,13 +101,15 @@ class SpanContext : public opentracing::SpanContext {
98
101
99
102
SpanContext withBaggage (const StrMap& baggage) const
100
103
{
104
+ std::lock_guard<std::mutex> lock (_mutex);
101
105
return SpanContext (
102
106
_traceID, _spanID, _parentID, _flags, baggage, _debugID);
103
107
}
104
108
105
109
template <typename Function>
106
110
void forEachBaggageItem (Function f) const
107
111
{
112
+ std::lock_guard<std::mutex> lock (_mutex);
108
113
for (auto && pair : _baggage) {
109
114
if (!f (pair.first , pair.second )) {
110
115
break ;
@@ -115,6 +120,7 @@ class SpanContext : public opentracing::SpanContext {
115
120
template <typename Function>
116
121
void forEachBaggageItem (Function f)
117
122
{
123
+ std::lock_guard<std::mutex> lock (_mutex);
118
124
for (auto && pair : _baggage) {
119
125
if (!f (pair.first , pair.second )) {
120
126
break ;
@@ -160,9 +166,17 @@ class SpanContext : public opentracing::SpanContext {
160
166
161
167
bool operator ==(const SpanContext& rhs) const
162
168
{
169
+ {
170
+ std::lock (_mutex, rhs._mutex );
171
+ std::lock_guard<std::mutex> lock (_mutex, std::adopt_lock);
172
+ std::lock_guard<std::mutex> rhsLock (rhs._mutex , std::adopt_lock);
173
+ if (_baggage != rhs._baggage ) {
174
+ return false ;
175
+ }
176
+ }
163
177
return _traceID == rhs._traceID && _spanID == rhs._spanID &&
164
178
_parentID == rhs._parentID && _flags == rhs._flags &&
165
- _baggage == rhs. _baggage && _debugID == rhs._debugID ;
179
+ _debugID == rhs._debugID ;
166
180
}
167
181
168
182
private:
@@ -172,6 +186,7 @@ class SpanContext : public opentracing::SpanContext {
172
186
unsigned char _flags;
173
187
StrMap _baggage;
174
188
std::string _debugID;
189
+ mutable std::mutex _mutex; // Protects _baggage.
175
190
};
176
191
177
192
} // namespace jaegertracing
0 commit comments