]>
git.proxmox.com Git - ceph.git/blob - ceph/src/common/ceph_atomic.h
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
11 // ceph::atomic – thin wrapper to differentiate behavior of atomics.
13 // Not all users of the common truly need costly atomic operations to
14 // synchronize data between CPUs and threads. Some, like crimson-osd,
15 // stick to shared-nothing approach. Enforcing issue of atomics in
16 // such cases is wasteful – on x86 any locked instruction works actually
17 // like a full memory barrier stalling execution till CPU's store and
18 // load buffers are drained.
20 #if defined(WITH_SEASTAR) && !defined(WITH_BLUESTORE)
22 #include <type_traits>
30 dummy_atomic() = default;
31 dummy_atomic(const dummy_atomic
&) = delete;
32 dummy_atomic(T value
) : value(std::move(value
)) {
34 bool is_lock_free() const noexcept
{
37 void store(T desired
, std::memory_order
) noexcept
{
38 value
= std::move(desired
);
40 T
load(std::memory_order
= std::memory_order_seq_cst
) const noexcept
{
43 T
operator=(T desired
) noexcept
{
44 value
= std::move(desired
);
46 operator T() const noexcept
{
50 // We need to differentiate with SFINAE as std::atomic offers beefier
51 // interface for integral types.
52 std::enable_if_t
<std::is_integral_v
<T
>, T
> operator++() {
55 std::enable_if_t
<std::is_integral_v
<T
>, T
> operator++(int) {
58 std::enable_if_t
<std::is_integral_v
<T
>, T
> operator--() {
61 std::enable_if_t
<std::is_integral_v
<T
>, T
> operator--(int) {
64 std::enable_if_t
<std::is_integral_v
<T
>, T
> operator+=(const dummy_atomic
& b
) {
68 std::enable_if_t
<std::is_integral_v
<T
>, T
> operator-=(const dummy_atomic
& b
) {
73 static constexpr bool is_always_lock_free
= true;
76 template <class T
> using atomic
= dummy_atomic
<T
>;
82 template <class T
> using atomic
= ::std::atomic
<T
>;
85 #endif // WITH_SEASTAR