]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
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> | |
92f5a8d4 | 28 | #include <boost/predef/platform/windows_runtime.h> |
7c673cae FG |
29 | |
30 | #if BOOST_PLAT_WINDOWS_RUNTIME | |
31 | #include <thread> | |
32 | #endif | |
33 | ||
34 | // BOOST_SMT_PAUSE | |
35 | ||
b32b8144 | 36 | #if defined(_MSC_VER) && _MSC_VER >= 1310 && ( defined(_M_IX86) || defined(_M_X64) ) && !defined(__c2__) |
7c673cae FG |
37 | |
38 | extern "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 | ||
56 | namespace boost | |
57 | { | |
58 | ||
59 | namespace detail | |
60 | { | |
61 | ||
62 | #if !defined( BOOST_USE_WINDOWS_H ) && !BOOST_PLAT_WINDOWS_RUNTIME | |
92f5a8d4 TL |
63 | |
64 | #if defined(__clang__) && defined(__x86_64__) | |
65 | // clang x64 warns that __stdcall is ignored | |
66 | # define BOOST_SP_STDCALL | |
7c673cae | 67 | #else |
92f5a8d4 | 68 | # define BOOST_SP_STDCALL __stdcall |
7c673cae | 69 | #endif |
92f5a8d4 TL |
70 | |
71 | #if defined(__LP64__) // Cygwin 64 | |
72 | extern "C" __declspec(dllimport) void BOOST_SP_STDCALL Sleep( unsigned int ms ); | |
73 | #else | |
74 | extern "C" __declspec(dllimport) void BOOST_SP_STDCALL Sleep( unsigned long ms ); | |
7c673cae FG |
75 | #endif |
76 | ||
92f5a8d4 TL |
77 | #undef BOOST_SP_STDCALL |
78 | ||
79 | #endif // !defined( BOOST_USE_WINDOWS_H ) && !BOOST_PLAT_WINDOWS_RUNTIME | |
80 | ||
81 | inline void yield( unsigned k ) BOOST_NOEXCEPT | |
7c673cae FG |
82 | { |
83 | if( k < 4 ) | |
84 | { | |
85 | } | |
86 | #if defined( BOOST_SMT_PAUSE ) | |
87 | else if( k < 16 ) | |
88 | { | |
89 | BOOST_SMT_PAUSE | |
90 | } | |
91 | #endif | |
92 | #if !BOOST_PLAT_WINDOWS_RUNTIME | |
93 | else if( k < 32 ) | |
94 | { | |
95 | Sleep( 0 ); | |
96 | } | |
97 | else | |
98 | { | |
99 | Sleep( 1 ); | |
100 | } | |
101 | #else | |
102 | else | |
103 | { | |
104 | // Sleep isn't supported on the Windows Runtime. | |
105 | std::this_thread::yield(); | |
106 | } | |
107 | #endif | |
108 | } | |
109 | ||
110 | } // namespace detail | |
111 | ||
112 | } // namespace boost | |
113 | ||
114 | #elif defined( BOOST_HAS_PTHREADS ) | |
115 | ||
116 | #ifndef _AIX | |
117 | #include <sched.h> | |
118 | #else | |
119 | // AIX's sched.h defines ::var which sometimes conflicts with Lambda's var | |
120 | extern "C" int sched_yield(void); | |
121 | #endif | |
122 | ||
123 | #include <time.h> | |
124 | ||
125 | namespace boost | |
126 | { | |
127 | ||
128 | namespace detail | |
129 | { | |
130 | ||
131 | inline void yield( unsigned k ) | |
132 | { | |
133 | if( k < 4 ) | |
134 | { | |
135 | } | |
136 | #if defined( BOOST_SMT_PAUSE ) | |
137 | else if( k < 16 ) | |
138 | { | |
139 | BOOST_SMT_PAUSE | |
140 | } | |
141 | #endif | |
142 | else if( k < 32 || k & 1 ) | |
143 | { | |
144 | sched_yield(); | |
145 | } | |
146 | else | |
147 | { | |
148 | // g++ -Wextra warns on {} or {0} | |
149 | struct timespec rqtp = { 0, 0 }; | |
150 | ||
151 | // POSIX says that timespec has tv_sec and tv_nsec | |
152 | // But it doesn't guarantee order or placement | |
153 | ||
154 | rqtp.tv_sec = 0; | |
155 | rqtp.tv_nsec = 1000; | |
156 | ||
157 | nanosleep( &rqtp, 0 ); | |
158 | } | |
159 | } | |
160 | ||
161 | } // namespace detail | |
162 | ||
163 | } // namespace boost | |
164 | ||
165 | #else | |
166 | ||
167 | namespace boost | |
168 | { | |
169 | ||
170 | namespace detail | |
171 | { | |
172 | ||
173 | inline void yield( unsigned ) | |
174 | { | |
175 | } | |
176 | ||
177 | } // namespace detail | |
178 | ||
179 | } // namespace boost | |
180 | ||
181 | #endif | |
182 | ||
183 | #endif // #ifndef BOOST_SMART_PTR_DETAIL_YIELD_K_HPP_INCLUDED |