sp_interlocked.hpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. #ifndef BOOST_SMART_PTR_DETAIL_SP_INTERLOCKED_HPP_INCLUDED
  2. #define BOOST_SMART_PTR_DETAIL_SP_INTERLOCKED_HPP_INCLUDED
  3. // MS compatible compilers support #pragma once
  4. #if defined(_MSC_VER) && (_MSC_VER >= 1020)
  5. # pragma once
  6. #endif
  7. //
  8. // boost/detail/sp_interlocked.hpp
  9. //
  10. // Copyright 2005, 2014 Peter Dimov
  11. //
  12. // Distributed under the Boost Software License, Version 1.0.
  13. // See accompanying file LICENSE_1_0.txt or copy at
  14. // http://www.boost.org/LICENSE_1_0.txt
  15. //
  16. #include <boost/config.hpp>
  17. // BOOST_SP_HAS_INTRIN_H
  18. // VC9 has intrin.h, but it collides with <utility>
  19. #if defined( BOOST_MSVC ) && BOOST_MSVC >= 1600
  20. # define BOOST_SP_HAS_INTRIN_H
  21. // Unlike __MINGW64__, __MINGW64_VERSION_MAJOR is defined by MinGW-w64 for both 32 and 64-bit targets.
  22. #elif defined( __MINGW64_VERSION_MAJOR )
  23. // MinGW-w64 provides intrin.h for both 32 and 64-bit targets.
  24. # define BOOST_SP_HAS_INTRIN_H
  25. #elif defined( __LP64__ )
  26. // We have to use intrin.h on Cygwin 64
  27. # define BOOST_SP_HAS_INTRIN_H
  28. // Intel C++ on Windows on VC10+ stdlib
  29. #elif defined( BOOST_INTEL_WIN ) && defined( _CPPLIB_VER ) && _CPPLIB_VER >= 520
  30. # define BOOST_SP_HAS_INTRIN_H
  31. // clang-cl on Windows on VC10+ stdlib
  32. #elif defined( __clang__ ) && defined( _MSC_VER ) && defined( _CPPLIB_VER ) && _CPPLIB_VER >= 520
  33. # define BOOST_SP_HAS_INTRIN_H
  34. #endif
  35. #if defined( BOOST_USE_WINDOWS_H )
  36. # include <windows.h>
  37. # define BOOST_SP_INTERLOCKED_INCREMENT InterlockedIncrement
  38. # define BOOST_SP_INTERLOCKED_DECREMENT InterlockedDecrement
  39. # define BOOST_SP_INTERLOCKED_COMPARE_EXCHANGE InterlockedCompareExchange
  40. # define BOOST_SP_INTERLOCKED_EXCHANGE InterlockedExchange
  41. # define BOOST_SP_INTERLOCKED_EXCHANGE_ADD InterlockedExchangeAdd
  42. #elif defined( BOOST_USE_INTRIN_H ) || defined( BOOST_SP_HAS_INTRIN_H )
  43. #include <intrin.h>
  44. # define BOOST_SP_INTERLOCKED_INCREMENT _InterlockedIncrement
  45. # define BOOST_SP_INTERLOCKED_DECREMENT _InterlockedDecrement
  46. # define BOOST_SP_INTERLOCKED_COMPARE_EXCHANGE _InterlockedCompareExchange
  47. # define BOOST_SP_INTERLOCKED_EXCHANGE _InterlockedExchange
  48. # define BOOST_SP_INTERLOCKED_EXCHANGE_ADD _InterlockedExchangeAdd
  49. #elif defined( _WIN32_WCE )
  50. #if _WIN32_WCE >= 0x600
  51. extern "C" long __cdecl _InterlockedIncrement( long volatile * );
  52. extern "C" long __cdecl _InterlockedDecrement( long volatile * );
  53. extern "C" long __cdecl _InterlockedCompareExchange( long volatile *, long, long );
  54. extern "C" long __cdecl _InterlockedExchange( long volatile *, long );
  55. extern "C" long __cdecl _InterlockedExchangeAdd( long volatile *, long );
  56. # define BOOST_SP_INTERLOCKED_INCREMENT _InterlockedIncrement
  57. # define BOOST_SP_INTERLOCKED_DECREMENT _InterlockedDecrement
  58. # define BOOST_SP_INTERLOCKED_COMPARE_EXCHANGE _InterlockedCompareExchange
  59. # define BOOST_SP_INTERLOCKED_EXCHANGE _InterlockedExchange
  60. # define BOOST_SP_INTERLOCKED_EXCHANGE_ADD _InterlockedExchangeAdd
  61. #else
  62. // under Windows CE we still have old-style Interlocked* functions
  63. extern "C" long __cdecl InterlockedIncrement( long* );
  64. extern "C" long __cdecl InterlockedDecrement( long* );
  65. extern "C" long __cdecl InterlockedCompareExchange( long*, long, long );
  66. extern "C" long __cdecl InterlockedExchange( long*, long );
  67. extern "C" long __cdecl InterlockedExchangeAdd( long*, long );
  68. # define BOOST_SP_INTERLOCKED_INCREMENT InterlockedIncrement
  69. # define BOOST_SP_INTERLOCKED_DECREMENT InterlockedDecrement
  70. # define BOOST_SP_INTERLOCKED_COMPARE_EXCHANGE InterlockedCompareExchange
  71. # define BOOST_SP_INTERLOCKED_EXCHANGE InterlockedExchange
  72. # define BOOST_SP_INTERLOCKED_EXCHANGE_ADD InterlockedExchangeAdd
  73. #endif
  74. #elif defined( BOOST_MSVC ) || defined( BOOST_INTEL_WIN )
  75. #if defined( __CLRCALL_PURE_OR_CDECL )
  76. extern "C" long __CLRCALL_PURE_OR_CDECL _InterlockedIncrement( long volatile * );
  77. extern "C" long __CLRCALL_PURE_OR_CDECL _InterlockedDecrement( long volatile * );
  78. extern "C" long __CLRCALL_PURE_OR_CDECL _InterlockedCompareExchange( long volatile *, long, long );
  79. extern "C" long __CLRCALL_PURE_OR_CDECL _InterlockedExchange( long volatile *, long );
  80. extern "C" long __CLRCALL_PURE_OR_CDECL _InterlockedExchangeAdd( long volatile *, long );
  81. #else
  82. extern "C" long __cdecl _InterlockedIncrement( long volatile * );
  83. extern "C" long __cdecl _InterlockedDecrement( long volatile * );
  84. extern "C" long __cdecl _InterlockedCompareExchange( long volatile *, long, long );
  85. extern "C" long __cdecl _InterlockedExchange( long volatile *, long );
  86. extern "C" long __cdecl _InterlockedExchangeAdd( long volatile *, long );
  87. # if defined( BOOST_MSVC ) && BOOST_MSVC == 1310
  88. //From MSDN, Visual Studio .NET 2003 spedific: To declare one of the interlocked functions
  89. //for use as an intrinsic, the function must be declared with the leading underscore and
  90. //the new function must appear in a #pragma intrinsic statement.
  91. # pragma intrinsic( _InterlockedIncrement )
  92. # pragma intrinsic( _InterlockedDecrement )
  93. # pragma intrinsic( _InterlockedCompareExchange )
  94. # pragma intrinsic( _InterlockedExchange )
  95. # pragma intrinsic( _InterlockedExchangeAdd )
  96. # endif
  97. #endif
  98. # define BOOST_SP_INTERLOCKED_INCREMENT _InterlockedIncrement
  99. # define BOOST_SP_INTERLOCKED_DECREMENT _InterlockedDecrement
  100. # define BOOST_SP_INTERLOCKED_COMPARE_EXCHANGE _InterlockedCompareExchange
  101. # define BOOST_SP_INTERLOCKED_EXCHANGE _InterlockedExchange
  102. # define BOOST_SP_INTERLOCKED_EXCHANGE_ADD _InterlockedExchangeAdd
  103. #elif defined( WIN32 ) || defined( _WIN32 ) || defined( __WIN32__ ) || defined( __CYGWIN__ )
  104. namespace boost
  105. {
  106. namespace detail
  107. {
  108. extern "C" __declspec(dllimport) long __stdcall InterlockedIncrement( long volatile * );
  109. extern "C" __declspec(dllimport) long __stdcall InterlockedDecrement( long volatile * );
  110. extern "C" __declspec(dllimport) long __stdcall InterlockedCompareExchange( long volatile *, long, long );
  111. extern "C" __declspec(dllimport) long __stdcall InterlockedExchange( long volatile *, long );
  112. extern "C" __declspec(dllimport) long __stdcall InterlockedExchangeAdd( long volatile *, long );
  113. } // namespace detail
  114. } // namespace boost
  115. # define BOOST_SP_INTERLOCKED_INCREMENT ::boost::detail::InterlockedIncrement
  116. # define BOOST_SP_INTERLOCKED_DECREMENT ::boost::detail::InterlockedDecrement
  117. # define BOOST_SP_INTERLOCKED_COMPARE_EXCHANGE ::boost::detail::InterlockedCompareExchange
  118. # define BOOST_SP_INTERLOCKED_EXCHANGE ::boost::detail::InterlockedExchange
  119. # define BOOST_SP_INTERLOCKED_EXCHANGE_ADD ::boost::detail::InterlockedExchangeAdd
  120. #else
  121. # error "Interlocked intrinsics not available"
  122. #endif
  123. #endif // #ifndef BOOST_SMART_PTR_DETAIL_SP_INTERLOCKED_HPP_INCLUDED