28 #ifndef MPQC_CHEMISTRY_QC_BASIS_INTEGRALENGINEPOOL_HPP_
29 #define MPQC_CHEMISTRY_QC_BASIS_INTEGRALENGINEPOOL_HPP_
32 #include <mpqc/utility/mutex.hpp>
33 #include <chemistry/qc/basis/integral.h>
35 #include <util/misc/scexception.h>
48 template<
typename RefEngType>
51 pthread_key_t engine_key_;
52 RefEngType prototype_;
54 std::vector<void*> tls_ptrs;
58 typedef RefEngType engine_type;
66 if (pthread_key_create(&engine_key_,
nullptr) != 0)
68 "IntegralEnginePool::IntegralEnginePool() "
69 "Unable to register thread local storage key. "
70 "Likely due to a limited number of keys.",
83 for(
void *ptr : tls_ptrs){
84 destroy_thread_object(ptr);
87 pthread_key_delete(engine_key_);
99 RefEngType *RefEngine =
100 reinterpret_cast<RefEngType*>(pthread_getspecific(engine_key_));
102 if (RefEngine ==
nullptr) {
103 RefEngine =
new RefEngType;
108 mutex::global::lock();
109 *RefEngine = prototype_->clone();
110 mutex::global::unlock();
113 pthread_setspecific(engine_key_, RefEngine);
116 tls_ptrs.push_back(pthread_getspecific(engine_key_));
126 static void destroy_thread_object(
void* p) {
127 RefEngType* ptr = reinterpret_cast<RefEngType*>(p);