Refactor yield_k.hpp

This commit is contained in:
Peter Dimov
2020-06-07 20:40:41 +03:00
parent 3db4ad9a15
commit 5d31c1c443

View File

@@ -24,6 +24,7 @@
// //
#include <boost/config.hpp> #include <boost/config.hpp>
#include <boost/config/pragma_message.hpp>
// BOOST_SMT_PAUSE // BOOST_SMT_PAUSE
@@ -37,6 +38,10 @@ extern "C" void _mm_pause();
#define BOOST_SMT_PAUSE __asm__ __volatile__( "rep; nop" : : : "memory" ); #define BOOST_SMT_PAUSE __asm__ __volatile__( "rep; nop" : : : "memory" );
#else
#define BOOST_SMT_PAUSE
#endif #endif
// //
@@ -72,22 +77,18 @@ namespace detail
#endif // !defined( BOOST_USE_WINDOWS_H ) #endif // !defined( BOOST_USE_WINDOWS_H )
inline void yield( unsigned k ) BOOST_NOEXCEPT #if defined(BOOST_SP_REPORT_IMPLEMENTATION)
{ BOOST_PRAGMA_MESSAGE("Using Win32 yield_k")
if( k < 16 )
{
#if defined( BOOST_SMT_PAUSE )
BOOST_SMT_PAUSE
#endif #endif
}
else if( k < 32 ) inline void sp_thread_yield()
{ {
Sleep( 0 ); Sleep( 0 );
} }
else
{ inline void sp_thread_sleep()
Sleep( 1 ); {
} Sleep( 1 );
} }
} // namespace detail } // namespace detail
@@ -111,31 +112,27 @@ namespace boost
namespace detail namespace detail
{ {
inline void yield( unsigned k ) #if defined(BOOST_SP_REPORT_IMPLEMENTATION)
{ BOOST_PRAGMA_MESSAGE("Using POSIX yield_k")
if( k < 16 )
{
#if defined( BOOST_SMT_PAUSE )
BOOST_SMT_PAUSE
#endif #endif
}
else if( k < 32 || k & 1 )
{
sched_yield();
}
else
{
// g++ -Wextra warns on {} or {0}
struct timespec rqtp = { 0, 0 };
// POSIX says that timespec has tv_sec and tv_nsec inline void sp_thread_yield()
// But it doesn't guarantee order or placement {
sched_yield();
}
rqtp.tv_sec = 0; inline void sp_thread_sleep()
rqtp.tv_nsec = 1000; {
// g++ -Wextra warns on {} or {0}
struct timespec rqtp = { 0, 0 };
nanosleep( &rqtp, 0 ); // POSIX says that timespec has tv_sec and tv_nsec
} // But it doesn't guarantee order or placement
rqtp.tv_sec = 0;
rqtp.tv_nsec = 1000;
nanosleep( &rqtp, 0 );
} }
} // namespace detail } // namespace detail
@@ -144,14 +141,24 @@ inline void yield( unsigned k )
#else #else
#if defined(BOOST_SP_REPORT_IMPLEMENTATION)
BOOST_PRAGMA_MESSAGE("Using empty yield_k")
#endif
namespace boost namespace boost
{ {
namespace detail namespace detail
{ {
inline void yield( unsigned ) inline void sp_thread_yield()
{ {
BOOST_SMT_PAUSE
}
inline void sp_thread_sleep()
{
BOOST_SMT_PAUSE
} }
} // namespace detail } // namespace detail
@@ -160,4 +167,30 @@ inline void yield( unsigned )
#endif #endif
namespace boost
{
namespace detail
{
inline void yield( unsigned k ) BOOST_NOEXCEPT
{
if( k < 16 )
{
BOOST_SMT_PAUSE
}
else if( k < 32 )
{
sp_thread_yield();
}
else
{
sp_thread_sleep();
}
}
} // namespace detail
} // namespace boost
#endif // #ifndef BOOST_SMART_PTR_DETAIL_YIELD_K_HPP_INCLUDED #endif // #ifndef BOOST_SMART_PTR_DETAIL_YIELD_K_HPP_INCLUDED