Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r85651 - in trunk/boost/interprocess: detail sync/spin
From: igaztanaga_at_[hidden]
Date: 2013-09-11 16:57:21


Author: igaztanaga
Date: 2013-09-11 16:57:21 EDT (Wed, 11 Sep 2013)
New Revision: 85651
URL: http://svn.boost.org/trac/boost/changeset/85651

Log:
Improved spin_wait storing a long system tick variable instead of OS_highres_count_t, which can be quite big on POSIX systems. Removed __int64 constructor and replaced with static_cast to fix MinGW errors.

Text files modified:
   trunk/boost/interprocess/detail/os_thread_functions.hpp | 32 +++++++++++++++++++-------------
   trunk/boost/interprocess/sync/spin/wait.hpp | 37 ++++++++++++++++---------------------
   2 files changed, 35 insertions(+), 34 deletions(-)

Modified: trunk/boost/interprocess/detail/os_thread_functions.hpp
==============================================================================
--- trunk/boost/interprocess/detail/os_thread_functions.hpp Wed Sep 11 14:14:32 2013 (r85650)
+++ trunk/boost/interprocess/detail/os_thread_functions.hpp 2013-09-11 16:57:21 EDT (Wed, 11 Sep 2013) (r85651)
@@ -95,7 +95,7 @@
 
 typedef unsigned __int64 OS_highres_count_t;
 
-inline OS_highres_count_t get_system_tick_in_highres_counts()
+inline unsigned long get_system_tick_in_highres_counts()
 {
    __int64 freq;
    unsigned long curres;
@@ -103,13 +103,13 @@
    //Frequency in counts per second
    if(!winapi::query_performance_frequency(&freq)){
       //Tick resolution in ms
- return (curres-1)/10000u + 1;
+ return (curres-1ul)/10000ul + 1ul;
    }
    else{
       //In femtoseconds
- __int64 count_fs = __int64(1000000000000000LL - 1LL)/freq + 1LL;
- __int64 tick_counts = (__int64(curres)*100000000LL - 1LL)/count_fs + 1LL;
- return static_cast<OS_highres_count_t>(tick_counts);
+ __int64 count_fs = (1000000000000000LL - 1LL)/freq + 1LL;
+ __int64 tick_counts = (static_cast<__int64>(curres)*100000000LL - 1LL)/count_fs + 1LL;
+ return static_cast<unsigned long>(tick_counts);
    }
 }
 
@@ -141,6 +141,9 @@
 inline bool system_highres_count_less(const OS_highres_count_t &l, const OS_highres_count_t &r)
 { return l < r; }
 
+inline bool system_highres_count_less_ul(const OS_highres_count_t &l, unsigned long r)
+{ return l < static_cast<OS_highres_count_t>(r); }
+
 inline void thread_sleep_tick()
 { winapi::sleep_tick(); }
 
@@ -278,18 +281,15 @@
    #endif
 }
 
