1#ifndef NGEN_MULTITHREADING_HPP
2#define NGEN_MULTITHREADING_HPP
12#include <shared_mutex>
13#include <system_error>
25 template <std::
size_t N>
28 for (std::size_t i = 0; i < N; ++i)
60 template <details::
string_literal Name>
96 void fill(std::shared_ptr<T> buffer_ptr,
bool immediate_use);
105 std::shared_ptr<T>
ptr_ =
nullptr;
111 template <
typename Key,
typename T>
129 std::pair<cache_slot<T>&,
bool>
find_or_insert(Key
const& key, std::optional<Key>
const& eviction_floor);
132 std::set<Key>
keys()
const;
164 template <
typename T>
187 template <
typename MapType>
191 auto eviction_iterator = cache.begin();
192 auto end = cache.end();
193 while (eviction_iterator != end && eviction_iterator->first < floor_index) {
194 evicted.insert(evicted.end(), cache.extract(eviction_iterator++));
199 template <
typename T>
202 assert(buffer_ptr !=
nullptr);
203 assert(state_.load(std::memory_order_relaxed) == STATE::EMPTY ||
204 state_.load(std::memory_order_relaxed) == STATE::ERROR);
205 ptr_ = std::move(buffer_ptr);
210 state_.store(immediate_use ? STATE::HOT : STATE::FILLED, std::memory_order_release);
213 template <
typename T>
216 assert(state_.load(std::memory_order_relaxed) == STATE::EMPTY);
217 state_.store(STATE::ERROR, std::memory_order_release);
220 template <
typename T>
223 int state = STATE::EMPTY;
224 while ((state = state_.load(std::memory_order_acquire)) == STATE::EMPTY) {
226 #pragma omp taskyield
229 if (state == STATE::ERROR)
235 if (state == STATE::FILLED)
236 state_.store(STATE::HOT, std::memory_order_release);
241 template <
typename Key,
typename T>
247 std::shared_lock l(mutex);
248 auto cache_iter = cache.find(key);
250 if (cache_iter != cache.end())
251 return {cache_iter->second,
false};
257 decltype(cache) evicted;
258 std::unique_lock ul(mutex);
261 cache_iter = cache.find(key);
263 if (cache_iter != cache.end())
264 return {cache_iter->second,
false};
266 if (eviction_floor) {
267 assert(*eviction_floor <= key);
277 return {cache.try_emplace(key).first->second,
true};
280 template <
typename T>
283 if (
void* existing = pthread_getspecific(key_.key))
284 return *
static_cast<T*
>(existing);
288 std::lock_guard l{instances_mutex_};
289 created = instances_.emplace_back(std::make_unique<T>()).get();
291 int err = pthread_setspecific(key_.key, created);
293 throw std::system_error(err, std::generic_category(),
"pthread_setspecific");
297 template <
typename Key,
typename T>
300 std::shared_lock l(mutex);
301 std::set<Key> result;
302 for (
auto const& entry : cache)
303 result.insert(entry.first);
Definition multithreading.hpp:165
per_thread(per_thread const &)=delete
std::vector< std::unique_ptr< T > > instances_
Definition multithreading.hpp:178
thread_key key_
Definition multithreading.hpp:176
T & get()
Definition multithreading.hpp:281
std::mutex instances_mutex_
Definition multithreading.hpp:177
per_thread & operator=(per_thread const &)=delete
std::mutex & named_library_mutex(char const *name)
Definition multithreading.cpp:10
Definition multithreading.hpp:20
MapType evict_stale_values(MapType &cache, typename MapType::key_type const &floor_index)
Definition multithreading.hpp:188
std::mutex & library_mutex()
Definition multithreading.hpp:61
Definition multithreading.hpp:79
std::variant< std::shared_ptr< T >, failed_fill > get()
Definition multithreading.hpp:221
STATE
Definition multithreading.hpp:107
@ FILLED
Definition multithreading.hpp:107
@ HOT
Definition multithreading.hpp:107
@ EMPTY
Definition multithreading.hpp:107
@ ERROR
Definition multithreading.hpp:107
cache_slot(cache_slot &&)=delete
void fill(std::shared_ptr< T > buffer_ptr, bool immediate_use)
Definition multithreading.hpp:200
cache_slot(cache_slot const &)=delete
std::atomic< int > state_
Definition multithreading.hpp:108
void fail()
Definition multithreading.hpp:214
std::shared_ptr< T > ptr_
Definition multithreading.hpp:105
Definition multithreading.hpp:26
char value[N]
Definition multithreading.hpp:32
constexpr string_literal(const char(&str)[N])
Definition multithreading.hpp:27
Definition multithreading.hpp:68
Definition multithreading.hpp:112
std::pair< cache_slot< T > &, bool > find_or_insert(Key const &key, std::optional< Key > const &eviction_floor)
Definition multithreading.hpp:242
std::set< Key > keys() const
Definition multithreading.hpp:298
std::map< Key, cache_slot< T > > cache
Definition multithreading.hpp:135
std::shared_mutex mutex
Definition multithreading.hpp:136
Definition multithreading.hpp:141
thread_key()
Definition multithreading.cpp:24
thread_key & operator=(thread_key const &)=delete
thread_key(thread_key const &)=delete
~thread_key()
Definition multithreading.cpp:31
pthread_key_t key
Definition multithreading.hpp:146