1#ifndef BOOST_SMART_PTR_DETAIL_YIELD_K_HPP_INCLUDED
2#define BOOST_SMART_PTR_DETAIL_YIELD_K_HPP_INCLUDED
3
4// MS compatible compilers support #pragma once
5
6#if defined(_MSC_VER) && (_MSC_VER >= 1020)
7# pragma once
8#endif
9
10//
11// yield_k.hpp
12//
13// Copyright (c) 2008 Peter Dimov
14// Copyright (c) Microsoft Corporation 2014
15//
16// void yield( unsigned k );
17//
18// Typical use:
19//
20// for( unsigned k = 0; !try_lock(); ++k ) yield( k );
21//
22// Distributed under the Boost Software License, Version 1.0.
23// See accompanying file LICENSE_1_0.txt or copy at
24// http://www.boost.org/LICENSE_1_0.txt
25//
26
27#include <boost/config.hpp>
28#include <boost/predef.h>
29
30#if BOOST_PLAT_WINDOWS_RUNTIME
31#include <thread>
32#endif
33
34// BOOST_SMT_PAUSE
35
36#if defined(_MSC_VER) && _MSC_VER >= 1310 && ( defined(_M_IX86) || defined(_M_X64) )
37
38extern "C" void _mm_pause();
39
40#define BOOST_SMT_PAUSE _mm_pause();
41
42#elif defined(__GNUC__) && ( defined(__i386__) || defined(__x86_64__) )
43
44#define BOOST_SMT_PAUSE __asm__ __volatile__( "rep; nop" : : : "memory" );
45
46#endif
47
48//
49
50#if defined( WIN32 ) || defined( _WIN32 ) || defined( __WIN32__ ) || defined( __CYGWIN__ )
51
52#if defined( BOOST_USE_WINDOWS_H )
53# include <windows.h>
54#endif
55
56namespace boost
57{
58
59namespace detail
60{
61
62#if !defined( BOOST_USE_WINDOWS_H ) && !BOOST_PLAT_WINDOWS_RUNTIME
63 extern "C" void __stdcall Sleep( unsigned long ms );
64#endif
65
66inline void yield( unsigned k )
67{
68 if( k < 4 )
69 {
70 }
71#if defined( BOOST_SMT_PAUSE )
72 else if( k < 16 )
73 {
74 BOOST_SMT_PAUSE
75 }
76#endif
77#if !BOOST_PLAT_WINDOWS_RUNTIME
78 else if( k < 32 )
79 {
80 Sleep( 0 );
81 }
82 else
83 {
84 Sleep( 1 );
85 }
86#else
87 else
88 {
89 // Sleep isn't supported on the Windows Runtime.
90 std::this_thread::yield();
91 }
92#endif
93}
94
95} // namespace detail
96
97} // namespace boost
98
99#elif defined( BOOST_HAS_PTHREADS )
100
101#ifndef _AIX
102#include <sched.h>
103#else
104 // AIX's sched.h defines ::var which sometimes conflicts with Lambda's var
105 extern "C" int sched_yield(void);
106#endif
107
108#include <time.h>
109
110namespace boost
111{
112
113namespace detail
114{
115
116inline void yield( unsigned k )
117{
118 if( k < 4 )
119 {
120 }
121#if defined( BOOST_SMT_PAUSE )
122 else if( k < 16 )
123 {
124 BOOST_SMT_PAUSE
125 }
126#endif
127 else if( k < 32 || k & 1 )
128 {
129 sched_yield();
130 }
131 else
132 {
133 // g++ -Wextra warns on {} or {0}
134 struct timespec rqtp = { 0, 0 };
135
136 // POSIX says that timespec has tv_sec and tv_nsec
137 // But it doesn't guarantee order or placement
138
139 rqtp.tv_sec = 0;
140 rqtp.tv_nsec = 1000;
141
142 nanosleep( &rqtp, 0 );
143 }
144}
145
146} // namespace detail
147
148} // namespace boost
149
150#else
151
152namespace boost
153{
154
155namespace detail
156{
157
158inline void yield( unsigned )
159{
160}
161
162} // namespace detail
163
164} // namespace boost
165
166#endif
167
168#endif // #ifndef BOOST_SMART_PTR_DETAIL_YIELD_K_HPP_INCLUDED
169