2 * Copyright 2014 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.
17 #ifndef FOLLY_ATOMIC_STRUCT_H_
18 #define FOLLY_ATOMIC_STRUCT_H_
21 #include <type_traits>
22 #include <folly/Traits.h>
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");
49 static Raw encode(T v) noexcept {
50 // we expect the compiler to optimize away the memcpy, but without
51 // it we would violate strict aliasing rules
53 memcpy(&d, &v, sizeof(T));
57 static T decode(Raw d) noexcept {
59 memcpy(&v, &d, sizeof(T));
64 AtomicStruct() = default;
65 ~AtomicStruct() = default;
66 AtomicStruct(AtomicStruct<T> const &) = delete;
67 AtomicStruct<T>& operator= (AtomicStruct<T> const &) = delete;
69 constexpr /* implicit */ AtomicStruct(T v) noexcept : data(encode(v)) {}
71 bool is_lock_free() const noexcept {
72 return data.is_lock_free();
75 bool compare_exchange_strong(
77 std::memory_order mo = std::memory_order_seq_cst) noexcept {
79 bool rv = data.compare_exchange_strong(d0, encode(v1), mo);
86 bool compare_exchange_weak(
88 std::memory_order mo = std::memory_order_seq_cst) noexcept {
90 bool rv = data.compare_exchange_weak(d0, encode(v1), mo);
97 T exchange(T v, std::memory_order mo = std::memory_order_seq_cst) noexcept {
98 return decode(data.exchange(encode(v), mo));
101 /* implicit */ operator T () const noexcept {
105 T load(std::memory_order mo = std::memory_order_seq_cst) const noexcept {
106 return decode(data.load(mo));
109 T operator= (T v) noexcept {
110 return decode(data = encode(v));
113 void store(T v, std::memory_order mo = std::memory_order_seq_cst) noexcept {
114 data.store(encode(v), mo);
117 // std::atomic also provides volatile versions of all of the access
118 // methods. These are callable on volatile objects, and also can
119 // theoretically have different implementations than their non-volatile
120 // counterpart. If someone wants them here they can easily be added
121 // by duplicating the above code and the corresponding unit tests.
126 template <> struct AtomicStructIntPick<1> { typedef uint8_t type; };
127 template <> struct AtomicStructIntPick<2> { typedef uint16_t type; };
128 template <> struct AtomicStructIntPick<3> { typedef uint32_t type; };
129 template <> struct AtomicStructIntPick<4> { typedef uint32_t type; };
130 template <> struct AtomicStructIntPick<5> { typedef uint64_t type; };
131 template <> struct AtomicStructIntPick<6> { typedef uint64_t type; };
132 template <> struct AtomicStructIntPick<7> { typedef uint64_t type; };
133 template <> struct AtomicStructIntPick<8> { typedef uint64_t type; };
135 } // namespace detail