2 * Copyright 2017 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.
19 #include <folly/Traits.h>
20 #include <folly/synchronization/detail/AtomicUtils.h>
24 #include <type_traits>
29 template <int N> struct AtomicStructIntPick {};
32 /// AtomicStruct<T> work like C++ atomics, but can be used on any POD
36 template <typename> class Atom = std::atomic,
37 typename Raw = typename detail::AtomicStructIntPick<sizeof(T)>::type>
39 static_assert(alignof(T) <= alignof(Raw),
40 "target type can't have stricter alignment than matching int");
41 static_assert(sizeof(T) <= sizeof(Raw),
42 "underlying type isn't big enough");
43 static_assert(std::is_trivial<T>::value ||
44 folly::IsTriviallyCopyable<T>::value,
45 "target type must be trivially copyable");
52 static Raw encode(T v) noexcept {
53 // we expect the compiler to optimize away the memcpy, but without
54 // it we would violate strict aliasing rules
56 memcpy(&d, &v, sizeof(T));
60 static T decode(Raw d) noexcept {
62 memcpy(&v, &d, sizeof(T));
67 AtomicStruct() = default;
68 ~AtomicStruct() = default;
69 AtomicStruct(AtomicStruct<T> const &) = delete;
70 AtomicStruct<T>& operator= (AtomicStruct<T> const &) = delete;
72 constexpr /* implicit */ AtomicStruct(T v) noexcept : typedData(v) {}
74 bool is_lock_free() const noexcept {
75 return data.is_lock_free();
78 bool compare_exchange_strong(
81 std::memory_order mo = std::memory_order_seq_cst) noexcept {
82 return compare_exchange_strong(
83 v0, v1, mo, detail::default_failure_memory_order(mo));
85 bool compare_exchange_strong(
88 std::memory_order success,
89 std::memory_order failure) noexcept {
91 bool rv = data.compare_exchange_strong(d0, encode(v1), success, failure);
98 bool compare_exchange_weak(
101 std::memory_order mo = std::memory_order_seq_cst) noexcept {
102 return compare_exchange_weak(
103 v0, v1, mo, detail::default_failure_memory_order(mo));
105 bool compare_exchange_weak(
108 std::memory_order success,
109 std::memory_order failure) noexcept {
111 bool rv = data.compare_exchange_weak(d0, encode(v1), success, failure);
118 T exchange(T v, std::memory_order mo = std::memory_order_seq_cst) noexcept {
119 return decode(data.exchange(encode(v), mo));
122 /* implicit */ operator T () const noexcept {
126 T load(std::memory_order mo = std::memory_order_seq_cst) const noexcept {
127 return decode(data.load(mo));
130 T operator= (T v) noexcept {
131 return decode(data = encode(v));
134 void store(T v, std::memory_order mo = std::memory_order_seq_cst) noexcept {
135 data.store(encode(v), mo);
138 // std::atomic also provides volatile versions of all of the access
139 // methods. These are callable on volatile objects, and also can
140 // theoretically have different implementations than their non-volatile
141 // counterpart. If someone wants them here they can easily be added
142 // by duplicating the above code and the corresponding unit tests.
147 template <> struct AtomicStructIntPick<1> { typedef uint8_t type; };
148 template <> struct AtomicStructIntPick<2> { typedef uint16_t type; };
149 template <> struct AtomicStructIntPick<3> { typedef uint32_t type; };
150 template <> struct AtomicStructIntPick<4> { typedef uint32_t type; };
151 template <> struct AtomicStructIntPick<5> { typedef uint64_t type; };
152 template <> struct AtomicStructIntPick<6> { typedef uint64_t type; };
153 template <> struct AtomicStructIntPick<7> { typedef uint64_t type; };
154 template <> struct AtomicStructIntPick<8> { typedef uint64_t type; };
156 } // namespace detail