proxygen
AtomicStruct.h
Go to the documentation of this file.
1 /*
2  * Copyright 2013-present Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <atomic>
20 #include <cstddef>
21 #include <cstdint>
22 #include <cstring>
23 #include <type_traits>
24 
25 #include <folly/ConstexprMath.h>
26 #include <folly/Traits.h>
28 
29 namespace folly {
30 
31 namespace detail {
32 template <size_t>
34 template <>
35 struct AtomicStructRaw<0> {
36  using type = uint8_t;
37 };
38 template <>
39 struct AtomicStructRaw<1> {
40  using type = uint16_t;
41 };
42 template <>
43 struct AtomicStructRaw<2> {
44  using type = uint32_t;
45 };
46 template <>
47 struct AtomicStructRaw<3> {
48  using type = uint64_t;
49 };
50 } // namespace detail
51 
54 template <typename T, template <typename> class Atom = std::atomic>
55 class AtomicStruct {
56  private:
58 
59  static_assert(alignof(T) <= alignof(Raw), "underlying type is under-aligned");
60  static_assert(sizeof(T) <= sizeof(Raw), "underlying type is under-sized");
61  static_assert(
63  "target type must be trivially copyable");
64 
65  Atom<Raw> data;
66 
67  static Raw encode(T v) noexcept {
68  // we expect the compiler to optimize away the memcpy, but without
69  // it we would violate strict aliasing rules
70  Raw d = 0;
71  memcpy(&d, static_cast<void*>(&v), sizeof(T));
72  return d;
73  }
74 
75  static T decode(Raw d) noexcept {
76  T v;
77  memcpy(static_cast<void*>(&v), &d, sizeof(T));
78  return v;
79  }
80 
81  public:
82  AtomicStruct() = default;
83  ~AtomicStruct() = default;
84  AtomicStruct(AtomicStruct<T> const&) = delete;
85  AtomicStruct<T>& operator=(AtomicStruct<T> const&) = delete;
86 
87  constexpr /* implicit */ AtomicStruct(T v) noexcept : data(encode(v)) {}
88 
90  return data.is_lock_free();
91  }
92 
94  T& v0,
95  T v1,
96  std::memory_order mo = std::memory_order_seq_cst) noexcept {
97  return compare_exchange_strong(
98  v0, v1, mo, detail::default_failure_memory_order(mo));
99  }
101  T& v0,
102  T v1,
103  std::memory_order success,
104  std::memory_order failure) noexcept {
105  Raw d0 = encode(v0);
106  bool rv = data.compare_exchange_strong(d0, encode(v1), success, failure);
107  if (!rv) {
108  v0 = decode(d0);
109  }
110  return rv;
111  }
112 
114  T& v0,
115  T v1,
116  std::memory_order mo = std::memory_order_seq_cst) noexcept {
117  return compare_exchange_weak(
118  v0, v1, mo, detail::default_failure_memory_order(mo));
119  }
121  T& v0,
122  T v1,
123  std::memory_order success,
124  std::memory_order failure) noexcept {
125  Raw d0 = encode(v0);
126  bool rv = data.compare_exchange_weak(d0, encode(v1), success, failure);
127  if (!rv) {
128  v0 = decode(d0);
129  }
130  return rv;
131  }
132 
133  T exchange(T v, std::memory_order mo = std::memory_order_seq_cst) noexcept {
134  return decode(data.exchange(encode(v), mo));
135  }
136 
137  /* implicit */ operator T() const noexcept {
138  return decode(data);
139  }
140 
141  T load(std::memory_order mo = std::memory_order_seq_cst) const noexcept {
142  return decode(data.load(mo));
143  }
144 
146  return decode(data = encode(v));
147  }
148 
149  void store(T v, std::memory_order mo = std::memory_order_seq_cst) noexcept {
150  data.store(encode(v), mo);
151  }
152 
153  // std::atomic also provides volatile versions of all of the access
154  // methods. These are callable on volatile objects, and also can
155  // theoretically have different implementations than their non-volatile
156  // counterpart. If someone wants them here they can easily be added
157  // by duplicating the above code and the corresponding unit tests.
158 };
159 
160 } // namespace folly
_t< detail::AtomicStructRaw< constexpr_log2_ceil(sizeof(folly::IndexedMemPool::TaggedPtr))>> Raw
Definition: AtomicStruct.h:57
void store(T v, std::memory_order mo=std::memory_order_seq_cst) noexcept
Definition: AtomicStruct.h:149
unique_ptr< IOBuf > encode(vector< HPACKHeader > &headers, HPACKEncoder &encoder)
auto v
bool compare_exchange_weak(T &v0, T v1, std::memory_order success, std::memory_order failure) noexcept
Definition: AtomicStruct.h:120
TokenBindingMessage decode(folly::io::Cursor &cursor)
Definition: Types.cpp:132
bool compare_exchange_strong(T &v0, T v1, std::memory_order mo=std::memory_order_seq_cst) noexcept
Definition: AtomicStruct.h:93
static T decode(Raw d) noexcept
Definition: AtomicStruct.h:75
folly::std T
T load(std::memory_order mo=std::memory_order_seq_cst) const noexcept
Definition: AtomicStruct.h:141
T exchange(T v, std::memory_order mo=std::memory_order_seq_cst) noexcept
Definition: AtomicStruct.h:133
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
requires E e noexcept(noexcept(s.error(std::move(e))))
constexpr AtomicStruct(T v) noexcept
Definition: AtomicStruct.h:87
Atom< Raw > data
Definition: AtomicStruct.h:59
bool is_lock_free() const noexcept
Definition: AtomicStruct.h:89
std::memory_order default_failure_memory_order(std::memory_order successMode)
Definition: AtomicUtils.h:24
typename T::type _t
Definition: Traits.h:171
bool compare_exchange_strong(T &v0, T v1, std::memory_order success, std::memory_order failure) noexcept
Definition: AtomicStruct.h:100
#define Atom
constexpr auto data(C &c) -> decltype(c.data())
Definition: Access.h:71
std::is_trivially_copyable< T > is_trivially_copyable
Definition: Traits.h:313
static const char *const value
Definition: Conv.cpp:50
static Raw encode(T v) noexcept
Definition: AtomicStruct.h:67
bool compare_exchange_weak(T &v0, T v1, std::memory_order mo=std::memory_order_seq_cst) noexcept
Definition: AtomicStruct.h:113
const
Definition: upload.py:398
T operator=(T v) noexcept
Definition: AtomicStruct.h:145