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");
51 static Raw encode(T v) noexcept {
52 // we expect the compiler to optimize away the memcpy, but without
53 // it we would violate strict aliasing rules
55 memcpy(&d, &v, sizeof(T));
59 static T decode(Raw d) noexcept {
61 memcpy(&v, &d, sizeof(T));
66 AtomicStruct() = default;
67 ~AtomicStruct() = default;
68 AtomicStruct(AtomicStruct<T> const &) = delete;
69 AtomicStruct<T>& operator= (AtomicStruct<T> const &) = delete;
71 constexpr /* implicit */ AtomicStruct(T v) noexcept : typedData(v) {}
73 bool is_lock_free() const noexcept {
74 return data.is_lock_free();
77 bool compare_exchange_strong(
79 std::memory_order mo = std::memory_order_seq_cst) noexcept {
81 bool rv = data.compare_exchange_strong(d0, encode(v1), mo);
88 bool compare_exchange_weak(
90 std::memory_order mo = std::memory_order_seq_cst) noexcept {
92 bool rv = data.compare_exchange_weak(d0, encode(v1), mo);
99 T exchange(T v, std::memory_order mo = std::memory_order_seq_cst) noexcept {
100 return decode(data.exchange(encode(v), mo));
103 /* implicit */ operator T () const noexcept {
107 T load(std::memory_order mo = std::memory_order_seq_cst) const noexcept {
108 return decode(data.load(mo));
111 T operator= (T v) noexcept {
112 return decode(data = encode(v));
115 void store(T v, std::memory_order mo = std::memory_order_seq_cst) noexcept {
116 data.store(encode(v), mo);
119 // std::atomic also provides volatile versions of all of the access
120 // methods. These are callable on volatile objects, and also can
121 // theoretically have different implementations than their non-volatile
122 // counterpart. If someone wants them here they can easily be added
123 // by duplicating the above code and the corresponding unit tests.
128 template <> struct AtomicStructIntPick<1> { typedef uint8_t type; };
129 template <> struct AtomicStructIntPick<2> { typedef uint16_t type; };
130 template <> struct AtomicStructIntPick<3> { typedef uint32_t type; };
131 template <> struct AtomicStructIntPick<4> { typedef uint32_t type; };
132 template <> struct AtomicStructIntPick<5> { typedef uint64_t type; };
133 template <> struct AtomicStructIntPick<6> { typedef uint64_t type; };
134 template <> struct AtomicStructIntPick<7> { typedef uint64_t type; };
135 template <> struct AtomicStructIntPick<8> { typedef uint64_t type; };
137 } // namespace detail