]> git.donarmstrong.com Git - rsem.git/blob - boost/smart_ptr/detail/yield_k.hpp
Updated boost to v1.55.0
[rsem.git] / boost / smart_ptr / detail / yield_k.hpp
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 //
15 //  void yield( unsigned k );
16 //
17 //  Typical use:
18 //
19 //  for( unsigned k = 0; !try_lock(); ++k ) yield( k );
20 //
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
24 //
25
26 #include <boost/config.hpp>
27
28 // BOOST_SMT_PAUSE
29
30 #if defined(_MSC_VER) && _MSC_VER >= 1310 && ( defined(_M_IX86) || defined(_M_X64) )
31
32 extern "C" void _mm_pause();
33
34 #define BOOST_SMT_PAUSE _mm_pause();
35
36 #elif defined(__GNUC__) && ( defined(__i386__) || defined(__x86_64__) )
37
38 #define BOOST_SMT_PAUSE __asm__ __volatile__( "rep; nop" : : : "memory" );
39
40 #endif
41
42 //
43
44 #if defined( WIN32 ) || defined( _WIN32 ) || defined( __WIN32__ ) || defined( __CYGWIN__ )
45
46 #if defined( BOOST_USE_WINDOWS_H )
47 # include <windows.h>
48 #endif
49
50 namespace boost
51 {
52
53 namespace detail
54 {
55
56 #if !defined( BOOST_USE_WINDOWS_H )
57   extern "C" void __stdcall Sleep( unsigned long ms );
58 #endif
59
60 inline void yield( unsigned k )
61 {
62     if( k < 4 )
63     {
64     }
65 #if defined( BOOST_SMT_PAUSE )
66     else if( k < 16 )
67     {
68         BOOST_SMT_PAUSE
69     }
70 #endif
71     else if( k < 32 )
72     {
73         Sleep( 0 );
74     }
75     else
76     {
77         Sleep( 1 );
78     }
79 }
80
81 } // namespace detail
82
83 } // namespace boost
84
85 #elif defined( BOOST_HAS_PTHREADS )
86
87 #include <sched.h>
88 #include <time.h>
89
90 namespace boost
91 {
92
93 namespace detail
94 {
95
96 inline void yield( unsigned k )
97 {
98     if( k < 4 )
99     {
100     }
101 #if defined( BOOST_SMT_PAUSE )
102     else if( k < 16 )
103     {
104         BOOST_SMT_PAUSE
105     }
106 #endif
107     else if( k < 32 || k & 1 )
108     {
109         sched_yield();
110     }
111     else
112     {
113         // g++ -Wextra warns on {} or {0}
114         struct timespec rqtp = { 0, 0 };
115
116         // POSIX says that timespec has tv_sec and tv_nsec
117         // But it doesn't guarantee order or placement
118
119         rqtp.tv_sec = 0;
120         rqtp.tv_nsec = 1000;
121
122         nanosleep( &rqtp, 0 );
123     }
124 }
125
126 } // namespace detail
127
128 } // namespace boost
129
130 #else
131
132 namespace boost
133 {
134
135 namespace detail
136 {
137
138 inline void yield( unsigned )
139 {
140 }
141
142 } // namespace detail
143
144 } // namespace boost
145
146 #endif
147
148 #endif // #ifndef BOOST_SMART_PTR_DETAIL_YIELD_K_HPP_INCLUDED