Add mechanizm for caching local and peer addresses in AsyncSSLSocket.
[folly.git] / folly / AtomicStruct.h
1 /*
2  * Copyright 2015 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 #ifndef FOLLY_ATOMIC_STRUCT_H_
18 #define FOLLY_ATOMIC_STRUCT_H_
19
20 #include <atomic>
21 #include <type_traits>
22 #include <folly/Traits.h>
23 #include <string.h>
24 #include <stdint.h>
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   Atom<Raw> data;
48
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
52     Raw d = 0;
53     memcpy(&d, &v, sizeof(T));
54     return d;
55   }
56
57   static T decode(Raw d) noexcept {
58     T v;
59     memcpy(&v, &d, sizeof(T));
60     return v;
61   }
62
63  public:
64   AtomicStruct() = default;
65   ~AtomicStruct() = default;
66   AtomicStruct(AtomicStruct<T> const &) = delete;
67   AtomicStruct<T>& operator= (AtomicStruct<T> const &) = delete;
68
69   constexpr /* implicit */ AtomicStruct(T v) noexcept : data(encode(v)) {}
70
71   bool is_lock_free() const noexcept {
72     return data.is_lock_free();
73   }
74
75   bool compare_exchange_strong(
76           T& v0, T v1,
77           std::memory_order mo = std::memory_order_seq_cst) noexcept {
78     Raw d0 = encode(v0);
79     bool rv = data.compare_exchange_strong(d0, encode(v1), mo);
80     if (!rv) {
81       v0 = decode(d0);
82     }
83     return rv;
84   }
85
86   bool compare_exchange_weak(
87           T& v0, T v1,
88           std::memory_order mo = std::memory_order_seq_cst) noexcept {
89     Raw d0 = encode(v0);
90     bool rv = data.compare_exchange_weak(d0, encode(v1), mo);
91     if (!rv) {
92       v0 = decode(d0);
93     }
94     return rv;
95   }
96
97   T exchange(T v, std::memory_order mo = std::memory_order_seq_cst) noexcept {
98     return decode(data.exchange(encode(v), mo));
99   }
100
101   /* implicit */ operator T () const noexcept {
102     return decode(data);
103   }
104
105   T load(std::memory_order mo = std::memory_order_seq_cst) const noexcept {
106     return decode(data.load(mo));
107   }
108
109   T operator= (T v) noexcept {
110     return decode(data = encode(v));
111   }
112
113   void store(T v, std::memory_order mo = std::memory_order_seq_cst) noexcept {
114     data.store(encode(v), mo);
115   }
116
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.
122 };
123
124 namespace detail {
125
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; };
134
135 } // namespace detail
136
137 } // namespace folly
138
139 #endif