Skip to content

Commit db56654

Browse files
committed
Use std::atomic instead of ATOMIC_READ/WRITE_8 macroses
1 parent cea621d commit db56654

File tree

3 files changed

+9
-33
lines changed

3 files changed

+9
-33
lines changed

api/include/opentelemetry/common/macros.h

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -337,31 +337,6 @@ point.
337337

338338
#endif
339339

340-
//
341-
// Atomic wrappers based on compiler intrinsics for memory read/write.
342-
// The tailing number is read/write length in bits.
343-
//
344-
// N.B. Compiler intrinsic is used because the usage of C++ standard library is restricted in the
345-
// OpenTelemetry C++ API.
346-
//
347-
#if defined(__GNUC__)
348-
349-
# define OPENTELEMETRY_ATOMIC_READ_8(ptr) __atomic_load_n(ptr, __ATOMIC_SEQ_CST)
350-
# define OPENTELEMETRY_ATOMIC_WRITE_8(ptr, value) __atomic_store_n(ptr, value, __ATOMIC_SEQ_CST)
351-
352-
#elif defined(_MSC_VER)
353-
354-
# include <intrin.h>
355-
356-
# define OPENTELEMETRY_ATOMIC_READ_8(ptr) \
357-
static_cast<uint8_t>(_InterlockedCompareExchange8(reinterpret_cast<char *>(ptr), 0, 0))
358-
# define OPENTELEMETRY_ATOMIC_WRITE_8(ptr, value) \
359-
_InterlockedExchange8(reinterpret_cast<char *>(ptr), static_cast<char>(value))
360-
361-
#else
362-
# error port atomics read/write for the current platform
363-
#endif
364-
365340
/* clang-format on */
366341
//
367342
// The if/elif order matters here. If both OPENTELEMETRY_BUILD_IMPORT_DLL and

api/include/opentelemetry/logs/logger.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
#pragma once
55

6+
#include <atomic>
7+
68
#include "opentelemetry/version.h"
79
#include "opentelemetry/logs/logger_type_traits.h"
810
#include "opentelemetry/logs/severity.h"
@@ -286,7 +288,7 @@ class Logger
286288

287289
inline bool Enabled(Severity severity) const noexcept
288290
{
289-
return static_cast<uint8_t>(severity) >= OPENTELEMETRY_ATOMIC_READ_8(&minimum_severity_);
291+
return static_cast<uint8_t>(severity) >= minimum_severity_;
290292
}
291293

292294
/**
@@ -473,7 +475,7 @@ class Logger
473475

474476
void SetMinimumSeverity(uint8_t severity_or_max) noexcept
475477
{
476-
OPENTELEMETRY_ATOMIC_WRITE_8(&minimum_severity_, severity_or_max);
478+
minimum_severity_ = severity_or_max;
477479
}
478480

479481
private:
@@ -486,7 +488,7 @@ class Logger
486488
// read/write should be handled. And std::atomic can not be used here because it is not ABI
487489
// compatible for OpenTelemetry C++ API.
488490
//
489-
mutable uint8_t minimum_severity_{kMaxSeverity};
491+
mutable std::atomic<uint8_t> minimum_severity_{kMaxSeverity};
490492
};
491493
} // namespace logs
492494
OPENTELEMETRY_END_NAMESPACE

api/include/opentelemetry/trace/tracer.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#pragma once
55

66
#include <chrono>
7+
#include <atomic>
78

89
#include "opentelemetry/version.h"
910
#include "opentelemetry/context/context.h"
@@ -172,7 +173,7 @@ class Tracer
172173
*
173174
* @since ABI_VERSION 2
174175
*/
175-
bool Enabled() const noexcept { return OPENTELEMETRY_ATOMIC_READ_8(&this->enabled_) != 0; }
176+
bool Enabled() const noexcept { return enabled_ != 0; }
176177
#endif
177178

178179
#if OPENTELEMETRY_ABI_VERSION_NO == 1
@@ -227,17 +228,15 @@ class Tracer
227228
*/
228229
void UpdateEnabled(const bool enabled) noexcept
229230
{
230-
OPENTELEMETRY_ATOMIC_WRITE_8(&this->enabled_, enabled);
231+
enabled_ = enabled;
231232
}
232233
#endif
233234

234235
private:
235236
#if OPENTELEMETRY_ABI_VERSION_NO >= 2
236237
// Variable to support implementation of Enabled method introduced in ABI V2.
237238
// Mutable allows enabled_ to be used as 'bool *' (instead of 'const bool *'), with the
238-
// OPENTELEMETRY_ATOMIC_READ_8 macro's internal casts when used from a const function.
239-
// std::atomic can not be used here because it is not ABI compatible for OpenTelemetry C++ API.
240-
mutable bool enabled_ = true;
239+
mutable std::atomic<bool> enabled_ = true;
241240
#endif
242241
};
243242
} // namespace trace

0 commit comments

Comments
 (0)