Index: boost/thread/detail/thread.hpp =================================================================== --- boost/thread/detail/thread.hpp (revision 395) +++ boost/thread/detail/thread.hpp (working copy) @@ -27,6 +27,12 @@ #include +/** Enabling the boost thread extension, will enable a cached version of + * boost::this_thread::get_id() and the retrieval of the main thread::id + * via boost::main_thread::get_id(), both base on thread_ext.hpp. + */ +#define BOOST_THREAD_EXT + #ifdef BOOST_MSVC #pragma warning(push) #pragma warning(disable:4251) @@ -374,7 +380,11 @@ namespace this_thread { +#ifndef BOOST_THREAD_EXT thread::id BOOST_THREAD_DECL get_id(); +#else + struct handler; +#endif void BOOST_THREAD_DECL interruption_point(); bool BOOST_THREAD_DECL interruption_enabled(); @@ -395,7 +405,12 @@ thread_data(thread_data_) {} friend class thread; + +#ifndef BOOST_THREAD_EXT friend id BOOST_THREAD_DECL this_thread::get_id(); +#else + friend struct this_thread::handler; +#endif public: id(): thread_data() @@ -523,6 +538,10 @@ } } +#ifdef BOOST_THREAD_EXT +#include +#endif + #ifdef BOOST_MSVC #pragma warning(pop) #endif Index: boost/thread/detail/thread_ext.hpp =================================================================== --- boost/thread/detail/thread_ext.hpp (revision 0) +++ boost/thread/detail/thread_ext.hpp (revision 0) @@ -0,0 +1,54 @@ +/** + * @note Copyright\n + * ADOxx for Windows and Linux\n + * (C) COPYRIGHT BOC - Business Objectives Consulting 1996-2011\n + * All Rights Reserved\n + * Use, duplication or disclosure restricted by BOC Information Systems\n + * Vienna, 2011 + * @brief + * @author iloehken, 2011 + * + */ + +#ifdef _MSC_VER +#pragma once +#endif + +#ifndef THREAD_THREAD_EXT_HPP_ +#define THREAD_THREAD_EXT_HPP_ + +namespace boost { namespace this_thread +{ + //-------------------------------------------------------------------- + struct BOOST_THREAD_DECL handler + //-------------------------------------------------------------------- + { + private: + static thread::id const make_id(); // called once by get_id to initialize caching and abstract from platform + + public: + static thread::id const& get_id(); + static thread::id const& get_main_id(); + }; + + //-------------------------------------------------------------------- + inline thread::id const& + get_id() + //-------------------------------------------------------------------- + { + return handler::get_id(); + } +} // namespace this_thread + +namespace main_thread +{ + //-------------------------------------------------------------------- + inline thread::id const& + get_id() + //-------------------------------------------------------------------- + { + return this_thread::handler::get_main_id(); + } +}} // namespace boost::main_thread + +#endif Index: libs/thread/src/pthread/thread.cpp =================================================================== --- libs/thread/src/pthread/thread.cpp (revision 395) +++ libs/thread/src/pthread/thread.cpp (working copy) @@ -449,7 +449,11 @@ namespace this_thread { +#ifndef BOOST_THREAD_EXT thread::id get_id() +#else + boost::thread::id const handler::make_id() +#endif { boost::detail::thread_data_base* const thread_info=get_or_make_current_thread_data(); return thread::id(thread_info?thread_info->shared_from_this():detail::thread_data_ptr()); @@ -600,3 +604,7 @@ } + +#ifdef BOOST_THREAD_EXT +#include "../thread_ext.inl" +#endif Index: libs/thread/src/thread_ext.inl =================================================================== --- libs/thread/src/thread_ext.inl (revision 0) +++ libs/thread/src/thread_ext.inl (revision 0) @@ -0,0 +1,69 @@ +/** + * @note Copyright\n + * ADOxx for Windows and Linux\n + * (C) COPYRIGHT BOC - Business Objectives Consulting 1996-2011\n + * All Rights Reserved\n + * Use, duplication or disclosure restricted by BOC Information Systems\n + * Vienna, 2011 + * @brief + * @author iloehken, 2011 + * + */ + +#include + +namespace boost { namespace this_thread { + +//-------------------------------------------------------------------- +boost::thread::id const& +handler::get_id() +//-------------------------------------------------------------------- +{ + /** ILO : 24/02/2011 + * Injected code, that stores the thread id in thread specific_ptr + * to speed up access, because without caching the retrieval of the + * current thread id is really slow (60 times faster with caching) + */ + static boost::thread_specific_ptr aCachedID; + + if(boost::thread::id* pThreadID = aCachedID.get()) + { + return *pThreadID; + } + + aCachedID.reset(new thread::id(handler::make_id())); + return *aCachedID.get(); +} + +namespace { + +/** ILO : 24/02/2011 + * + * Simple Holder for MainThreadID used with recurring static + * initialization scheme to determine the main thread in a safe way. + */ +//-------------------------------------------------------------------- +struct main_id +//-------------------------------------------------------------------- +{ + boost::thread::id const m_id;; + + //-------------------------------------------------------------------- + explicit main_id() + : m_id(boost::this_thread::get_id()) + //-------------------------------------------------------------------- + {} +}; + +} // namespace anonymous + +//-------------------------------------------------------------------- +boost::thread::id const& +handler::get_main_id() +//-------------------------------------------------------------------- +{ + return boost::details::pool::singleton_default::instance().m_id; +} + +}} + Index: libs/thread/src/win32/thread.cpp =================================================================== --- libs/thread/src/win32/thread.cpp (revision 395) +++ libs/thread/src/win32/thread.cpp (working copy) @@ -482,7 +482,11 @@ return false; } +#ifndef BOOST_THREAD_EXT thread::id get_id() +#else + boost::thread::id const handler::make_id() +#endif { return thread::id(get_or_make_current_thread_data()); } @@ -621,4 +625,6 @@ } - +#ifdef BOOST_THREAD_EXT +#include "../thread_ext.inl" +#endif