Ticket #5173: thread_ext.patch

File thread_ext.patch, 6.6 KB (added by anonymous, 12 years ago)

proposed patch to speedup access

  • boost/thread/detail/thread.hpp

     
    2727
    2828#include <boost/config/abi_prefix.hpp>
    2929
     30/** Enabling the boost thread extension, will enable a cached version of
     31 *  boost::this_thread::get_id() and the retrieval of the main thread::id
     32 *  via boost::main_thread::get_id(), both base on thread_ext.hpp.
     33 */
     34#define BOOST_THREAD_EXT
     35
    3036#ifdef BOOST_MSVC
    3137#pragma warning(push)
    3238#pragma warning(disable:4251)
     
    374380
    375381    namespace this_thread
    376382    {
     383#ifndef BOOST_THREAD_EXT
    377384        thread::id BOOST_THREAD_DECL get_id();
     385#else
     386        struct handler;
     387#endif
    378388
    379389        void BOOST_THREAD_DECL interruption_point();
    380390        bool BOOST_THREAD_DECL interruption_enabled();
     
    395405            thread_data(thread_data_)
    396406        {}
    397407        friend class thread;
     408
     409#ifndef BOOST_THREAD_EXT
    398410        friend id BOOST_THREAD_DECL this_thread::get_id();
     411#else
     412        friend struct this_thread::handler;
     413#endif
    399414    public:
    400415        id():
    401416            thread_data()
     
    523538    }
    524539}
    525540
     541#ifdef BOOST_THREAD_EXT
     542#include <boost/thread/detail/thread_ext.hpp>
     543#endif
     544
    526545#ifdef BOOST_MSVC
    527546#pragma warning(pop)
    528547#endif
  • boost/thread/detail/thread_ext.hpp

     
     1/**
     2 * @note Copyright\n
     3 *     ADOxx for Windows and Linux\n
     4 *      (C) COPYRIGHT BOC - Business Objectives Consulting 1996-2011\n
     5 *      All Rights Reserved\n
     6 *      Use, duplication or disclosure restricted by BOC Information Systems\n
     7 *      Vienna, 2011
     8 * @brief
     9 * @author iloehken, 2011
     10 *
     11 */
     12
     13#ifdef _MSC_VER
     14#pragma once
     15#endif
     16
     17#ifndef THREAD_THREAD_EXT_HPP_
     18#define THREAD_THREAD_EXT_HPP_
     19
     20namespace boost { namespace this_thread
     21{
     22  //--------------------------------------------------------------------
     23  struct BOOST_THREAD_DECL handler
     24  //--------------------------------------------------------------------
     25  {
     26  private:
     27    static thread::id const make_id(); // called once by get_id to initialize caching and abstract from platform
     28
     29  public:
     30    static thread::id const& get_id();
     31    static thread::id const& get_main_id();
     32  };
     33
     34  //--------------------------------------------------------------------
     35  inline thread::id const&
     36  get_id()
     37  //--------------------------------------------------------------------
     38  {
     39    return handler::get_id();
     40  }
     41} // namespace this_thread
     42
     43namespace main_thread
     44{
     45  //--------------------------------------------------------------------
     46  inline thread::id const&
     47  get_id()
     48  //--------------------------------------------------------------------
     49  {
     50    return this_thread::handler::get_main_id();
     51  }
     52}} // namespace boost::main_thread
     53
     54#endif
  • libs/thread/src/pthread/thread.cpp

     
    449449
    450450    namespace this_thread
    451451    {
     452#ifndef BOOST_THREAD_EXT
    452453        thread::id get_id()
     454#else
     455        boost::thread::id const handler::make_id()
     456#endif
    453457        {
    454458            boost::detail::thread_data_base* const thread_info=get_or_make_current_thread_data();
    455459            return thread::id(thread_info?thread_info->shared_from_this():detail::thread_data_ptr());
     
    600604
    601605
    602606}
     607
     608#ifdef BOOST_THREAD_EXT
     609#include "../thread_ext.inl"
     610#endif
  • libs/thread/src/thread_ext.inl

     
     1/**
     2 * @note Copyright\n
     3 *     ADOxx for Windows and Linux\n
     4 *      (C) COPYRIGHT BOC - Business Objectives Consulting 1996-2011\n
     5 *      All Rights Reserved\n
     6 *      Use, duplication or disclosure restricted by BOC Information Systems\n
     7 *      Vienna, 2011
     8 * @brief
     9 * @author iloehken, 2011
     10 *
     11 */
     12
     13#include <boost/pool/detail/singleton.hpp>
     14
     15namespace boost { namespace this_thread {
     16
     17//--------------------------------------------------------------------
     18boost::thread::id const&
     19handler::get_id()
     20//--------------------------------------------------------------------
     21{
     22  /** ILO : 24/02/2011
     23   *  Injected code, that stores the thread id in thread specific_ptr
     24   *  to speed up access, because without caching the retrieval of the
     25   *  current thread id is really slow (60 times faster with caching)
     26   */
     27  static boost::thread_specific_ptr<thread::id> aCachedID;
     28
     29  if(boost::thread::id* pThreadID = aCachedID.get())
     30  {
     31    return *pThreadID;
     32  }
     33
     34  aCachedID.reset(new thread::id(handler::make_id()));
     35  return *aCachedID.get();
     36}
     37
     38namespace {
     39
     40/** ILO : 24/02/2011
     41 *
     42 *  Simple Holder for MainThreadID used with recurring static
     43 *  initialization scheme to determine the main thread in a safe way.
     44 */
     45//--------------------------------------------------------------------
     46struct main_id
     47//--------------------------------------------------------------------
     48{
     49  boost::thread::id const m_id;;
     50
     51  //--------------------------------------------------------------------
     52  explicit main_id()
     53    : m_id(boost::this_thread::get_id())
     54  //--------------------------------------------------------------------
     55  {}
     56};
     57
     58} // namespace anonymous
     59
     60//--------------------------------------------------------------------
     61boost::thread::id const&
     62handler::get_main_id()
     63//--------------------------------------------------------------------
     64{
     65  return boost::details::pool::singleton_default<main_id>::instance().m_id;
     66}
     67
     68}}
     69
  • libs/thread/src/win32/thread.cpp

     
    482482            return false;
    483483        }
    484484
     485#ifndef BOOST_THREAD_EXT
    485486        thread::id get_id()
     487#else
     488        boost::thread::id const handler::make_id()
     489#endif
    486490        {
    487491            return thread::id(get_or_make_current_thread_data());
    488492        }
     
    621625
    622626}
    623627
    624 
     628#ifdef BOOST_THREAD_EXT
     629#include "../thread_ext.inl"
     630#endif