sp_interlocked.hpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. #endif
  32. #if defined( BOOST_USE_WINDOWS_H )
  33. # include <windows.h>
  34. # define BOOST_SP_INTERLOCKED_INCREMENT InterlockedIncrement
  35. # define BOOST_SP_INTERLOCKED_DECREMENT InterlockedDecrement
  36. # define BOOST_SP_INTERLOCKED_COMPARE_EXCHANGE InterlockedCompareExchange
  37. # define BOOST_SP_INTERLOCKED_EXCHANGE InterlockedExchange
  38. # define BOOST_SP_INTERLOCKED_EXCHANGE_ADD InterlockedExchangeAdd
  39. #elif defined( BOOST_USE_INTRIN_H ) || defined( BOOST_SP_HAS_INTRIN_H )
  40. #include <intrin.h>
  41. # define BOOST_SP_INTERLOCKED_INCREMENT _InterlockedIncrement
  42. # define BOOST_SP_INTERLOCKED_DECREMENT _InterlockedDecrement
  43. # define BOOST_SP_INTERLOCKED_COMPARE_EXCHANGE _InterlockedCompareExchange
  44. # define BOOST_SP_INTERLOCKED_EXCHANGE _InterlockedExchange
  45. # define BOOST_SP_INTERLOCKED_EXCHANGE_ADD _InterlockedExchangeAdd
  46. #elif defined( _WIN32_WCE )
  47. #if _WIN32_WCE >= 0x600
  48. extern "C" long __cdecl _InterlockedIncrement( long volatile * );
  49. extern "C" long __cdecl _InterlockedDecrement( long volatile * );
  50. extern "C" long __cdecl _InterlockedCompareExchange( long volatile *, long, long );
  51. extern "C" long __cdecl _InterlockedExchange( long volatile *, long );
  52. extern "C" long __cdecl _InterlockedExchangeAdd( long volatile *, long );
  53. # define BOOST_SP_INTERLOCKED_INCREMENT _InterlockedIncrement
  54. # define BOOST_SP_INTERLOCKED_DECREMENT _InterlockedDecrement
  55. # define BOOST_SP_INTERLOCKED_COMPARE_EXCHANGE _InterlockedCompareExchange
  56. # define BOOST_SP_INTERLOCKED_EXCHANGE _InterlockedExchange
  57. # define BOOST_SP_INTERLOCKED_EXCHANGE_ADD _InterlockedExchangeAdd
  58. #else
  59. // under Windows CE we still have old-style Interlocked* functions
  60. extern "C" long __cdecl InterlockedIncrement( long* );
  61. extern "C" long __cdecl InterlockedDecrement( long* );
  62. extern "C" long __cdecl InterlockedCompareExchange( long*, long, long );
  63. extern "C" long __cdecl InterlockedExchange( long*, long );
  64. extern "C" long __cdecl InterlockedExchangeAdd( long*, long );
  65. # define BOOST_SP_INTERLOCKED_INCREMENT InterlockedIncrement
  66. # define BOOST_SP_INTERLOCKED_DECREMENT InterlockedDecrement
  67. # define BOOST_SP_INTERLOCKED_COMPARE_EXCHANGE InterlockedCompareExchange
  68. # define BOOST_SP_INTERLOCKED_EXCHANGE InterlockedExchange
  69. # define BOOST_SP_INTERLOCKED_EXCHANGE_ADD InterlockedExchangeAdd
  70. #endif
  71. #elif defined( BOOST_MSVC ) || defined( BOOST_INTEL_WIN )
  72. #if defined( __CLRCALL_PURE_OR_CDECL )
  73. extern "C" long __CLRCALL_PURE_OR_CDECL _InterlockedIncrement( long volatile * );
  74. extern "C" long __CLRCALL_PURE_OR_CDECL _InterlockedDecrement( long volatile * );
  75. extern "C" long __CLRCALL_PURE_OR_CDECL _InterlockedCompareExchange( long volatile *, long, long );
  76. extern "C" long __CLRCALL_PURE_OR_CDECL _InterlockedExchange( long volatile *, long );
  77. extern "C" long __CLRCALL_PURE_OR_CDECL _InterlockedExchangeAdd( long volatile *, long );
  78. #else
  79. extern "C" long __cdecl _InterlockedIncrement( long volatile * );
  80. extern "C" long __cdecl _InterlockedDecrement( long volatile * );
  81. extern "C" long __cdecl _InterlockedCompareExchange( long volatile *, long, long );
  82. extern "C" long __cdecl _InterlockedExchange( long volatile *, long );
  83. extern "C" long __cdecl _InterlockedExchangeAdd( long volatile *, long );
  84. # if defined( BOOST_MSVC ) && BOOST_MSVC == 1310
  85. //From MSDN, Visual Studio .NET 2003 spedific: To declare one of the interlocked functions
  86. //for use as an intrinsic, the function must be declared with the leading underscore and
  87. //the new function must appear in a #pragma intrinsic statement.
  88. # pragma intrinsic( _InterlockedIncrement )
  89. # pragma intrinsic( _InterlockedDecrement )
  90. # pragma intrinsic( _InterlockedCompareExchange )
  91. # pragma intrinsic( _InterlockedExchange )
  92. # pragma intrinsic( _InterlockedExchangeAdd )
  93. # endif
  94. #endif
  95. # define BOOST_SP_INTERLOCKED_INCREMENT _InterlockedIncrement
  96. # define BOOST_SP_INTERLOCKED_DECREMENT _InterlockedDecrement
  97. # define BOOST_SP_INTERLOCKED_COMPARE_EXCHANGE _InterlockedCompareExchange
  98. # define BOOST_SP_INTERLOCKED_EXCHANGE _InterlockedExchange
  99. # define BOOST_SP_INTERLOCKED_EXCHANGE_ADD _InterlockedExchangeAdd
  100. #elif defined( WIN32 ) || defined( _WIN32 ) || defined( __WIN32__ ) || defined( __CYGWIN__ )
  101. namespace boost
  102. {
  103. namespace detail
  104. {
  105. extern "C" __declspec(dllimport) long __stdcall InterlockedIncrement( long volatile * );
  106. extern "C" __declspec(dllimport) long __stdcall InterlockedDecrement( long volatile * );
  107. extern "C" __declspec(dllimport) long __stdcall InterlockedCompareExchange( long volatile *, long, long );
  108. extern "C" __declspec(dllimport) long __stdcall InterlockedExchange( long volatile *, long );
  109. extern "C" __declspec(dllimport) long __stdcall InterlockedExchangeAdd( long volatile *, long );
  110. } // namespace detail
  111. } // namespace boost
  112. # define BOOST_SP_INTERLOCKED_INCREMENT ::boost::detail::InterlockedIncrement
  113. # define BOOST_SP_INTERLOCKED_DECREMENT ::boost::detail::InterlockedDecrement
  114. # define BOOST_SP_INTERLOCKED_COMPARE_EXCHANGE ::boost::detail::InterlockedCompareExchange
  115. # define BOOST_SP_INTERLOCKED_EXCHANGE ::boost::detail::InterlockedExchange
  116. # define BOOST_SP_INTERLOCKED_EXCHANGE_ADD ::boost::detail::InterlockedExchangeAdd
  117. #else
  118. # error "Interlocked intrinsics not available"
  119. #endif
  120. #endif // #ifndef BOOST_SMART_PTR_DETAIL_SP_INTERLOCKED_HPP_INCLUDED