| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
#pragma once |
| 18 |
|
| 19 |
#include <cstdint> |
| 20 |
#include <memory> |
| 21 |
#include <type_traits> |
| 22 |
#include <utility> |
| 23 |
|
| 24 |
#include <folly/SharedMutex.h> |
| 25 |
#include <folly/ThreadLocal.h> |
| 26 |
#include <folly/Utility.h> |
| 27 |
#include <folly/lang/Access.h> |
| 28 |
#include <folly/synchronization/Lock.h> |
| 29 |
#include <folly/synchronization/RelaxedAtomic.h> |
| 30 |
|
| 31 |
namespace folly { |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
template <typename T, typename Mutex = SharedMutex> |
| 65 |
class thread_cached_synchronized { |
| 66 |
static_assert(std::is_same<std::decay_t<T>, T>::value, "not decayed"); |
| 67 |
static_assert(std::is_copy_constructible<T>::value, "not copy-constructible"); |
| 68 |
|
| 69 |
public: |
| 70 |
using value_type = T; |
| 71 |
|
| 72 |
private: |
| 73 |
using version_type = std::uint64_t; |
| 74 |
|
| 75 |
struct truth_state { |
| 76 |
relaxed_atomic<version_type> version{0}; |
| 77 |
Mutex mutex{}; |
| 78 |
value_type value; |
| 79 |
|
| 80 |
template <typename... A> |
| 81 |
truth_state(A&&... a) noexcept( |
| 82 |
std::is_nothrow_constructible<Mutex>{} && |
| 83 |
std::is_nothrow_constructible<value_type, A...>{}) |
| 84 |
: value{static_cast<A&&>(a)...} {} |
| 85 |
}; |
| 86 |
|
| 87 |
struct cache_state { |
| 88 |
version_type version{0}; |
| 89 |
value_type value; |
| 90 |
|
| 91 |
template <typename... A> |
| 92 |
cache_state(A&&... a) |
| 93 |
noexcept(std::is_nothrow_constructible<value_type, A...>{}) |
| 94 |
: value{static_cast<A&&>(a)...} {} |
| 95 |
}; |
| 96 |
using tlp_cache_state = ThreadLocalPtr<cache_state>; |
| 97 |
|
| 98 |
template <typename... A> |
| 99 |
static constexpr bool nx = noexcept(truth_state{ |
| 100 |
in_place, FOLLY_DECLVAL(A)...}); |
| 101 |
|
| 102 |
template <bool C> |
| 103 |
using if_ = std::enable_if_t<C, int>; |
| 104 |
|
| 105 |
using swap_fn = access::swap_fn; |
| 106 |
|
| 107 |
public: |
| 108 |
template <typename A = value_type, if_<std::is_constructible<A>{}> = 0> |
| 109 |
thread_cached_synchronized() noexcept(nx<>) : truth_{in_place} {} |
| 110 |
explicit thread_cached_synchronized(value_type const& a) |
| 111 |
noexcept(nx<value_type const&>) |
| 112 |
: truth_{a} {} |
| 113 |
explicit thread_cached_synchronized(value_type&& a) noexcept(nx<value_type&&>) |
| 114 |
: truth_{static_cast<value_type&&>(a)} {} |
| 115 |
template <typename A, if_<std::is_constructible<value_type, A>{}> = 0> |
| 116 |
explicit thread_cached_synchronized(A&& a) noexcept(nx<A&&>) |
| 117 |
: truth_{static_cast<A&&>(a)} {} |
| 118 |
template <typename... A, if_<std::is_constructible<value_type, A...>{}> = 0> |
| 119 |
explicit thread_cached_synchronized(in_place_t, A&&... a) noexcept(nx<A&&...>) |
| 120 |
: truth_{static_cast<A&&>(a)...} {} |
| 121 |
|
| 122 |
template <typename A, if_<std::is_assignable<value_type&, A>{}> = 0> |
| 123 |
thread_cached_synchronized& operator=(A&& a) noexcept(false) { |
| 124 |
store(static_cast<A&&>(a)); |
| 125 |
return *this; |
| 126 |
} |
| 127 |
|
| 128 |
template <typename A = value_type> |
| 129 |
void store(A&& a = A{}) { |
| 130 |
mutate([&](auto& value) { value = static_cast<A&&>(a); }); |
| 131 |
} |
| 132 |
|
| 133 |
template <typename A = value_type> |
| 134 |
value_type exchange(A&& a = A{}) { |
| 135 |
return mutate([&](auto& value) { |
| 136 |
return std::exchange(value, static_cast<A&&>(a)); |
| 137 |
}); |
| 138 |
} |
| 139 |
|
| 140 |
template <typename A> |
| 141 |
bool compare_exchange(value_type& expected, A&& desired) { |
| 142 |
return mutate_cx(expected, static_cast<A&&>(desired)); |
| 143 |
} |
| 144 |
|
| 145 |
template <typename A> |
| 146 |
void swap(A& that) noexcept(false) { |
| 147 |
mutate([&](auto& value) { access::swap(value, that); }); |
| 148 |
} |
| 149 |
|
| 150 |
template <typename A, if_<is_invocable_v<swap_fn, value_type&, A&>> = 0> |
| 151 |
friend void swap(thread_cached_synchronized& self, A& that) noexcept(false) { |
| 152 |
self.swap(that); |
| 153 |
} |
| 154 |
|
| 155 |
value_type const& operator*() const { return ref(); } |
| 156 |
value_type const* operator->() const { return std::addressof(ref()); } |
| 157 |
value_type load() const { return folly::as_const(ref()); } |
| 158 |
operator value_type() const { return load(); } |
| 159 |
|
| 160 |
private: |
| 161 |
void invalidate_caches() { |
| 162 |
truth_.version = truth_.version + 1; |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
template < |
| 167 |
typename F, |
| 168 |
typename R = invoke_result_t<F, value_type&>, |
| 169 |
if_<std::is_void<R>{}> = 0> |
| 170 |
R mutate_locked(F f) { |
| 171 |
f(truth_.value); |
| 172 |
invalidate_caches(); |
| 173 |
} |
| 174 |
template < |
| 175 |
typename F, |
| 176 |
typename R = invoke_result_t<F, value_type&>, |
| 177 |
if_<!std::is_void<R>{}> = 0> |
| 178 |
R mutate_locked(F f) { |
| 179 |
decltype(auto) ret = f(truth_.value); |
| 180 |
invalidate_caches(); |
| 181 |
return ret; |
| 182 |
} |
| 183 |
template <typename F, typename R = invoke_result_t<F, value_type&>> |
| 184 |
R mutate(F f) { |
| 185 |
unique_lock<Mutex> lock{truth_.mutex}; |
| 186 |
return mutate_locked(f); |
| 187 |
} |
| 188 |
|
| 189 |
template <typename A> |
| 190 |
bool mutate_cx(value_type& expected, A&& desired) { |
| 191 |
unique_lock<Mutex> lock{truth_.mutex}; |
| 192 |
auto const eq = folly::as_const(truth_.value) == folly::as_const(expected); |
| 193 |
if (eq) { |
| 194 |
truth_.value = |
| 195 |
static_cast<A&&>(desired); |
| 196 |
invalidate_caches(); |
| 197 |
} else { |
| 198 |
expected = folly::as_const(truth_.value); |
| 199 |
} |
| 200 |
return eq; |
| 201 |
} |
| 202 |
|
| 203 |
FOLLY_ERASE value_type& ref() const { |
| 204 |
auto const cache = cache_.get(); |
| 205 |
auto const unexpired = cache && cache->version == truth_.version; |
| 206 |
return FOLLY_LIKELY(unexpired) ? cache->value : get_slow(); |
| 207 |
} |
| 208 |
|
| 209 |
FOLLY_NOINLINE value_type& get_slow() const { |
| 210 |
hybrid_lock<Mutex> lock{truth_.mutex}; |
| 211 |
auto cache = cache_.get(); |
| 212 |
if (cache == nullptr) { |
| 213 |
cache = new cache_state{truth_.value}; |
| 214 |
cache_.reset(cache); |
| 215 |
} else { |
| 216 |
cache->value = truth_.value; |
| 217 |
} |
| 218 |
cache->version = truth_.version; |
| 219 |
return cache->value; |
| 220 |
} |
| 221 |
|
| 222 |
mutable truth_state truth_; |
| 223 |
mutable tlp_cache_state cache_; |
| 224 |
}; |
| 225 |
|
| 226 |
} |