Remove need for boost::thread_local_ptr

This commit is contained in:
Jason Turner 2011-09-11 09:19:06 -06:00
parent cd97880d70
commit 5efdcdff99
2 changed files with 23 additions and 10 deletions

View File

@ -10,7 +10,6 @@
#ifndef CHAISCRIPT_NO_THREADS #ifndef CHAISCRIPT_NO_THREADS
#include <thread> #include <thread>
#include <mutex> #include <mutex>
#include <boost/thread/tss.hpp>
#else #else
#pragma message ("ChaiScript is compiling without thread safety.") #pragma message ("ChaiScript is compiling without thread safety.")
#endif #endif
@ -59,6 +58,8 @@ namespace chaiscript
class shared_mutex : public std::mutex { }; class shared_mutex : public std::mutex { };
using std::mutex;
using std::recursive_mutex; using std::recursive_mutex;
@ -71,26 +72,37 @@ namespace chaiscript
public: public:
~Thread_Storage() ~Thread_Storage()
{ {
m_thread_storage.reset();
} }
inline T *operator->() const inline T *operator->() const
{ {
if (!m_thread_storage.get()) return get_tls().get();
{
m_thread_storage.reset(new T());
}
return m_thread_storage.get();
} }
inline T &operator*() const inline T &operator*() const
{ {
return *(this->operator->()); return *get_tls();
} }
private: private:
mutable boost::thread_specific_ptr<T> m_thread_storage; std::shared_ptr<T> get_tls() const
{
unique_lock<mutex> lock(m_mutex);
typename std::map<std::thread::id, std::shared_ptr<T> >::iterator itr = m_instances.find(std::this_thread::get_id());
if (itr != m_instances.end()) { return itr->second; }
std::shared_ptr<T> 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<std::thread::id, std::shared_ptr<T> > m_instances;
}; };
#else #else

View File

@ -15,6 +15,7 @@
#include <type_traits> #include <type_traits>
#include <stdexcept> #include <stdexcept>
#include <vector> #include <vector>
#include <cassert>
#include "proxy_functions_detail.hpp" #include "proxy_functions_detail.hpp"
namespace chaiscript namespace chaiscript