make Range::size() constexpr
[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   union {
47     Atom<Raw> data;
48     T typedData;
49   };
50
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
54     Raw d = 0;
55     memcpy(&d, &v, sizeof(T));
56     return d;
57   }
58
59   static T decode(Raw d) noexcept {
60     T v;
61     memcpy(&v, &d, sizeof(T));
62     return v;
63   }
64
65  public:
66   AtomicStruct() = default;
67   ~AtomicStruct() = default;
68   AtomicStruct(AtomicStruct<T> const &) = delete;
69   AtomicStruct<T>& operator= (AtomicStruct<T> const &) = delete;
70
71   constexpr /* implicit */ AtomicStruct(T v) noexcept : typedData(v) {}
72
73   bool is_lock_free() const noexcept {
74     return data.is_lock_free();
75   }
76
77   bool compare_exchange_strong(
78           T& v0, T v1,
79           std::memory_order mo = std::memory_order_seq_cst) noexcept {
80     Raw d0 = encode(v0);
81     bool rv = data.compare_exchange_strong(d0, encode(v1), mo);
82     if (!rv) {
83       v0 = decode(d0);
84     }
85     return rv;
86   }
87
88   bool compare_exchange_weak(
89           T& v0, T v1,
90           std::memory_order mo = std::memory_order_seq_cst) noexcept {
91     Raw d0 = encode(v0);
92     bool rv = data.compare_exchange_weak(d0, encode(v1), mo);
93     if (!rv) {
94       v0 = decode(d0);
95     }
96     return rv;
97   }
98
99   T exchange(T v, std::memory_order mo = std::memory_order_seq_cst) noexcept {
100     return decode(data.exchange(encode(v), mo));
101   }
102
103   /* implicit */ operator T () const noexcept {
104     return decode(data);
105   }
106
107   T load(std::memory_order mo = std::memory_order_seq_cst) const noexcept {
108     return decode(data.load(mo));
109   }
110
111   T operator= (T v) noexcept {
112     return decode(data = encode(v));
113   }
114
115   void store(T v, std::memory_order mo = std::memory_order_seq_cst) noexcept {
116     data.store(encode(v), mo);
117   }
118
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.
124 };
125
126 namespace detail {
127
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; };
136
137 } // namespace detail
138
139 } // namespace folly