-inline OS_highres_count_t get_system_tick_in_highres_counts()
+inline unsigned long get_system_tick_in_highres_counts()
 {
    #ifndef BOOST_INTERPROCESS_MATCH_ABSOLUTE_TIME
- struct timespec ts;
- ts.tv_sec = 0;
- ts.tv_nsec = get_system_tick_ns();
- return ts;
+ return get_system_tick_ns();
    #else
    mach_timebase_info_data_t info;
    mach_timebase_info(&info);
             //ns
- return static_cast<OS_highres_count_t>
+ return static_cast<unsigned long>
    (
       static_cast<double>(get_system_tick_ns())
          / (static_cast<double>(info.numer) / info.denom)
@@ -346,7 +346,10 @@
 }
 
 inline bool system_highres_count_less(const OS_highres_count_t &l, const OS_highres_count_t &r)
-{ return l.tv_sec < r.tv_sec || (l.tv_sec == r.tv_sec && l.tv_nsec < r.tv_nsec); }
+{ return l.tv_sec < r.tv_sec || (l.tv_sec == r.tv_sec && l.tv_nsec < r.tv_nsec); }
+
+inline bool system_highres_count_less_ul(const OS_highres_count_t &l, unsigned long r)
+{ return !l.tv_sec && (static_cast<unsigned long>(l.tv_nsec) < r); }
 
 #else
 
@@ -367,7 +370,10 @@
 { return l - r; }
 
 inline bool system_highres_count_less(const OS_highres_count_t &l, const OS_highres_count_t &r)
-{ return l < r; }
+{ return l < r; }
+
+inline bool system_highres_count_less_ul(const OS_highres_count_t &l, unsigned long r)
+{ return l < static_cast<OS_highres_count_t>(r); }
 
 #endif
 

Modified: trunk/boost/interprocess/sync/spin/wait.hpp
==============================================================================
--- trunk/boost/interprocess/sync/spin/wait.hpp Wed Sep 11 14:14:32 2013 (r85650)
+++ trunk/boost/interprocess/sync/spin/wait.hpp 2013-09-11 16:57:21 EDT (Wed, 11 Sep 2013) (r85651)
@@ -1,7 +1,7 @@
 //////////////////////////////////////////////////////////////////////////////
 //
 // (C) Copyright Peter Dimov 2008.
-// (C) Copyright Ion Gaztanaga 2013. Distributed under the Boost
+// (C) Copyright Ion Gaztanaga 2013-2013. Distributed under the Boost
 // Software License, Version 1.0. (See accompanying file
 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
 //
@@ -73,13 +73,11 @@
 class spin_wait
 {
    public:
+
+ static const unsigned int nop_pause_limit = 32u;
    spin_wait()
- : m_k(0u)
- {
- (void)m_nop_pause_limit;
- (void)m_yield_only_counts;
- (void)m_count_start;
- }
+ : m_count_start(), m_ul_yield_only_counts(), m_k()
+ {}
 
    #ifdef BOOST_INTERPROCESS_SPIN_WAIT_DEBUG
    ~spin_wait()
@@ -101,19 +99,19 @@
          this->init_limits();
       }
       //Nop tries
- if( m_k < (m_nop_pause_limit >> 2) ){
+ if( m_k < (nop_pause_limit >> 2) ){
 
       }
       //Pause tries if the processor supports it
       #if defined(BOOST_INTERPROCESS_SMT_PAUSE)
- else if( m_k < m_nop_pause_limit ){
+ else if( m_k < nop_pause_limit ){
          BOOST_INTERPROCESS_SMT_PAUSE
       }
       #endif
       //Yield/Sleep strategy
       else{
          //Lazy initialization of tick information
- if(m_k == m_nop_pause_limit){
+ if(m_k == nop_pause_limit){
             this->init_tick_info();
          }
          else if( this->yield_or_sleep() ){
@@ -129,7 +127,6 @@
    void reset()
    {
       m_k = 0u;
- m_count_start = ipcdetail::get_current_system_highres_count();
    }
 
    private:
@@ -137,45 +134,43 @@
    void init_limits()
    {
       unsigned int num_cores = ipcdetail::num_core_holder<0>::get();
- m_nop_pause_limit = num_cores > 1u ? 32u : 0u;
+ m_k = num_cores > 1u ? 0u : nop_pause_limit;
    }
 
    void init_tick_info()
    {
- m_yield_only_counts = ipcdetail::get_system_tick_in_highres_counts();
+ m_ul_yield_only_counts = ipcdetail::get_system_tick_in_highres_counts();
       m_count_start = ipcdetail::get_current_system_highres_count();
    }
 
    //Returns true if yield must be called, false is sleep must be called
    bool yield_or_sleep()
    {
- if(ipcdetail::is_highres_count_zero(m_yield_only_counts)){ //If yield-only limit was reached then yield one in every two tries
+ if(!m_ul_yield_only_counts){ //If yield-only limit was reached then yield one in every two tries
          return (m_k & 1u) != 0;
       }
       else{ //Try to see if we've reched yield-only time limit
          const ipcdetail::OS_highres_count_t now = ipcdetail::get_current_system_highres_count();
          const ipcdetail::OS_highres_count_t elapsed = ipcdetail::system_highres_count_subtract(now, m_count_start);
- if(!ipcdetail::system_highres_count_less(elapsed, m_yield_only_counts)){
+ if(!ipcdetail::system_highres_count_less_ul(elapsed, m_ul_yield_only_counts)){
             #ifdef BOOST_INTERPROCESS_SPIN_WAIT_DEBUG
             std::cout << "elapsed!\n"
- << " m_yield_only_counts: ";
- ipcdetail::ostream_highres_count(std::cout, m_yield_only_counts)
+ << " m_ul_yield_only_counts: " << m_ul_yield_only_counts
                      << " system tick(us): " << ipcdetail::get_system_tick_us() << '\n'
                       << " m_k: " << m_k << " elapsed counts: ";
                      ipcdetail::ostream_highres_count(std::cout, elapsed) << std::endl;
             #endif
             //Yield-only time reached, now it's time to sleep
- ipcdetail::zero_highres_count(m_yield_only_counts);
+ m_ul_yield_only_counts = 0ul;
             return false;
          }
       }
       return true; //Otherwise yield
    }
 
- unsigned int m_k;
- unsigned int m_nop_pause_limit;
- ipcdetail::OS_highres_count_t m_yield_only_counts;
    ipcdetail::OS_highres_count_t m_count_start;
+ unsigned long m_ul_yield_only_counts;
+ unsigned int m_k;
 };
 
 } // namespace interprocess


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk