Pack required boost code together.
[xy_vsfilter.git] / src / thirdparty / boost_1_47_0 / boost / smart_ptr / detail / yield_k.hpp
blob4f174bb31b92ce91c80381258da7e4a816b28d2b
1 #ifndef BOOST_SMART_PTR_DETAIL_YIELD_K_HPP_INCLUDED
2 #define BOOST_SMART_PTR_DETAIL_YIELD_K_HPP_INCLUDED
4 // MS compatible compilers support #pragma once
6 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
7 # pragma once
8 #endif
11 // yield_k.hpp
13 // Copyright (c) 2008 Peter Dimov
15 // void yield( unsigned k );
17 // Typical use:
19 // for( unsigned k = 0; !try_lock(); ++k ) yield( k );
21 // Distributed under the Boost Software License, Version 1.0.
22 // See accompanying file LICENSE_1_0.txt or copy at
23 // http://www.boost.org/LICENSE_1_0.txt
26 #include <boost/config.hpp>
28 // BOOST_SMT_PAUSE
30 #if defined(_MSC_VER) && _MSC_VER >= 1310 && ( defined(_M_IX86) || defined(_M_X64) )
32 extern "C" void _mm_pause();
33 #pragma intrinsic( _mm_pause )
35 #define BOOST_SMT_PAUSE _mm_pause();
37 #elif defined(__GNUC__) && ( defined(__i386__) || defined(__x86_64__) )
39 #define BOOST_SMT_PAUSE __asm__ __volatile__( "rep; nop" : : : "memory" );
41 #endif
45 #if defined( WIN32 ) || defined( _WIN32 ) || defined( __WIN32__ ) || defined( __CYGWIN__ )
47 #if defined( BOOST_USE_WINDOWS_H )
48 # include <windows.h>
49 #endif
51 namespace boost
54 namespace detail
57 #if !defined( BOOST_USE_WINDOWS_H )
58 extern "C" void __stdcall Sleep( unsigned long ms );
59 #endif
61 inline void yield( unsigned k )
63 if( k < 4 )
66 #if defined( BOOST_SMT_PAUSE )
67 else if( k < 16 )
69 BOOST_SMT_PAUSE
71 #endif
72 else if( k < 32 )
74 Sleep( 0 );
76 else
78 Sleep( 1 );
82 } // namespace detail
84 } // namespace boost
86 #elif defined( BOOST_HAS_PTHREADS )
88 #include <sched.h>
89 #include <time.h>
91 namespace boost
94 namespace detail
97 inline void yield( unsigned k )
99 if( k < 4 )
102 #if defined( BOOST_SMT_PAUSE )
103 else if( k < 16 )
105 BOOST_SMT_PAUSE
107 #endif
108 else if( k < 32 || k & 1 )
110 sched_yield();
112 else
114 // g++ -Wextra warns on {} or {0}
115 struct timespec rqtp = { 0, 0 };
117 // POSIX says that timespec has tv_sec and tv_nsec
118 // But it doesn't guarantee order or placement
120 rqtp.tv_sec = 0;
121 rqtp.tv_nsec = 1000;
123 nanosleep( &rqtp, 0 );
127 } // namespace detail
129 } // namespace boost
131 #else
133 namespace boost
136 namespace detail
139 inline void yield( unsigned )
143 } // namespace detail
145 } // namespace boost
147 #endif
149 #endif // #ifndef BOOST_SMART_PTR_DETAIL_YIELD_K_HPP_INCLUDED