Merging latest spin_wait improvements + unused typedef removal.

[SVN r85653]
This commit is contained in:
Ion Gaztañaga
2013-09-11 21:02:47 +00:00
parent e559ac3275
commit 762aaea036
7 changed files with 35 additions and 52 deletions

View File

@@ -95,7 +95,7 @@ inline unsigned long get_system_tick_us()
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 @@ inline OS_highres_count_t get_system_tick_in_highres_counts()
//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 OS_highres_count_t system_highres_count_subtract(const OS_highres_count_t
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 @@ inline unsigned long get_system_tick_ns()
#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 OS_highres_count_t system_highres_count_subtract(const OS_highres_count_t
}
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 @@ inline OS_highres_count_t system_highres_count_subtract(const OS_highres_count_t
{ 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

View File

@@ -847,7 +847,6 @@ class segment_manager
{
(void)is_intrusive;
typedef IndexType<ipcdetail::index_config<CharT, MemoryAlgorithm> > index_type;
typedef ipcdetail::index_key<CharT, void_pointer> index_key_t;
typedef typename index_type::iterator index_it;
//-------------------------------
@@ -949,7 +948,6 @@ class segment_manager
{
(void)is_intrusive_index;
typedef IndexType<ipcdetail::index_config<CharT, MemoryAlgorithm> > index_type;
typedef ipcdetail::index_key<CharT, void_pointer> index_key_t;
typedef typename index_type::iterator index_it;
typedef typename index_type::value_type intrusive_value_type;

View File

@@ -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 @@ unsigned int num_core_holder<Dummy>::num_cores = ipcdetail::get_num_cores();
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 @@ class spin_wait
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 @@ class spin_wait
void reset()
{
m_k = 0u;
m_count_start = ipcdetail::get_current_system_highres_count();
}
private:
@@ -137,45 +134,43 @@ class spin_wait
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

View File

@@ -48,8 +48,6 @@ bool CheckEqualContainers(MyShmCont *shmcont, MyStdCont *stdcont)
if(shmcont->size() != stdcont->size())
return false;
typedef typename MyShmCont::value_type value_type;
typename MyShmCont::iterator itshm(shmcont->begin()), itshmend(shmcont->end());
typename MyStdCont::iterator itstd(stdcont->begin());
typename MyStdCont::size_type dist = (typename MyStdCont::size_type)std::distance(itshm, itshmend);

View File

@@ -47,7 +47,6 @@ int main ()
const char *allocName = "testAllocation";
typedef boost::interprocess::vector<int, shmem_allocator_int_t > MyVect;
typedef boost::interprocess::list<int, shmem_allocator_int_t > MyList;
//---- ALLOC, NAMED_ALLOC, NAMED_NEW TEST ----//
{

View File

@@ -100,7 +100,6 @@ template<class ManagedMemory>
bool test_named_iterators(ManagedMemory &m)
{
typedef typename ManagedMemory::char_type char_type;
typedef std::char_traits<char_type> char_traits_type;
std::vector<char*> buffers;
const int BufferLen = 100;
char_type name[BufferLen];
@@ -172,7 +171,6 @@ template<class ManagedMemory>
bool test_shrink_to_fit(ManagedMemory &m)
{
typedef typename ManagedMemory::char_type char_type;
typedef std::char_traits<char_type> char_traits_type;
std::vector<char*> buffers;
const int BufferLen = 100;
char_type name[BufferLen];
@@ -214,7 +212,6 @@ template<class ManagedMemory>
bool test_direct_named_allocation_destruction(ManagedMemory &m)
{
typedef typename ManagedMemory::char_type char_type;
typedef std::char_traits<char_type> char_traits_type;
std::vector<char*> buffers;
const int BufferLen = 100;
char_type name[BufferLen];
@@ -255,7 +252,6 @@ template<class ManagedMemory>
bool test_named_allocation_inverse_destruction(ManagedMemory &m)
{
typedef typename ManagedMemory::char_type char_type;
typedef std::char_traits<char_type> char_traits_type;
std::vector<char*> buffers;
const int BufferLen = 100;
@@ -295,7 +291,6 @@ template<class ManagedMemory>
bool test_named_allocation_mixed_destruction(ManagedMemory &m)
{
typedef typename ManagedMemory::char_type char_type;
typedef std::char_traits<char_type> char_traits_type;
std::vector<char*> buffers;
const int BufferLen = 100;
@@ -337,7 +332,6 @@ template<class ManagedMemory>
bool test_inverse_named_allocation_destruction(ManagedMemory &m)
{
typedef typename ManagedMemory::char_type char_type;
typedef std::char_traits<char_type> char_traits_type;
std::vector<char*> buffers;
const int BufferLen = 100;

View File

@@ -53,7 +53,6 @@ int simple_test()
typedef deleter<base_class, managed_shared_memory::segment_manager>
base_deleter_t;
typedef shared_ptr<base_class, base_class_allocator, base_deleter_t> base_shared_ptr;
typedef weak_ptr<base_class, base_class_allocator, base_deleter_t> base_weak_ptr;
std::string process_name;
test::get_process_id_name(process_name);
@@ -561,17 +560,11 @@ void test_alias()
typedef allocator<void, managed_shared_memory::segment_manager>
v_allocator_t;
typedef deleter<alias_tester, managed_shared_memory::segment_manager>
alias_tester_deleter_t;
typedef deleter<int, managed_shared_memory::segment_manager>
int_deleter_t;
typedef shared_ptr<alias_tester, v_allocator_t, alias_tester_deleter_t> alias_tester_shared_ptr;
typedef shared_ptr<int, v_allocator_t, int_deleter_t> int_shared_ptr;
typedef shared_ptr<const int, v_allocator_t, int_deleter_t> const_int_shared_ptr;
typedef shared_ptr<volatile int, v_allocator_t, int_deleter_t> volatile_int_shared_ptr;
std::string process_name;
test::get_process_id_name(process_name);