10#ifndef EIGEN_CXX11_THREADPOOL_THREAD_LOCAL_H
11#define EIGEN_CXX11_THREADPOOL_THREAD_LOCAL_H
13#ifdef EIGEN_AVOID_THREAD_LOCAL
15#ifdef EIGEN_THREAD_LOCAL
16#undef EIGEN_THREAD_LOCAL
21#if ((EIGEN_COMP_GNUC) || __has_feature(cxx_thread_local) || EIGEN_COMP_MSVC)
22#define EIGEN_THREAD_LOCAL static thread_local
29#include <Availability.h>
30#include <TargetConditionals.h>
34#if EIGEN_COMP_CLANGAPPLE && \
35 ((EIGEN_COMP_CLANGAPPLE < 8000042) || (TARGET_OS_IPHONE && __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_9_0))
38#undef EIGEN_THREAD_LOCAL
40#elif defined(__ANDROID__) && EIGEN_COMP_CLANG
47#if defined(__ANDROID__) && defined(__clang__) && defined(__NDK_MAJOR__) && defined(__NDK_MINOR__) && \
48 ((__NDK_MAJOR__ < 12) || ((__NDK_MAJOR__ == 12) && (__NDK_MINOR__ < 1)))
49#undef EIGEN_THREAD_LOCAL
56#include "./InternalHeaderCheck.h"
62struct ThreadLocalNoOpInitialize {
63 void operator()(T&)
const {}
67struct ThreadLocalNoOpRelease {
68 void operator()(T&)
const {}
105template <
typename T,
typename Initialize =
internal::ThreadLocalNoOpInitialize<T>,
106 typename Release =
internal::ThreadLocalNoOpRelease<T>>
109 static_assert(std::is_default_constructible<T>::value,
"ThreadLocal data type must be default constructible");
112 explicit ThreadLocal(
int capacity)
113 : ThreadLocal(capacity, internal::ThreadLocalNoOpInitialize<T>(), internal::ThreadLocalNoOpRelease<T>()) {}
115 ThreadLocal(
int capacity, Initialize initialize)
116 : ThreadLocal(capacity, std::move(initialize), internal::ThreadLocalNoOpRelease<T>()) {}
118 ThreadLocal(
int capacity, Initialize initialize, Release release)
119 : initialize_(std::move(initialize)),
120 release_(std::move(release)),
125 eigen_assert(capacity_ >= 0);
126 data_.resize(capacity_);
127 for (
int i = 0; i < capacity_; ++i) {
128 ptr_.emplace_back(
nullptr);
133 std::thread::id this_thread = std::this_thread::get_id();
134 if (capacity_ == 0)
return SpilledLocal(this_thread);
136 std::size_t h = std::hash<std::thread::id>()(this_thread);
137 const int start_idx = h % capacity_;
148 while (ptr_[idx].load() !=
nullptr) {
149 ThreadIdAndValue& record = *(ptr_[idx].load());
150 if (record.thread_id == this_thread)
return record.value;
153 if (idx >= capacity_) idx -= capacity_;
154 if (idx == start_idx)
break;
161 if (filled_records_.load() >= capacity_)
return SpilledLocal(this_thread);
167 int insertion_index = filled_records_.fetch_add(1, std::memory_order_relaxed);
168 if (insertion_index >= capacity_)
return SpilledLocal(this_thread);
172 data_[insertion_index].thread_id = this_thread;
173 initialize_(data_[insertion_index].value);
176 ThreadIdAndValue* inserted = &data_[insertion_index];
179 ThreadIdAndValue* empty =
nullptr;
185 const int insertion_idx = idx;
190 while (ptr_[idx].load() !=
nullptr) {
192 if (idx >= capacity_) idx -= capacity_;
195 eigen_assert(idx != insertion_idx);
199 }
while (!ptr_[idx].compare_exchange_weak(empty, inserted));
201 return inserted->value;
205 void ForEach(std::function<
void(std::thread::id, T&)> f) {
208 for (
auto& ptr : ptr_) {
209 ThreadIdAndValue* record = ptr.load();
210 if (record ==
nullptr)
continue;
211 f(record->thread_id, record->value);
215 if (filled_records_.load(std::memory_order_relaxed) < capacity_)
return;
218 EIGEN_MUTEX_LOCK lock(mu_);
219 for (
auto& kv : per_thread_map_) {
220 f(kv.first, kv.second);
228 for (
auto& ptr : ptr_) {
229 ThreadIdAndValue* record = ptr.load();
230 if (record ==
nullptr)
continue;
231 release_(record->value);
235 if (filled_records_.load(std::memory_order_relaxed) < capacity_)
return;
238 EIGEN_MUTEX_LOCK lock(mu_);
239 for (
auto& kv : per_thread_map_) {
245 struct ThreadIdAndValue {
246 std::thread::id thread_id;
251 T& SpilledLocal(std::thread::id this_thread) {
252 EIGEN_MUTEX_LOCK lock(mu_);
254 auto it = per_thread_map_.find(this_thread);
255 if (it == per_thread_map_.end()) {
256 auto result = per_thread_map_.emplace(this_thread, T());
257 eigen_assert(result.second);
258 initialize_((*result.first).second);
259 return (*result.first).second;
265 Initialize initialize_;
271 MaxSizeVector<ThreadIdAndValue> data_;
275 MaxSizeVector<std::atomic<ThreadIdAndValue*>> ptr_;
278 std::atomic<int> filled_records_;
284 std::unordered_map<std::thread::id, T> per_thread_map_;
Namespace containing all symbols from the Eigen library.
Definition B01_Experimental.dox:1