2 * Copyright 2016 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include <type_traits>
21 #include <folly/Traits.h>
28 template <int N> struct AtomicStructIntPick {};
31 /// AtomicStruct<T> work like C++ atomics, but can be used on any POD
35 template<typename> class Atom = std::atomic,
36 typename Raw = typename detail::AtomicStructIntPick<sizeof(T)>::type>
38 static_assert(alignof(T) <= alignof(Raw),
39 "target type can't have stricter alignment than matching int");
40 static_assert(sizeof(T) <= sizeof(Raw),
41 "underlying type isn't big enough");
42 static_assert(std::is_trivial<T>::value ||
43 folly::IsTriviallyCopyable<T>::value,
44 "target type must be trivially copyable");
48 static Raw encode(T v) noexcept {
49 // we expect the compiler to optimize away the memcpy, but without
50 // it we would violate strict aliasing rules
52 memcpy(&d, &v, sizeof(T));
56 static T decode(Raw d) noexcept {
58 memcpy(&v, &d, sizeof(T));
63 AtomicStruct() = default;
64 ~AtomicStruct() = default;
65 AtomicStruct(AtomicStruct<T> const &) = delete;
66 AtomicStruct<T>& operator= (AtomicStruct<T> const &) = delete;
68 constexpr /* implicit */ AtomicStruct(T v) noexcept : data(encode(v)) {}
70 bool is_lock_free() const noexcept {
71 return data.is_lock_free();
74 bool compare_exchange_strong(
76 std::memory_order mo = std::memory_order_seq_cst) noexcept {
78 bool rv = data.compare_exchange_strong(d0, encode(v1), mo);
85 bool compare_exchange_weak(
87 std::memory_order mo = std::memory_order_seq_cst) noexcept {
89 bool rv = data.compare_exchange_weak(d0, encode(v1), mo);
96 T exchange(T v, std::memory_order mo = std::memory_order_seq_cst) noexcept {
97 return decode(data.exchange(encode(v), mo));
100 /* implicit */ operator T () const noexcept {
104 T load(std::memory_order mo = std::memory_order_seq_cst) const noexcept {
105 return decode(data.load(mo));
108 T operator= (T v) noexcept {
109 return decode(data = encode(v));
112 void store(T v, std::memory_order mo = std::memory_order_seq_cst) noexcept {
113 data.store(encode(v), mo);
116 // std::atomic also provides volatile versions of all of the access
117 // methods. These are callable on volatile objects, and also can
118 // theoretically have different implementations than their non-volatile
119 // counterpart. If someone wants them here they can easily be added
120 // by duplicating the above code and the corresponding unit tests.
125 template <> struct AtomicStructIntPick<1> { typedef uint8_t type; };
126 template <> struct AtomicStructIntPick<2> { typedef uint16_t type; };
127 template <> struct AtomicStructIntPick<3> { typedef uint32_t type; };
128 template <> struct AtomicStructIntPick<4> { typedef uint32_t type; };
129 template <> struct AtomicStructIntPick<5> { typedef uint64_t type; };
130 template <> struct AtomicStructIntPick<6> { typedef uint64_t type; };
131 template <> struct AtomicStructIntPick<7> { typedef uint64_t type; };
132 template <> struct AtomicStructIntPick<8> { typedef uint64_t type; };
134 } // namespace detail