From 5efdcdff99729587e90454b51d492359aea538e1 Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Sun, 11 Sep 2011 09:19:06 -0600 Subject: [PATCH] Remove need for boost::thread_local_ptr --- include/chaiscript/chaiscript_threading.hpp | 32 +++++++++++++------ .../dispatchkit/proxy_functions.hpp | 1 + 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/include/chaiscript/chaiscript_threading.hpp b/include/chaiscript/chaiscript_threading.hpp index a8d8d8c..bc7ae3c 100644 --- a/include/chaiscript/chaiscript_threading.hpp +++ b/include/chaiscript/chaiscript_threading.hpp @@ -10,7 +10,6 @@ #ifndef CHAISCRIPT_NO_THREADS #include #include -#include #else #pragma message ("ChaiScript is compiling without thread safety.") #endif @@ -59,6 +58,8 @@ namespace chaiscript class shared_mutex : public std::mutex { }; + using std::mutex; + using std::recursive_mutex; @@ -71,26 +72,37 @@ namespace chaiscript public: ~Thread_Storage() { - m_thread_storage.reset(); } inline T *operator->() const { - if (!m_thread_storage.get()) - { - m_thread_storage.reset(new T()); - } - - return m_thread_storage.get(); + return get_tls().get(); } inline T &operator*() const { - return *(this->operator->()); + return *get_tls(); } + private: - mutable boost::thread_specific_ptr m_thread_storage; + std::shared_ptr get_tls() const + { + unique_lock lock(m_mutex); + + typename std::map >::iterator itr = m_instances.find(std::this_thread::get_id()); + + if (itr != m_instances.end()) { return itr->second; } + + std::shared_ptr new_instance(new T()); + + m_instances.insert(std::make_pair(std::this_thread::get_id(), new_instance)); + + return new_instance; + } + + mutable mutex m_mutex; + mutable std::map > m_instances; }; #else diff --git a/include/chaiscript/dispatchkit/proxy_functions.hpp b/include/chaiscript/dispatchkit/proxy_functions.hpp index f25b43b..5225121 100644 --- a/include/chaiscript/dispatchkit/proxy_functions.hpp +++ b/include/chaiscript/dispatchkit/proxy_functions.hpp @@ -15,6 +15,7 @@ #include #include #include +#include #include "proxy_functions_detail.hpp" namespace chaiscript