yield_k.hpp
Go to the documentation of this file.
00001 #ifndef BOOST_SMART_PTR_DETAIL_YIELD_K_HPP_INCLUDED
00002 #define BOOST_SMART_PTR_DETAIL_YIELD_K_HPP_INCLUDED
00003 
00004 // MS compatible compilers support #pragma once
00005 
00006 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
00007 # pragma once
00008 #endif
00009 
00010 //
00011 //  yield_k.hpp
00012 //
00013 //  Copyright (c) 2008 Peter Dimov
00014 //
00015 //  void yield( unsigned k );
00016 //
00017 //  Typical use:
00018 //
00019 //  for( unsigned k = 0; !try_lock(); ++k ) yield( k );
00020 //
00021 //  Distributed under the Boost Software License, Version 1.0.
00022 //  See accompanying file LICENSE_1_0.txt or copy at
00023 //  http://www.boost.org/LICENSE_1_0.txt
00024 //
00025 
00026 #include <boost/config.hpp>
00027 
00028 // BOOST_SMT_PAUSE
00029 
00030 #if defined(_MSC_VER) && _MSC_VER >= 1310 && ( defined(_M_IX86) || defined(_M_X64) )
00031 
00032 extern "C" void _mm_pause();
00033 #pragma intrinsic( _mm_pause )
00034 
00035 #define BOOST_SMT_PAUSE _mm_pause();
00036 
00037 #elif defined(__GNUC__) && ( defined(__i386__) || defined(__x86_64__) )
00038 
00039 #define BOOST_SMT_PAUSE __asm__ __volatile__( "rep; nop" : : : "memory" );
00040 
00041 #endif
00042 
00043 //
00044 
00045 #if defined( WIN32 ) || defined( _WIN32 ) || defined( __WIN32__ ) || defined( __CYGWIN__ )
00046 
00047 #if defined( BOOST_USE_WINDOWS_H )
00048 # include <windows.h>
00049 #endif
00050 
00051 namespace boost
00052 {
00053 
00054 namespace detail
00055 {
00056 
00057 #if !defined( BOOST_USE_WINDOWS_H )
00058   extern "C" void __stdcall Sleep( unsigned ms );
00059 #endif
00060 
00061 inline void yield( unsigned k )
00062 {
00063     if( k < 4 )
00064     {
00065     }
00066 #if defined( BOOST_SMT_PAUSE )
00067     else if( k < 16 )
00068     {
00069         BOOST_SMT_PAUSE
00070     }
00071 #endif
00072     else if( k < 32 )
00073     {
00074         Sleep( 0 );
00075     }
00076     else
00077     {
00078         Sleep( 1 );
00079     }
00080 }
00081 
00082 } // namespace detail
00083 
00084 } // namespace boost
00085 
00086 #elif defined( BOOST_HAS_PTHREADS )
00087 
00088 #include <sched.h>
00089 #include <time.h>
00090 
00091 namespace boost
00092 {
00093 
00094 namespace detail
00095 {
00096 
00097 inline void yield( unsigned k )
00098 {
00099     if( k < 4 )
00100     {
00101     }
00102 #if defined( BOOST_SMT_PAUSE )
00103     else if( k < 16 )
00104     {
00105         BOOST_SMT_PAUSE
00106     }
00107 #endif
00108     else if( k < 32 || k & 1 )
00109     {
00110         sched_yield();
00111     }
00112     else
00113     {
00114         // g++ -Wextra warns on {} or {0}
00115         struct timespec rqtp = { 0, 0 };
00116 
00117         // POSIX says that timespec has tv_sec and tv_nsec
00118         // But it doesn't guarantee order or placement
00119 
00120         rqtp.tv_sec = 0;
00121         rqtp.tv_nsec = 1000;
00122 
00123         nanosleep( &rqtp, 0 );
00124     }
00125 }
00126 
00127 } // namespace detail
00128 
00129 } // namespace boost
00130 
00131 #else
00132 
00133 namespace boost
00134 {
00135 
00136 namespace detail
00137 {
00138 
00139 inline void yield( unsigned )
00140 {
00141 }
00142 
00143 } // namespace detail
00144 
00145 } // namespace boost
00146 
00147 #endif
00148 
00149 #endif // #ifndef BOOST_SMART_PTR_DETAIL_YIELD_K_HPP_INCLUDED


appl
Author(s): petercai
autogenerated on Tue Jan 7 2014 11:02:29