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>
27#include <folly/synchronization/detail/AtomicUtils.h>
28
29namespace folly {
30
31namespace detail {
32template <size_t>
33struct AtomicStructRaw;
34template <>
35struct AtomicStructRaw<0> {
36 using type = uint8_t;
37};
38template <>
39struct AtomicStructRaw<1> {
40 using type = uint16_t;
41};
42template <>
43struct AtomicStructRaw<2> {
44 using type = uint32_t;
45};
46template <>
47struct AtomicStructRaw<3> {
48 using type = uint64_t;
49};
50} // namespace detail
51
52/// AtomicStruct<T> work like C++ atomics, but can be used on any POD
53/// type <= 8 bytes.
54template <typename T, template <typename> class Atom = std::atomic>
55class AtomicStruct {
56 private:
57 using Raw = _t<detail::AtomicStructRaw<constexpr_log2_ceil(sizeof(T))>>;
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(
62 std::is_trivial<T>::value || is_trivially_copyable<T>::value,
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
89 bool is_lock_free() const noexcept {
90 return data.is_lock_free();
91 }
92
93 bool compare_exchange_strong(
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 }
100 bool compare_exchange_strong(
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
113 bool compare_exchange_weak(
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 }
120 bool compare_exchange_weak(
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
145 T operator=(T v) noexcept {
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
161