|
| 1 | +//This file was copied from boost/smart_ptr/detail and |
| 2 | +//modified here to avoid dependencies with that library |
| 3 | +#ifndef BOOST_INTERPROCESS_DETAIL_YIELD_K_HPP_INCLUDED |
| 4 | +#define BOOST_INTERPROCESS_DETAIL_YIELD_K_HPP_INCLUDED |
| 5 | + |
| 6 | +// MS compatible compilers support #pragma once |
| 7 | + |
| 8 | +#if defined(_MSC_VER) && (_MSC_VER >= 1020) |
| 9 | +# pragma once |
| 10 | +#endif |
| 11 | + |
| 12 | +// |
| 13 | +// yield_k.hpp |
| 14 | +// |
| 15 | +// Copyright (c) 2008 Peter Dimov |
| 16 | +// |
| 17 | +// void yield( unsigned k ); |
| 18 | +// |
| 19 | +// Typical use: |
| 20 | +// |
| 21 | +// for( unsigned k = 0; !try_lock(); ++k ) yield( k ); |
| 22 | +// |
| 23 | +// Distributed under the Boost Software License, Version 1.0. |
| 24 | +// See accompanying file LICENSE_1_0.txt or copy at |
| 25 | +// http://www.boost.org/LICENSE_1_0.txt |
| 26 | +// |
| 27 | + |
| 28 | +#include <boost/interprocess/detail/config_begin.hpp> |
| 29 | +#include <boost/interprocess/detail/workaround.hpp> |
| 30 | + |
| 31 | +// BOOST_INTERPROCESS_SMT_PAUSE |
| 32 | + |
| 33 | +#if defined(_MSC_VER) && _MSC_VER >= 1310 && ( defined(_M_IX86) || defined(_M_X64) ) |
| 34 | + |
| 35 | +extern "C" void _mm_pause(); |
| 36 | +#pragma intrinsic( _mm_pause ) |
| 37 | + |
| 38 | +#define BOOST_INTERPROCESS_SMT_PAUSE _mm_pause(); |
| 39 | + |
| 40 | +#elif defined(__GNUC__) && ( defined(__i386__) || defined(__x86_64__) ) |
| 41 | + |
| 42 | +#define BOOST_INTERPROCESS_SMT_PAUSE __asm__ __volatile__( "rep; nop" : : : "memory" ); |
| 43 | + |
| 44 | +#endif |
| 45 | + |
| 46 | +// |
| 47 | + |
| 48 | +#if defined (BOOST_INTERPROCESS_WINDOWS) |
| 49 | + |
| 50 | +#include <boost/interprocess/detail/win32_api.hpp> |
| 51 | + |
| 52 | +namespace boost |
| 53 | +{ |
| 54 | +namespace interprocess |
| 55 | +{ |
| 56 | +namespace ipcdetail |
| 57 | +{ |
| 58 | + |
| 59 | +inline void yield( unsigned k ) |
| 60 | +{ |
| 61 | + if( k < 4 ) |
| 62 | + { |
| 63 | + } |
| 64 | +#if defined( BOOST_INTERPROCESS_SMT_PAUSE ) |
| 65 | + else if( k < 16 ){ |
| 66 | + BOOST_INTERPROCESS_SMT_PAUSE |
| 67 | + } |
| 68 | +#endif |
| 69 | + else if( k < 32 ){ |
| 70 | + //Try to yield to another thread running on the current processor |
| 71 | + if(!winapi::SwitchToThread()){ |
| 72 | + //If not yield to any thread of same or higher priority on any processor |
| 73 | + boost::interprocess::winapi::Sleep(0); |
| 74 | + } |
| 75 | + } |
| 76 | + else{ |
| 77 | + //Yields to any thread on any processor |
| 78 | + boost::interprocess::winapi::Sleep(1); |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +} // namespace ipcdetail |
| 83 | +} // namespace interprocess |
| 84 | +} // namespace boost |
| 85 | + |
| 86 | +#else |
| 87 | + |
| 88 | +#include <sched.h> |
| 89 | +#include <time.h> |
| 90 | + |
| 91 | +namespace boost |
| 92 | +{ |
| 93 | +namespace interprocess |
| 94 | +{ |
| 95 | +namespace ipcdetail |
| 96 | +{ |
| 97 | + |
| 98 | +inline void yield( unsigned k ) |
| 99 | +{ |
| 100 | + if( k < 4 ) |
| 101 | + { |
| 102 | + } |
| 103 | +#if defined( BOOST_INTERPROCESS_SMT_PAUSE ) |
| 104 | + else if( k < 16 ) |
| 105 | + { |
| 106 | + BOOST_INTERPROCESS_SMT_PAUSE |
| 107 | + } |
| 108 | +#endif |
| 109 | + else if( k < 32 || k & 1 ) |
| 110 | + { |
| 111 | + sched_yield(); |
| 112 | + } |
| 113 | + else |
| 114 | + { |
| 115 | + // g++ -Wextra warns on {} or {0} |
| 116 | + struct timespec rqtp = { 0, 0 }; |
| 117 | + |
| 118 | + // POSIX says that timespec has tv_sec and tv_nsec |
| 119 | + // But it doesn't guarantee order or placement |
| 120 | + |
| 121 | + rqtp.tv_sec = 0; |
| 122 | + rqtp.tv_nsec = 1000; |
| 123 | + |
| 124 | + nanosleep( &rqtp, 0 ); |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +} // namespace ipcdetail |
| 129 | +} // namespace interprocess |
| 130 | +} // namespace boost |
| 131 | + |
| 132 | +#endif |
| 133 | + |
| 134 | +#include <boost/interprocess/detail/config_end.hpp> |
| 135 | + |
| 136 | +#endif // #ifndef BOOST_INTERPROCESS_DETAIL_YIELD_K_HPP_INCLUDED |
0 commit comments