logging: implement FATAL and DFATAL log levels
[folly.git] / folly / AtomicStruct.h
1 /*
2  * Copyright 2017 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 <folly/Traits.h>
20 #include <folly/detail/AtomicUtils.h>
21 #include <stdint.h>
22 #include <string.h>
23 #include <atomic>
24 #include <type_traits>
25
26 namespace folly {
27
28 namespace detail {
29 template <int N> struct AtomicStructIntPick {};
30 }
31
32 /// AtomicStruct<T> work like C++ atomics, but can be used on any POD
33 /// type <= 8 bytes.
34 template <
35     typename T,
36     template<typename> class Atom = std::atomic,
37     typename Raw = typename detail::AtomicStructIntPick<sizeof(T)>::type>
38 class AtomicStruct {
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");
46
47   union {
48     Atom<Raw> data;
49     T typedData;
50   };
51
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
55     Raw d = 0;
56     memcpy(&d, &v, sizeof(T));
57     return d;
58   }
59
60   static T decode(Raw d) noexcept {
61     T v;
62     memcpy(&v, &d, sizeof(T));
63     return v;
64   }
65
66  public:
67   AtomicStruct() = default;
68   ~AtomicStruct() = default;
69   AtomicStruct(AtomicStruct<T> const &) = delete;
70   AtomicStruct<T>& operator= (AtomicStruct<T> const &) = delete;
71
72   constexpr /* implicit */ AtomicStruct(T v) noexcept : typedData(v) {}
73
74   bool is_lock_free() const noexcept {
75     return data.is_lock_free();
76   }
77
78   bool compare_exchange_strong(
79       T& v0,
80       T v1,
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));
84   }
85   bool compare_exchange_strong(
86       T& v0,
87       T v1,
88       std::memory_order success,
89       std::memory_order failure) noexcept {
90     Raw d0 = encode(v0);
91     bool rv = data.compare_exchange_strong(d0, encode(v1), success, failure);
92     if (!rv) {
93       v0 = decode(d0);
94     }
95     return rv;
96   }
97
98   bool compare_exchange_weak(
99       T& v0,
100       T v1,
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));
104   }
105   bool compare_exchange_weak(
106       T& v0,
107       T v1,
108       std::memory_order success,
109       std::memory_order failure) noexcept {
110     Raw d0 = encode(v0);
111     bool rv = data.compare_exchange_weak(d0, encode(v1), success, failure);
112     if (!rv) {
113       v0 = decode(d0);
114     }
115     return rv;
116   }
117
118   T exchange(T v, std::memory_order mo = std::memory_order_seq_cst) noexcept {
119     return decode(data.exchange(encode(v), mo));
120   }
121
122   /* implicit */ operator T () const noexcept {
123     return decode(data);
124   }
125
126   T load(std::memory_order mo = std::memory_order_seq_cst) const noexcept {
127     return decode(data.load(mo));
128   }
129
130   T operator= (T v) noexcept {
131     return decode(data = encode(v));
132   }
133
134   void store(T v, std::memory_order mo = std::memory_order_seq_cst) noexcept {
135     data.store(encode(v), mo);
136   }
137
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.
143 };
144
145 namespace detail {
146
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; };
155
156 } // namespace detail
157
158 } // namespace folly