interlocked.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. // Copyright 2018, 2019 Andrey Semashev
  8. //
  9. // Distributed under the Boost Software License, Version 1.0. (See
  10. // accompanying file LICENSE_1_0.txt or copy at
  11. // http://www.boost.org/LICENSE_1_0.txt)
  12. //
  13. #include <boost/config.hpp>
  14. #ifdef BOOST_HAS_PRAGMA_ONCE
  15. #pragma once
  16. #endif
  17. // BOOST_INTERLOCKED_HAS_INTRIN_H
  18. // VC9 has intrin.h, but it collides with <utility>
  19. #if defined( BOOST_MSVC ) && BOOST_MSVC >= 1600
  20. # define BOOST_INTERLOCKED_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_INTERLOCKED_HAS_INTRIN_H
  25. #elif defined( __CYGWIN__ )
  26. // Cygwin and Cygwin64 provide intrin.h. On Cygwin64 we have to use intrin.h because it's an LP64 target,
  27. // where long is 64-bit and therefore _Interlocked* functions have different signatures.
  28. # define BOOST_INTERLOCKED_HAS_INTRIN_H
  29. // Intel C++ on Windows on VC10+ stdlib
  30. #elif defined( BOOST_INTEL_WIN ) && defined( _CPPLIB_VER ) && _CPPLIB_VER >= 520
  31. # define BOOST_INTERLOCKED_HAS_INTRIN_H
  32. // clang-cl on Windows on VC10+ stdlib
  33. #elif defined( __clang__ ) && defined( _MSC_VER ) && defined( _CPPLIB_VER ) && _CPPLIB_VER >= 520
  34. # define BOOST_INTERLOCKED_HAS_INTRIN_H
  35. #endif
  36. #if !defined(__LP64__)
  37. #define BOOST_INTERLOCKED_LONG32 long
  38. #else
  39. #define BOOST_INTERLOCKED_LONG32 int
  40. #endif
  41. #if defined( BOOST_USE_WINDOWS_H )
  42. # include <windows.h>
  43. # define BOOST_INTERLOCKED_INCREMENT(dest) \
  44. InterlockedIncrement((BOOST_INTERLOCKED_LONG32*)(dest))
  45. # define BOOST_INTERLOCKED_DECREMENT(dest) \
  46. InterlockedDecrement((BOOST_INTERLOCKED_LONG32*)(dest))
  47. # define BOOST_INTERLOCKED_COMPARE_EXCHANGE(dest, exchange, compare) \
  48. InterlockedCompareExchange((BOOST_INTERLOCKED_LONG32*)(dest), (BOOST_INTERLOCKED_LONG32)(exchange), (BOOST_INTERLOCKED_LONG32)(compare))
  49. # define BOOST_INTERLOCKED_EXCHANGE(dest, exchange) \
  50. InterlockedExchange((BOOST_INTERLOCKED_LONG32*)(dest), (BOOST_INTERLOCKED_LONG32)(exchange))
  51. # define BOOST_INTERLOCKED_EXCHANGE_ADD(dest, add) \
  52. InterlockedExchangeAdd((BOOST_INTERLOCKED_LONG32*)(dest), (BOOST_INTERLOCKED_LONG32)(add))
  53. # define BOOST_INTERLOCKED_COMPARE_EXCHANGE_POINTER(dest, exchange, compare) \
  54. InterlockedCompareExchangePointer((void**)(dest), (void*)(exchange), (void*)(compare))
  55. # define BOOST_INTERLOCKED_EXCHANGE_POINTER(dest, exchange) \
  56. InterlockedExchangePointer((void**)(dest), (void*)(exchange))
  57. #elif defined( BOOST_USE_INTRIN_H ) || defined( BOOST_INTERLOCKED_HAS_INTRIN_H )
  58. #include <intrin.h>
  59. # define BOOST_INTERLOCKED_INCREMENT(dest) \
  60. _InterlockedIncrement((BOOST_INTERLOCKED_LONG32*)(dest))
  61. # define BOOST_INTERLOCKED_DECREMENT(dest) \
  62. _InterlockedDecrement((BOOST_INTERLOCKED_LONG32*)(dest))
  63. # define BOOST_INTERLOCKED_COMPARE_EXCHANGE(dest, exchange, compare) \
  64. _InterlockedCompareExchange((BOOST_INTERLOCKED_LONG32*)(dest), (BOOST_INTERLOCKED_LONG32)(exchange), (BOOST_INTERLOCKED_LONG32)(compare))
  65. # define BOOST_INTERLOCKED_EXCHANGE(dest, exchange) \
  66. _InterlockedExchange((BOOST_INTERLOCKED_LONG32*)(dest), (BOOST_INTERLOCKED_LONG32)(exchange))
  67. # define BOOST_INTERLOCKED_EXCHANGE_ADD(dest, add) \
  68. _InterlockedExchangeAdd((BOOST_INTERLOCKED_LONG32*)(dest), (BOOST_INTERLOCKED_LONG32)(add))
  69. // Note: Though MSVC-12 defines _InterlockedCompareExchangePointer and _InterlockedExchangePointer in intrin.h, the latter
  70. // is actually broken as it conflicts with winnt.h from Windows SDK 8.1.
  71. # if (defined(_MSC_VER) && _MSC_VER >= 1900) || \
  72. (defined(_M_IA64) || defined(_M_AMD64) || defined(__x86_64__) || defined(__x86_64) || defined(_M_ARM64))
  73. # define BOOST_INTERLOCKED_COMPARE_EXCHANGE_POINTER(dest, exchange, compare) \
  74. _InterlockedCompareExchangePointer((void**)(dest), (void*)(exchange), (void*)(compare))
  75. # define BOOST_INTERLOCKED_EXCHANGE_POINTER(dest, exchange) \
  76. _InterlockedExchangePointer((void**)(dest), (void*)(exchange))
  77. # else
  78. # define BOOST_INTERLOCKED_COMPARE_EXCHANGE_POINTER(dest, exchange, compare) \
  79. ((void*)BOOST_INTERLOCKED_COMPARE_EXCHANGE((BOOST_INTERLOCKED_LONG32*)(dest), (BOOST_INTERLOCKED_LONG32)(exchange), (BOOST_INTERLOCKED_LONG32)(compare)))
  80. # define BOOST_INTERLOCKED_EXCHANGE_POINTER(dest, exchange) \
  81. ((void*)BOOST_INTERLOCKED_EXCHANGE((BOOST_INTERLOCKED_LONG32*)(dest), (BOOST_INTERLOCKED_LONG32)(exchange)))
  82. # endif
  83. #elif defined(_WIN32_WCE)
  84. #if _WIN32_WCE >= 0x600
  85. extern "C" BOOST_INTERLOCKED_LONG32 __cdecl _InterlockedIncrement( BOOST_INTERLOCKED_LONG32 volatile * );
  86. extern "C" BOOST_INTERLOCKED_LONG32 __cdecl _InterlockedDecrement( BOOST_INTERLOCKED_LONG32 volatile * );
  87. extern "C" BOOST_INTERLOCKED_LONG32 __cdecl _InterlockedCompareExchange( BOOST_INTERLOCKED_LONG32 volatile *, BOOST_INTERLOCKED_LONG32, BOOST_INTERLOCKED_LONG32 );
  88. extern "C" BOOST_INTERLOCKED_LONG32 __cdecl _InterlockedExchange( BOOST_INTERLOCKED_LONG32 volatile *, BOOST_INTERLOCKED_LONG32 );
  89. extern "C" BOOST_INTERLOCKED_LONG32 __cdecl _InterlockedExchangeAdd( BOOST_INTERLOCKED_LONG32 volatile *, BOOST_INTERLOCKED_LONG32 );
  90. # define BOOST_INTERLOCKED_INCREMENT(dest) \
  91. _InterlockedIncrement((BOOST_INTERLOCKED_LONG32*)(dest))
  92. # define BOOST_INTERLOCKED_DECREMENT(dest) \
  93. _InterlockedDecrement((BOOST_INTERLOCKED_LONG32*)(dest))
  94. # define BOOST_INTERLOCKED_COMPARE_EXCHANGE(dest, exchange, compare) \
  95. _InterlockedCompareExchange((BOOST_INTERLOCKED_LONG32*)(dest), (BOOST_INTERLOCKED_LONG32)(exchange), (BOOST_INTERLOCKED_LONG32)(compare))
  96. # define BOOST_INTERLOCKED_EXCHANGE(dest, exchange) \
  97. _InterlockedExchange((BOOST_INTERLOCKED_LONG32*)(dest), (BOOST_INTERLOCKED_LONG32)(exchange))
  98. # define BOOST_INTERLOCKED_EXCHANGE_ADD(dest, add) \
  99. _InterlockedExchangeAdd((BOOST_INTERLOCKED_LONG32*)(dest), (BOOST_INTERLOCKED_LONG32)(add))
  100. #else // _WIN32_WCE >= 0x600
  101. // under Windows CE we still have old-style Interlocked* functions
  102. extern "C" BOOST_INTERLOCKED_LONG32 __cdecl InterlockedIncrement( BOOST_INTERLOCKED_LONG32 * );
  103. extern "C" BOOST_INTERLOCKED_LONG32 __cdecl InterlockedDecrement( BOOST_INTERLOCKED_LONG32 * );
  104. extern "C" BOOST_INTERLOCKED_LONG32 __cdecl InterlockedCompareExchange( BOOST_INTERLOCKED_LONG32 *, BOOST_INTERLOCKED_LONG32, BOOST_INTERLOCKED_LONG32 );
  105. extern "C" BOOST_INTERLOCKED_LONG32 __cdecl InterlockedExchange( BOOST_INTERLOCKED_LONG32 *, BOOST_INTERLOCKED_LONG32 );
  106. extern "C" BOOST_INTERLOCKED_LONG32 __cdecl InterlockedExchangeAdd( BOOST_INTERLOCKED_LONG32 *, BOOST_INTERLOCKED_LONG32 );
  107. # define BOOST_INTERLOCKED_INCREMENT(dest) \
  108. InterlockedIncrement((BOOST_INTERLOCKED_LONG32*)(dest))
  109. # define BOOST_INTERLOCKED_DECREMENT(dest) \
  110. InterlockedDecrement((BOOST_INTERLOCKED_LONG32*)(dest))
  111. # define BOOST_INTERLOCKED_COMPARE_EXCHANGE(dest, exchange, compare) \
  112. InterlockedCompareExchange((BOOST_INTERLOCKED_LONG32*)(dest), (BOOST_INTERLOCKED_LONG32)(exchange), (BOOST_INTERLOCKED_LONG32)(compare))
  113. # define BOOST_INTERLOCKED_EXCHANGE(dest, exchange) \
  114. InterlockedExchange((BOOST_INTERLOCKED_LONG32*)(dest), (BOOST_INTERLOCKED_LONG32)(exchange))
  115. # define BOOST_INTERLOCKED_EXCHANGE_ADD(dest, add) \
  116. InterlockedExchangeAdd((BOOST_INTERLOCKED_LONG32*)(dest), (BOOST_INTERLOCKED_LONG32)(add))
  117. #endif // _WIN32_WCE >= 0x600
  118. # define BOOST_INTERLOCKED_COMPARE_EXCHANGE_POINTER(dest, exchange, compare) \
  119. ((void*)BOOST_INTERLOCKED_COMPARE_EXCHANGE((BOOST_INTERLOCKED_LONG32*)(dest), (BOOST_INTERLOCKED_LONG32)(exchange), (BOOST_INTERLOCKED_LONG32)(compare)))
  120. # define BOOST_INTERLOCKED_EXCHANGE_POINTER(dest, exchange) \
  121. ((void*)BOOST_INTERLOCKED_EXCHANGE((BOOST_INTERLOCKED_LONG32*)(dest), (BOOST_INTERLOCKED_LONG32)(exchange)))
  122. #elif defined( BOOST_MSVC ) || defined( BOOST_INTEL_WIN )
  123. # if defined( __CLRCALL_PURE_OR_CDECL )
  124. # define BOOST_INTERLOCKED_CLRCALL_PURE_OR_CDECL __CLRCALL_PURE_OR_CDECL
  125. # else
  126. # define BOOST_INTERLOCKED_CLRCALL_PURE_OR_CDECL __cdecl
  127. # endif
  128. extern "C" BOOST_INTERLOCKED_LONG32 BOOST_INTERLOCKED_CLRCALL_PURE_OR_CDECL _InterlockedIncrement( BOOST_INTERLOCKED_LONG32 volatile * );
  129. extern "C" BOOST_INTERLOCKED_LONG32 BOOST_INTERLOCKED_CLRCALL_PURE_OR_CDECL _InterlockedDecrement( BOOST_INTERLOCKED_LONG32 volatile * );
  130. extern "C" BOOST_INTERLOCKED_LONG32 BOOST_INTERLOCKED_CLRCALL_PURE_OR_CDECL _InterlockedCompareExchange( BOOST_INTERLOCKED_LONG32 volatile *, BOOST_INTERLOCKED_LONG32, BOOST_INTERLOCKED_LONG32 );
  131. extern "C" BOOST_INTERLOCKED_LONG32 BOOST_INTERLOCKED_CLRCALL_PURE_OR_CDECL _InterlockedExchange( BOOST_INTERLOCKED_LONG32 volatile *, BOOST_INTERLOCKED_LONG32 );
  132. extern "C" BOOST_INTERLOCKED_LONG32 BOOST_INTERLOCKED_CLRCALL_PURE_OR_CDECL _InterlockedExchangeAdd( BOOST_INTERLOCKED_LONG32 volatile *, BOOST_INTERLOCKED_LONG32 );
  133. # if defined( BOOST_MSVC ) && BOOST_MSVC >= 1310
  134. # pragma intrinsic( _InterlockedIncrement )
  135. # pragma intrinsic( _InterlockedDecrement )
  136. # pragma intrinsic( _InterlockedCompareExchange )
  137. # pragma intrinsic( _InterlockedExchange )
  138. # pragma intrinsic( _InterlockedExchangeAdd )
  139. # endif
  140. # if defined(_M_IA64) || defined(_M_AMD64) || defined(_M_ARM64)
  141. extern "C" void* BOOST_INTERLOCKED_CLRCALL_PURE_OR_CDECL _InterlockedCompareExchangePointer( void* volatile *, void*, void* );
  142. extern "C" void* BOOST_INTERLOCKED_CLRCALL_PURE_OR_CDECL _InterlockedExchangePointer( void* volatile *, void* );
  143. # if defined( BOOST_MSVC ) && BOOST_MSVC >= 1310
  144. # pragma intrinsic( _InterlockedCompareExchangePointer )
  145. # pragma intrinsic( _InterlockedExchangePointer )
  146. # endif
  147. # define BOOST_INTERLOCKED_COMPARE_EXCHANGE_POINTER(dest, exchange, compare) \
  148. _InterlockedCompareExchangePointer((void**)(dest), (void*)(exchange), (void*)(compare))
  149. # define BOOST_INTERLOCKED_EXCHANGE_POINTER(dest, exchange) \
  150. _InterlockedExchangePointer((void**)(dest), (void*)(exchange))
  151. # else
  152. # define BOOST_INTERLOCKED_COMPARE_EXCHANGE_POINTER(dest, exchange, compare) \
  153. ((void*)BOOST_INTERLOCKED_COMPARE_EXCHANGE((BOOST_INTERLOCKED_LONG32*)(dest), (BOOST_INTERLOCKED_LONG32)(exchange), (BOOST_INTERLOCKED_LONG32)(compare)))
  154. # define BOOST_INTERLOCKED_EXCHANGE_POINTER(dest, exchange) \
  155. ((void*)BOOST_INTERLOCKED_EXCHANGE((BOOST_INTERLOCKED_LONG32*)(dest), (BOOST_INTERLOCKED_LONG32)(exchange)))
  156. # endif
  157. # undef BOOST_INTERLOCKED_CLRCALL_PURE_OR_CDECL
  158. # define BOOST_INTERLOCKED_INCREMENT(dest) \
  159. _InterlockedIncrement((BOOST_INTERLOCKED_LONG32*)(dest))
  160. # define BOOST_INTERLOCKED_DECREMENT(dest) \
  161. _InterlockedDecrement((BOOST_INTERLOCKED_LONG32*)(dest))
  162. # define BOOST_INTERLOCKED_COMPARE_EXCHANGE(dest, exchange, compare) \
  163. _InterlockedCompareExchange((BOOST_INTERLOCKED_LONG32*)(dest), (BOOST_INTERLOCKED_LONG32)(exchange), (BOOST_INTERLOCKED_LONG32)(compare))
  164. # define BOOST_INTERLOCKED_EXCHANGE(dest, exchange) \
  165. _InterlockedExchange((BOOST_INTERLOCKED_LONG32*)(dest), (BOOST_INTERLOCKED_LONG32)(exchange))
  166. # define BOOST_INTERLOCKED_EXCHANGE_ADD(dest, add) \
  167. _InterlockedExchangeAdd((BOOST_INTERLOCKED_LONG32*)(dest), (BOOST_INTERLOCKED_LONG32)(add))
  168. #elif defined( WIN32 ) || defined( _WIN32 ) || defined( __WIN32__ )
  169. #define BOOST_INTERLOCKED_IMPORT __declspec(dllimport)
  170. namespace boost
  171. {
  172. namespace detail
  173. {
  174. extern "C" BOOST_INTERLOCKED_IMPORT BOOST_INTERLOCKED_LONG32 __stdcall InterlockedIncrement( BOOST_INTERLOCKED_LONG32 volatile * );
  175. extern "C" BOOST_INTERLOCKED_IMPORT BOOST_INTERLOCKED_LONG32 __stdcall InterlockedDecrement( BOOST_INTERLOCKED_LONG32 volatile * );
  176. extern "C" BOOST_INTERLOCKED_IMPORT BOOST_INTERLOCKED_LONG32 __stdcall InterlockedCompareExchange( BOOST_INTERLOCKED_LONG32 volatile *, BOOST_INTERLOCKED_LONG32, BOOST_INTERLOCKED_LONG32 );
  177. extern "C" BOOST_INTERLOCKED_IMPORT BOOST_INTERLOCKED_LONG32 __stdcall InterlockedExchange( BOOST_INTERLOCKED_LONG32 volatile *, BOOST_INTERLOCKED_LONG32 );
  178. extern "C" BOOST_INTERLOCKED_IMPORT BOOST_INTERLOCKED_LONG32 __stdcall InterlockedExchangeAdd( BOOST_INTERLOCKED_LONG32 volatile *, BOOST_INTERLOCKED_LONG32 );
  179. # if defined(_M_IA64) || defined(_M_AMD64) || defined(_M_ARM64)
  180. extern "C" BOOST_INTERLOCKED_IMPORT void* __stdcall InterlockedCompareExchangePointer( void* volatile *, void*, void* );
  181. extern "C" BOOST_INTERLOCKED_IMPORT void* __stdcall InterlockedExchangePointer( void* volatile *, void* );
  182. # endif
  183. } // namespace detail
  184. } // namespace boost
  185. # define BOOST_INTERLOCKED_INCREMENT(dest) \
  186. ::boost::detail::InterlockedIncrement((BOOST_INTERLOCKED_LONG32*)(dest))
  187. # define BOOST_INTERLOCKED_DECREMENT(dest) \
  188. ::boost::detail::InterlockedDecrement((BOOST_INTERLOCKED_LONG32*)(dest))
  189. # define BOOST_INTERLOCKED_COMPARE_EXCHANGE(dest, exchange, compare) \
  190. ::boost::detail::InterlockedCompareExchange((BOOST_INTERLOCKED_LONG32*)(dest), (BOOST_INTERLOCKED_LONG32)(exchange), (BOOST_INTERLOCKED_LONG32)(compare))
  191. # define BOOST_INTERLOCKED_EXCHANGE(dest, exchange) \
  192. ::boost::detail::InterlockedExchange((BOOST_INTERLOCKED_LONG32*)(dest), (BOOST_INTERLOCKED_LONG32)(exchange))
  193. # define BOOST_INTERLOCKED_EXCHANGE_ADD(dest, add) \
  194. ::boost::detail::InterlockedExchangeAdd((BOOST_INTERLOCKED_LONG32*)(dest), (BOOST_INTERLOCKED_LONG32)(add))
  195. # if defined(_M_IA64) || defined(_M_AMD64) || defined(_M_ARM64)
  196. # define BOOST_INTERLOCKED_COMPARE_EXCHANGE_POINTER(dest, exchange, compare) \
  197. ::boost::detail::InterlockedCompareExchangePointer((void**)(dest), (void*)(exchange), (void*)(compare))
  198. # define BOOST_INTERLOCKED_EXCHANGE_POINTER(dest, exchange) \
  199. ::boost::detail::InterlockedExchangePointer((void**)(dest), (void*)(exchange))
  200. # else
  201. # define BOOST_INTERLOCKED_COMPARE_EXCHANGE_POINTER(dest, exchange, compare) \
  202. ((void*)BOOST_INTERLOCKED_COMPARE_EXCHANGE((BOOST_INTERLOCKED_LONG32 volatile*)(dest),(BOOST_INTERLOCKED_LONG32)(exchange),(BOOST_INTERLOCKED_LONG32)(compare)))
  203. # define BOOST_INTERLOCKED_EXCHANGE_POINTER(dest, exchange) \
  204. ((void*)BOOST_INTERLOCKED_EXCHANGE((BOOST_INTERLOCKED_LONG32*)(dest),(BOOST_INTERLOCKED_LONG32)(exchange)))
  205. # endif
  206. #else
  207. # error "Interlocked intrinsics not available"
  208. #endif
  209. #endif // #ifndef BOOST_DETAIL_INTERLOCKED_HPP_INCLUDED