// This file is distributed under the BSD License. // See "license.txt" for details. // Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com) // and Jason Turner (jason@emptycrate.com) // http://www.chaiscript.com #ifndef CHAISCRIPT_THREADING_HPP_ #define CHAISCRIPT_THREADING_HPP_ #ifndef CHAISCRIPT_NO_THREADS #include #include #else #pragma message ("ChaiScript is compiling without thread safety.") #endif /// \file /// /// This file contains code necessary for thread support in ChaiScript. /// If the compiler definition CHAISCRIPT_NO_THREADS is defined then thread safety /// is disabled in ChaiScript. This has the result that some code is faster, because mutex locks are not required. /// It also has the side effect that the chaiscript::ChaiScript object may not be accessed from more than /// one thread simultaneously. namespace chaiscript { namespace detail { /// If threading is enabled, then this namespace contains std thread classes. /// If threading is not enabled, then stubbed in wrappers that do nothing are provided. /// This allows us to avoid \#ifdef code in the sections that need thread safety. namespace threading { #ifndef CHAISCRIPT_NO_THREADS template class unique_lock : public std::unique_lock { public: unique_lock(T &t) : std::unique_lock(t) {} }; template class shared_lock : public std::unique_lock { public: shared_lock(T &t) : std::unique_lock(t) {} void unlock() {} }; template class lock_guard : public std::lock_guard { public: lock_guard(T &t) : std::lock_guard(t) {} }; class shared_mutex : public std::mutex { }; using std::mutex; using std::recursive_mutex; /// Typesafe thread specific storage. If threading is enabled, this class uses a mutex protected map. If /// threading is not enabled, the class always returns the same data, regardless of which thread it is called from. /// /// \todo move to thread_local when it exists template class Thread_Storage { public: inline T *operator->() const { return get_tls().get(); } inline T &operator*() const { return *get_tls(); } private: std::shared_ptr get_tls() const { unique_lock lock(m_mutex); auto 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 template class unique_lock { public: unique_lock(T &) {} }; template class shared_lock { public: shared_lock(T &) {} void unlock() {} }; template class lock_guard { public: lock_guard(T &) {} }; class shared_mutex { }; class recursive_mutex {}; template class Thread_Storage { public: inline T *operator->() const { return &obj; } inline T &operator*() const { return obj; } private: mutable T obj; }; #endif } } } #endif