interlocked.hpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. #ifndef BOOST_DETAIL_INTERLOCKED_HPP_INCLUDED
  2. #define BOOST_DETAIL_INTERLOCKED_HPP_INCLUDED
  3. //
  4. // boost/detail/interlocked.hpp
  5. //
  6. // Copyright 2005 Peter Dimov
  7. //
  8. // Distributed under the Boost Software License, Version 1.0. (See
  9. // accompanying file LICENSE_1_0.txt or copy at
  10. // http://www.boost.org/LICENSE_1_0.txt)
  11. //
  12. #include <boost/config.hpp>
  13. // MS compatible compilers support #pragma once
  14. #ifdef BOOST_HAS_PRAGMA_ONCE
  15. #pragma once
  16. #endif
  17. #if defined( BOOST_USE_WINDOWS_H )
  18. # include <windows.h>
  19. # define BOOST_INTERLOCKED_INCREMENT InterlockedIncrement
  20. # define BOOST_INTERLOCKED_DECREMENT InterlockedDecrement
  21. # define BOOST_INTERLOCKED_COMPARE_EXCHANGE InterlockedCompareExchange
  22. # define BOOST_INTERLOCKED_EXCHANGE InterlockedExchange
  23. # define BOOST_INTERLOCKED_EXCHANGE_ADD InterlockedExchangeAdd
  24. # define BOOST_INTERLOCKED_COMPARE_EXCHANGE_POINTER InterlockedCompareExchangePointer
  25. # define BOOST_INTERLOCKED_EXCHANGE_POINTER InterlockedExchangePointer
  26. #elif defined( BOOST_USE_INTRIN_H )
  27. #include <intrin.h>
  28. # define BOOST_INTERLOCKED_INCREMENT _InterlockedIncrement
  29. # define BOOST_INTERLOCKED_DECREMENT _InterlockedDecrement
  30. # define BOOST_INTERLOCKED_COMPARE_EXCHANGE _InterlockedCompareExchange
  31. # define BOOST_INTERLOCKED_EXCHANGE _InterlockedExchange
  32. # define BOOST_INTERLOCKED_EXCHANGE_ADD _InterlockedExchangeAdd
  33. # if defined(_M_IA64) || defined(_M_AMD64) || defined(__x86_64__) || defined(__x86_64)
  34. # define BOOST_INTERLOCKED_COMPARE_EXCHANGE_POINTER _InterlockedCompareExchangePointer
  35. # define BOOST_INTERLOCKED_EXCHANGE_POINTER _InterlockedExchangePointer
  36. # else
  37. # define BOOST_INTERLOCKED_COMPARE_EXCHANGE_POINTER(dest,exchange,compare) \
  38. ((void*)BOOST_INTERLOCKED_COMPARE_EXCHANGE((long volatile*)(dest),(long)(exchange),(long)(compare)))
  39. # define BOOST_INTERLOCKED_EXCHANGE_POINTER(dest,exchange) \
  40. ((void*)BOOST_INTERLOCKED_EXCHANGE((long volatile*)(dest),(long)(exchange)))
  41. # endif
  42. #elif defined(_WIN32_WCE)
  43. #if _WIN32_WCE >= 0x600
  44. extern "C" long __cdecl _InterlockedIncrement( long volatile * );
  45. extern "C" long __cdecl _InterlockedDecrement( long volatile * );
  46. extern "C" long __cdecl _InterlockedCompareExchange( long volatile *, long, long );
  47. extern "C" long __cdecl _InterlockedExchange( long volatile *, long );
  48. extern "C" long __cdecl _InterlockedExchangeAdd( long volatile *, long );
  49. # define BOOST_INTERLOCKED_INCREMENT _InterlockedIncrement
  50. # define BOOST_INTERLOCKED_DECREMENT _InterlockedDecrement
  51. # define BOOST_INTERLOCKED_COMPARE_EXCHANGE _InterlockedCompareExchange
  52. # define BOOST_INTERLOCKED_EXCHANGE _InterlockedExchange
  53. # define BOOST_INTERLOCKED_EXCHANGE_ADD _InterlockedExchangeAdd
  54. #else
  55. // under Windows CE we still have old-style Interlocked* functions
  56. extern "C" long __cdecl InterlockedIncrement( long* );
  57. extern "C" long __cdecl InterlockedDecrement( long* );
  58. extern "C" long __cdecl InterlockedCompareExchange( long*, long, long );
  59. extern "C" long __cdecl InterlockedExchange( long*, long );
  60. extern "C" long __cdecl InterlockedExchangeAdd( long*, long );
  61. # define BOOST_INTERLOCKED_INCREMENT InterlockedIncrement
  62. # define BOOST_INTERLOCKED_DECREMENT InterlockedDecrement
  63. # define BOOST_INTERLOCKED_COMPARE_EXCHANGE InterlockedCompareExchange
  64. # define BOOST_INTERLOCKED_EXCHANGE InterlockedExchange
  65. # define BOOST_INTERLOCKED_EXCHANGE_ADD InterlockedExchangeAdd
  66. #endif
  67. # define BOOST_INTERLOCKED_COMPARE_EXCHANGE_POINTER(dest,exchange,compare) \
  68. ((void*)BOOST_INTERLOCKED_COMPARE_EXCHANGE((long*)(dest),(long)(exchange),(long)(compare)))
  69. # define BOOST_INTERLOCKED_EXCHANGE_POINTER(dest,exchange) \
  70. ((void*)BOOST_INTERLOCKED_EXCHANGE((long*)(dest),(long)(exchange)))
  71. #elif defined( BOOST_MSVC ) || defined( BOOST_INTEL_WIN )
  72. #if defined( BOOST_MSVC ) && BOOST_MSVC >= 1400
  73. #include <intrin.h>
  74. #else
  75. # if defined( __CLRCALL_PURE_OR_CDECL )
  76. # define BOOST_INTERLOCKED_CLRCALL_PURE_OR_CDECL __CLRCALL_PURE_OR_CDECL
  77. # else
  78. # define BOOST_INTERLOCKED_CLRCALL_PURE_OR_CDECL __cdecl
  79. # endif
  80. extern "C" long BOOST_INTERLOCKED_CLRCALL_PURE_OR_CDECL _InterlockedIncrement( long volatile * );
  81. extern "C" long BOOST_INTERLOCKED_CLRCALL_PURE_OR_CDECL _InterlockedDecrement( long volatile * );
  82. extern "C" long BOOST_INTERLOCKED_CLRCALL_PURE_OR_CDECL _InterlockedCompareExchange( long volatile *, long, long );
  83. extern "C" long BOOST_INTERLOCKED_CLRCALL_PURE_OR_CDECL _InterlockedExchange( long volatile *, long );
  84. extern "C" long BOOST_INTERLOCKED_CLRCALL_PURE_OR_CDECL _InterlockedExchangeAdd( long volatile *, long );
  85. # undef BOOST_INTERLOCKED_CLRCALL_PURE_OR_CDECL
  86. # if defined( BOOST_MSVC ) && BOOST_MSVC >= 1310
  87. # pragma intrinsic( _InterlockedIncrement )
  88. # pragma intrinsic( _InterlockedDecrement )
  89. # pragma intrinsic( _InterlockedCompareExchange )
  90. # pragma intrinsic( _InterlockedExchange )
  91. # pragma intrinsic( _InterlockedExchangeAdd )
  92. # endif
  93. #endif
  94. # if defined(_M_IA64) || defined(_M_AMD64)
  95. extern "C" void* __cdecl _InterlockedCompareExchangePointer( void* volatile *, void*, void* );
  96. extern "C" void* __cdecl _InterlockedExchangePointer( void* volatile *, void* );
  97. # define BOOST_INTERLOCKED_COMPARE_EXCHANGE_POINTER _InterlockedCompareExchangePointer
  98. # define BOOST_INTERLOCKED_EXCHANGE_POINTER _InterlockedExchangePointer
  99. # else
  100. # define BOOST_INTERLOCKED_COMPARE_EXCHANGE_POINTER(dest,exchange,compare) \
  101. ((void*)BOOST_INTERLOCKED_COMPARE_EXCHANGE((long volatile*)(dest),(long)(exchange),(long)(compare)))
  102. # define BOOST_INTERLOCKED_EXCHANGE_POINTER(dest,exchange) \
  103. ((void*)BOOST_INTERLOCKED_EXCHANGE((long volatile*)(dest),(long)(exchange)))
  104. # endif
  105. # define BOOST_INTERLOCKED_INCREMENT _InterlockedIncrement
  106. # define BOOST_INTERLOCKED_DECREMENT _InterlockedDecrement
  107. # define BOOST_INTERLOCKED_COMPARE_EXCHANGE _InterlockedCompareExchange
  108. # define BOOST_INTERLOCKED_EXCHANGE _InterlockedExchange
  109. # define BOOST_INTERLOCKED_EXCHANGE_ADD _InterlockedExchangeAdd
  110. // Unlike __MINGW64__, __MINGW64_VERSION_MAJOR is defined by MinGW-w64 for both 32 and 64-bit targets.
  111. #elif defined(__MINGW64_VERSION_MAJOR)
  112. // MinGW-w64 provides intrin.h for both 32 and 64-bit targets.
  113. #include <intrin.h>
  114. # define BOOST_INTERLOCKED_INCREMENT _InterlockedIncrement
  115. # define BOOST_INTERLOCKED_DECREMENT _InterlockedDecrement
  116. # define BOOST_INTERLOCKED_COMPARE_EXCHANGE _InterlockedCompareExchange
  117. # define BOOST_INTERLOCKED_EXCHANGE _InterlockedExchange
  118. # define BOOST_INTERLOCKED_EXCHANGE_ADD _InterlockedExchangeAdd
  119. # if defined(__x86_64__) || defined(__x86_64)
  120. # define BOOST_INTERLOCKED_COMPARE_EXCHANGE_POINTER _InterlockedCompareExchangePointer
  121. # define BOOST_INTERLOCKED_EXCHANGE_POINTER _InterlockedExchangePointer
  122. # else
  123. # define BOOST_INTERLOCKED_COMPARE_EXCHANGE_POINTER(dest,exchange,compare) \
  124. ((void*)BOOST_INTERLOCKED_COMPARE_EXCHANGE((long volatile*)(dest),(long)(exchange),(long)(compare)))
  125. # define BOOST_INTERLOCKED_EXCHANGE_POINTER(dest,exchange) \
  126. ((void*)BOOST_INTERLOCKED_EXCHANGE((long volatile*)(dest),(long)(exchange)))
  127. # endif
  128. #elif defined( WIN32 ) || defined( _WIN32 ) || defined( __WIN32__ ) || defined( __CYGWIN__ )
  129. #define BOOST_INTERLOCKED_IMPORT __declspec(dllimport)
  130. namespace boost
  131. {
  132. namespace detail
  133. {
  134. extern "C" BOOST_INTERLOCKED_IMPORT long __stdcall InterlockedIncrement( long volatile * );
  135. extern "C" BOOST_INTERLOCKED_IMPORT long __stdcall InterlockedDecrement( long volatile * );
  136. extern "C" BOOST_INTERLOCKED_IMPORT long __stdcall InterlockedCompareExchange( long volatile *, long, long );
  137. extern "C" BOOST_INTERLOCKED_IMPORT long __stdcall InterlockedExchange( long volatile *, long );
  138. extern "C" BOOST_INTERLOCKED_IMPORT long __stdcall InterlockedExchangeAdd( long volatile *, long );
  139. # if defined(_M_IA64) || defined(_M_AMD64)
  140. extern "C" BOOST_INTERLOCKED_IMPORT void* __stdcall InterlockedCompareExchangePointer( void* volatile *, void*, void* );
  141. extern "C" BOOST_INTERLOCKED_IMPORT void* __stdcall InterlockedExchangePointer( void* volatile *, void* );
  142. # endif
  143. } // namespace detail
  144. } // namespace boost
  145. # define BOOST_INTERLOCKED_INCREMENT ::boost::detail::InterlockedIncrement
  146. # define BOOST_INTERLOCKED_DECREMENT ::boost::detail::InterlockedDecrement
  147. # define BOOST_INTERLOCKED_COMPARE_EXCHANGE ::boost::detail::InterlockedCompareExchange
  148. # define BOOST_INTERLOCKED_EXCHANGE ::boost::detail::InterlockedExchange
  149. # define BOOST_INTERLOCKED_EXCHANGE_ADD ::boost::detail::InterlockedExchangeAdd
  150. # if defined(_M_IA64) || defined(_M_AMD64)
  151. # define BOOST_INTERLOCKED_COMPARE_EXCHANGE_POINTER ::boost::detail::InterlockedCompareExchangePointer
  152. # define BOOST_INTERLOCKED_EXCHANGE_POINTER ::boost::detail::InterlockedExchangePointer
  153. # else
  154. # define BOOST_INTERLOCKED_COMPARE_EXCHANGE_POINTER(dest,exchange,compare) \
  155. ((void*)BOOST_INTERLOCKED_COMPARE_EXCHANGE((long volatile*)(dest),(long)(exchange),(long)(compare)))
  156. # define BOOST_INTERLOCKED_EXCHANGE_POINTER(dest,exchange) \
  157. ((void*)BOOST_INTERLOCKED_EXCHANGE((long volatile*)(dest),(long)(exchange)))
  158. # endif
  159. #else
  160. # error "Interlocked intrinsics not available"
  161. #endif
  162. #endif // #ifndef BOOST_DETAIL_INTERLOCKED_HPP_INCLUDED