critical_section.hpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. * Copyright 2010 Vicente J. Botet Escriba
  3. * Copyright 2015 Andrey Semashev
  4. *
  5. * Distributed under the Boost Software License, Version 1.0.
  6. * See http://www.boost.org/LICENSE_1_0.txt
  7. */
  8. #ifndef BOOST_WINAPI_CRITICAL_SECTION_HPP_INCLUDED_
  9. #define BOOST_WINAPI_CRITICAL_SECTION_HPP_INCLUDED_
  10. #include <boost/winapi/basic_types.hpp>
  11. #include <boost/winapi/detail/cast_ptr.hpp>
  12. #include <boost/winapi/detail/header.hpp>
  13. #ifdef BOOST_HAS_PRAGMA_ONCE
  14. #pragma once
  15. #endif
  16. #if !defined( BOOST_USE_WINDOWS_H )
  17. extern "C" {
  18. #if !defined( BOOST_WINAPI_IS_MINGW )
  19. // Windows CE uses a different name for the structure
  20. #if defined (_WIN32_WCE)
  21. struct CRITICAL_SECTION;
  22. namespace boost {
  23. namespace winapi {
  24. namespace detail {
  25. typedef CRITICAL_SECTION winsdk_critical_section;
  26. }
  27. }
  28. }
  29. #else
  30. struct _RTL_CRITICAL_SECTION;
  31. namespace boost {
  32. namespace winapi {
  33. namespace detail {
  34. typedef _RTL_CRITICAL_SECTION winsdk_critical_section;
  35. }
  36. }
  37. }
  38. #endif
  39. #else
  40. // MinGW uses a different name for the structure
  41. struct _CRITICAL_SECTION;
  42. namespace boost {
  43. namespace winapi {
  44. namespace detail {
  45. typedef _CRITICAL_SECTION winapi_critical_section;
  46. }
  47. }
  48. }
  49. #endif
  50. #if !defined( BOOST_WINAPI_IS_MINGW )
  51. #if BOOST_WINAPI_PARTITION_APP_SYSTEM
  52. BOOST_WINAPI_IMPORT_EXCEPT_WM boost::winapi::VOID_ BOOST_WINAPI_WINAPI_CC
  53. InitializeCriticalSection(boost::winapi::detail::winsdk_critical_section* lpCriticalSection);
  54. #endif
  55. BOOST_WINAPI_IMPORT_EXCEPT_WM boost::winapi::VOID_ BOOST_WINAPI_WINAPI_CC
  56. EnterCriticalSection(boost::winapi::detail::winsdk_critical_section* lpCriticalSection);
  57. BOOST_WINAPI_IMPORT_EXCEPT_WM boost::winapi::VOID_ BOOST_WINAPI_WINAPI_CC
  58. LeaveCriticalSection(boost::winapi::detail::winsdk_critical_section* lpCriticalSection);
  59. #if BOOST_USE_WINAPI_VERSION >= 0x0403
  60. #if BOOST_WINAPI_PARTITION_APP_SYSTEM
  61. BOOST_WINAPI_IMPORT boost::winapi::BOOL_ BOOST_WINAPI_WINAPI_CC
  62. InitializeCriticalSectionAndSpinCount(
  63. boost::winapi::detail::winsdk_critical_section* lpCriticalSection,
  64. boost::winapi::DWORD_ dwSpinCount);
  65. BOOST_WINAPI_IMPORT boost::winapi::DWORD_ BOOST_WINAPI_WINAPI_CC
  66. SetCriticalSectionSpinCount(
  67. boost::winapi::detail::winsdk_critical_section* lpCriticalSection,
  68. boost::winapi::DWORD_ dwSpinCount);
  69. #endif
  70. #if BOOST_USE_WINAPI_VERSION >= BOOST_WINAPI_VERSION_WIN6
  71. BOOST_WINAPI_IMPORT boost::winapi::BOOL_ BOOST_WINAPI_WINAPI_CC
  72. InitializeCriticalSectionEx(
  73. boost::winapi::detail::winsdk_critical_section* lpCriticalSection,
  74. boost::winapi::DWORD_ dwSpinCount,
  75. boost::winapi::DWORD_ Flags);
  76. #endif
  77. #endif
  78. #if BOOST_USE_WINAPI_VERSION >= BOOST_WINAPI_VERSION_NT4
  79. BOOST_WINAPI_IMPORT_EXCEPT_WM boost::winapi::BOOL_ BOOST_WINAPI_WINAPI_CC
  80. TryEnterCriticalSection(boost::winapi::detail::winsdk_critical_section* lpCriticalSection);
  81. #endif
  82. BOOST_WINAPI_IMPORT_EXCEPT_WM boost::winapi::VOID_ BOOST_WINAPI_WINAPI_CC
  83. DeleteCriticalSection(boost::winapi::detail::winsdk_critical_section* lpCriticalSection);
  84. #else // defined( BOOST_WINAPI_IS_MINGW )
  85. BOOST_WINAPI_IMPORT_EXCEPT_WM boost::winapi::VOID_ BOOST_WINAPI_WINAPI_CC
  86. InitializeCriticalSection(boost::winapi::detail::winapi_critical_section* lpCriticalSection);
  87. BOOST_WINAPI_IMPORT_EXCEPT_WM boost::winapi::VOID_ BOOST_WINAPI_WINAPI_CC
  88. EnterCriticalSection(boost::winapi::detail::winapi_critical_section* lpCriticalSection);
  89. BOOST_WINAPI_IMPORT_EXCEPT_WM boost::winapi::VOID_ BOOST_WINAPI_WINAPI_CC
  90. LeaveCriticalSection(boost::winapi::detail::winapi_critical_section* lpCriticalSection);
  91. #if BOOST_USE_WINAPI_VERSION >= 0x0403
  92. BOOST_WINAPI_IMPORT boost::winapi::BOOL_ BOOST_WINAPI_WINAPI_CC
  93. InitializeCriticalSectionAndSpinCount(
  94. boost::winapi::detail::winapi_critical_section* lpCriticalSection,
  95. boost::winapi::DWORD_ dwSpinCount);
  96. BOOST_WINAPI_IMPORT boost::winapi::DWORD_ BOOST_WINAPI_WINAPI_CC
  97. SetCriticalSectionSpinCount(
  98. boost::winapi::detail::winapi_critical_section* lpCriticalSection,
  99. boost::winapi::DWORD_ dwSpinCount);
  100. #endif
  101. #if BOOST_USE_WINAPI_VERSION >= BOOST_WINAPI_VERSION_WIN6
  102. BOOST_WINAPI_IMPORT boost::winapi::BOOL_ BOOST_WINAPI_WINAPI_CC
  103. InitializeCriticalSectionEx(
  104. boost::winapi::detail::winapi_critical_section* lpCriticalSection,
  105. boost::winapi::DWORD_ dwSpinCount,
  106. boost::winapi::DWORD_ Flags);
  107. #endif
  108. #if BOOST_USE_WINAPI_VERSION >= BOOST_WINAPI_VERSION_NT4
  109. BOOST_WINAPI_IMPORT_EXCEPT_WM boost::winapi::BOOL_ BOOST_WINAPI_WINAPI_CC
  110. TryEnterCriticalSection(boost::winapi::detail::winapi_critical_section* lpCriticalSection);
  111. #endif
  112. BOOST_WINAPI_IMPORT_EXCEPT_WM boost::winapi::VOID_ BOOST_WINAPI_WINAPI_CC
  113. DeleteCriticalSection(boost::winapi::detail::winapi_critical_section* lpCriticalSection);
  114. #endif // defined( BOOST_WINAPI_IS_MINGW )
  115. } // extern "C"
  116. #endif
  117. namespace boost {
  118. namespace winapi {
  119. #pragma pack(push, 8)
  120. #if !defined(_WIN32_WCE)
  121. struct _RTL_CRITICAL_SECTION_DEBUG;
  122. typedef struct BOOST_MAY_ALIAS _RTL_CRITICAL_SECTION {
  123. _RTL_CRITICAL_SECTION_DEBUG* DebugInfo;
  124. LONG_ LockCount;
  125. LONG_ RecursionCount;
  126. HANDLE_ OwningThread;
  127. HANDLE_ LockSemaphore;
  128. ULONG_PTR_ SpinCount;
  129. } CRITICAL_SECTION_, *PCRITICAL_SECTION_;
  130. #else
  131. // Windows CE has different layout
  132. typedef struct BOOST_MAY_ALIAS CRITICAL_SECTION {
  133. unsigned int LockCount;
  134. HANDLE OwnerThread;
  135. HANDLE hCrit;
  136. DWORD needtrap;
  137. DWORD dwContentions;
  138. } CRITICAL_SECTION_, *LPCRITICAL_SECTION_;
  139. #endif
  140. #pragma pack(pop)
  141. #if BOOST_WINAPI_PARTITION_APP_SYSTEM
  142. BOOST_FORCEINLINE VOID_ InitializeCriticalSection(CRITICAL_SECTION_* lpCriticalSection)
  143. {
  144. ::InitializeCriticalSection(winapi::detail::cast_ptr(lpCriticalSection));
  145. }
  146. #endif
  147. BOOST_FORCEINLINE VOID_ EnterCriticalSection(CRITICAL_SECTION_* lpCriticalSection)
  148. {
  149. ::EnterCriticalSection(winapi::detail::cast_ptr(lpCriticalSection));
  150. }
  151. BOOST_FORCEINLINE VOID_ LeaveCriticalSection(CRITICAL_SECTION_* lpCriticalSection)
  152. {
  153. ::LeaveCriticalSection(winapi::detail::cast_ptr(lpCriticalSection));
  154. }
  155. #if BOOST_USE_WINAPI_VERSION >= 0x0403
  156. #if BOOST_WINAPI_PARTITION_APP_SYSTEM
  157. BOOST_FORCEINLINE BOOL_ InitializeCriticalSectionAndSpinCount(CRITICAL_SECTION_* lpCriticalSection, DWORD_ dwSpinCount)
  158. {
  159. return ::InitializeCriticalSectionAndSpinCount(winapi::detail::cast_ptr(lpCriticalSection), dwSpinCount);
  160. }
  161. BOOST_FORCEINLINE DWORD_ SetCriticalSectionSpinCount(CRITICAL_SECTION_* lpCriticalSection, DWORD_ dwSpinCount)
  162. {
  163. return ::SetCriticalSectionSpinCount(winapi::detail::cast_ptr(lpCriticalSection), dwSpinCount);
  164. }
  165. #endif
  166. // CRITICAL_SECTION_NO_DEBUG_INFO is defined for WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP)
  167. BOOST_CONSTEXPR_OR_CONST DWORD_ CRITICAL_SECTION_NO_DEBUG_INFO_ = 0x01000000;
  168. BOOST_CONSTEXPR_OR_CONST DWORD_ CRITICAL_SECTION_FLAG_NO_DEBUG_INFO_ = CRITICAL_SECTION_NO_DEBUG_INFO_;
  169. BOOST_CONSTEXPR_OR_CONST DWORD_ CRITICAL_SECTION_FLAG_DYNAMIC_SPIN_ = 0x02000000; // undocumented
  170. BOOST_CONSTEXPR_OR_CONST DWORD_ CRITICAL_SECTION_FLAG_STATIC_INIT_ = 0x04000000; // undocumented
  171. #if BOOST_USE_WINAPI_VERSION >= BOOST_WINAPI_VERSION_WIN6
  172. BOOST_FORCEINLINE BOOL_ InitializeCriticalSectionEx(CRITICAL_SECTION_* lpCriticalSection, DWORD_ dwSpinCount, DWORD_ Flags)
  173. {
  174. return ::InitializeCriticalSectionEx(winapi::detail::cast_ptr(lpCriticalSection), dwSpinCount, Flags);
  175. }
  176. #endif // BOOST_USE_WINAPI_VERSION >= BOOST_WINAPI_VERSION_WIN6
  177. #endif // BOOST_USE_WINAPI_VERSION >= 0x0403
  178. #if BOOST_USE_WINAPI_VERSION >= BOOST_WINAPI_VERSION_NT4
  179. BOOST_FORCEINLINE BOOL_ TryEnterCriticalSection(CRITICAL_SECTION_* lpCriticalSection)
  180. {
  181. return ::TryEnterCriticalSection(winapi::detail::cast_ptr(lpCriticalSection));
  182. }
  183. #endif // BOOST_USE_WINAPI_VERSION >= BOOST_WINAPI_VERSION_NT4
  184. BOOST_FORCEINLINE VOID_ DeleteCriticalSection(CRITICAL_SECTION_* lpCriticalSection)
  185. {
  186. ::DeleteCriticalSection(winapi::detail::cast_ptr(lpCriticalSection));
  187. }
  188. }
  189. }
  190. #include <boost/winapi/detail/footer.hpp>
  191. #endif // BOOST_WINAPI_CRITICAL_SECTION_HPP_INCLUDED_