folly: replace old-style header guards with "pragma once"
[folly.git] / folly / AtomicStruct.h
1 /*
2  * Copyright 2016 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 <type_traits>
21 #include <folly/Traits.h>
22 #include <string.h>
23 #include <stdint.h>
24
25 namespace folly {
26
27 namespace detail {
28 template <int N> struct AtomicStructIntPick {};
29 }
30
31 /// AtomicStruct<T> work like C++ atomics, but can be used on any POD
32 /// type <= 8 bytes.
33 template <
34     typename T,
35     template<typename> class Atom = std::atomic,
36     typename Raw = typename detail::AtomicStructIntPick<sizeof(T)>::type>
37 class AtomicStruct {
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");
45
46   Atom<Raw> data;
47
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
51     Raw d = 0;
52     memcpy(&d, &v, sizeof(T));
53     return d;
54   }
55
56   static T decode(Raw d) noexcept {
57     T v;
58     memcpy(&v, &d, sizeof(T));
59     return v;
60   }
61
62  public:
63   AtomicStruct() = default;
64   ~AtomicStruct() = default;
65   AtomicStruct(AtomicStruct<T> const &) = delete;
66   AtomicStruct<T>& operator= (AtomicStruct<T> const &) = delete;
67
68   constexpr /* implicit */ AtomicStruct(T v) noexcept : data(encode(v)) {}
69
70   bool is_lock_free() const noexcept {
71     return data.is_lock_free();
72   }
73
74   bool compare_exchange_strong(
75           T& v0, T v1,
76           std::memory_order mo = std::memory_order_seq_cst) noexcept {
77     Raw d0 = encode(v0);
78     bool rv = data.compare_exchange_strong(d0, encode(v1), mo);
79     if (!rv) {
80       v0 = decode(d0);
81     }
82     return rv;
83   }
84
85   bool compare_exchange_weak(
86           T& v0, T v1,
87           std::memory_order mo = std::memory_order_seq_cst) noexcept {
88     Raw d0 = encode(v0);
89     bool rv = data.compare_exchange_weak(d0, encode(v1), mo);
90     if (!rv) {
91       v0 = decode(d0);
92     }
93     return rv;
94   }
95
96   T exchange(T v, std::memory_order mo = std::memory_order_seq_cst) noexcept {
97     return decode(data.exchange(encode(v), mo));
98   }
99
100   /* implicit */ operator T () const noexcept {
101     return decode(data);
102   }
103
104   T load(std::memory_order mo = std::memory_order_seq_cst) const noexcept {
105     return decode(data.load(mo));
106   }
107
108   T operator= (T v) noexcept {
109     return decode(data = encode(v));
110   }
111
112   void store(T v, std::memory_order mo = std::memory_order_seq_cst) noexcept {
113     data.store(encode(v), mo);
114   }
115
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.
121 };
122
123 namespace detail {
124
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; };
133
134 } // namespace detail
135
136 } // namespace folly