32#ifndef MADNESS_WORLD_THREAD_SPECIFIC_H__INCLUDED
33#define MADNESS_WORLD_THREAD_SPECIFIC_H__INCLUDED
81 template <
typename Item>
90 }
else if (rc == ENOMEM) {
108 Slot* s =
static_cast<Slot*
>(pthread_getspecific(
key()));
109 if (s !=
nullptr && s->gen ==
gen_.load(std::memory_order_acquire))
118 std::lock_guard<std::mutex> lock(
mtx_);
120 gen_.fetch_add(1, std::memory_order_acq_rel);
125 std::lock_guard<std::mutex> lock(
mtx_);
140 std::lock_guard<std::mutex> lock(
mtx_);
141 const auto tid = std::this_thread::get_id();
142 auto it =
items_.find(tid);
144 it =
items_.emplace(tid, std::make_unique<Item>(
init_)).first;
147 pthread_setspecific(
key(), s);
149 s->item = it->second.get();
150 s->gen =
gen_.load(std::memory_order_relaxed);
157 pthread_key_delete(*key_ptr);
163 std::atomic<std::uint64_t>
gen_;
165 std::map<std::thread::id, std::unique_ptr<Item>>
items_;
Definition thread_specific.h:82
Item init_
seed for new items
Definition thread_specific.h:162
thread_specific(const thread_specific &)=delete
Item & local_slow(Slot *s)
Definition thread_specific.h:139
void clear()
Definition thread_specific.h:117
thread_specific(thread_specific &&)=delete
Item & local()
Definition thread_specific.h:107
std::mutex mtx_
guards items_
Definition thread_specific.h:164
std::unique_ptr< pthread_key_t, decltype(&pthread_key_deleter)> key_ptr_
Definition thread_specific.h:166
std::atomic< std::uint64_t > gen_
bumped by clear()
Definition thread_specific.h:163
thread_specific(Item init=Item())
Definition thread_specific.h:84
std::size_t size() const
Definition thread_specific.h:124
static void slot_deleter(void *p)
Definition thread_specific.h:154
static void pthread_key_deleter(const pthread_key_t *key_ptr)
Definition thread_specific.h:155
thread_specific & operator=(thread_specific &&)=delete
thread_specific & operator=(const thread_specific &)=delete
std::map< std::thread::id, std::unique_ptr< Item > > items_
Definition thread_specific.h:165
pthread_key_t & key()
Definition thread_specific.h:137
char * p(char *buf, const char *name, int k, int initial_level, double thresh, int order)
Definition derivatives.cc:72
Defines madness::MadnessException for exception handling.
#define MADNESS_EXCEPTION(msg, value)
Macro for throwing a MADNESS exception.
Definition madness_exception.h:119
Definition potentialmanager.cc:41
Namespace for all elements and tools of MADNESS.
Definition DFParameters.h:10
Definition thread_specific.h:132
std::uint64_t gen
Definition thread_specific.h:133
Item * item
Definition thread_specific.h:134