lwm_win32_cs.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. //
  2. // boost/signals2/detail/lwm_win32_cs.hpp
  3. //
  4. // Copyright (c) 2002, 2003 Peter Dimov
  5. // Copyright (c) 2008 Frank Mori Hess
  6. // Copyright (c) Microsoft Corporation 2014
  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. #ifndef BOOST_SIGNALS2_LWM_WIN32_CS_HPP
  13. #define BOOST_SIGNALS2_LWM_WIN32_CS_HPP
  14. // MS compatible compilers support #pragma once
  15. #if defined(_MSC_VER) && (_MSC_VER >= 1020)
  16. # pragma once
  17. #endif
  18. #include <boost/predef.h>
  19. #include <boost/assert.hpp>
  20. #ifdef BOOST_USE_WINDOWS_H
  21. #include <windows.h>
  22. #else
  23. struct _RTL_CRITICAL_SECTION;
  24. #endif
  25. namespace boost
  26. {
  27. namespace signals2
  28. {
  29. namespace detail
  30. {
  31. #ifndef BOOST_USE_WINDOWS_H
  32. struct critical_section
  33. {
  34. struct critical_section_debug * DebugInfo;
  35. long LockCount;
  36. long RecursionCount;
  37. void * OwningThread;
  38. void * LockSemaphore;
  39. #if defined(_WIN64)
  40. unsigned __int64 SpinCount;
  41. #else
  42. unsigned long SpinCount;
  43. #endif
  44. };
  45. #if BOOST_PLAT_WINDOWS_RUNTIME
  46. extern "C" __declspec(dllimport) void __stdcall InitializeCriticalSectionEx(::_RTL_CRITICAL_SECTION *, unsigned long, unsigned long);
  47. #else
  48. extern "C" __declspec(dllimport) void __stdcall InitializeCriticalSection(::_RTL_CRITICAL_SECTION *);
  49. #endif
  50. extern "C" __declspec(dllimport) void __stdcall EnterCriticalSection(::_RTL_CRITICAL_SECTION *);
  51. extern "C" __declspec(dllimport) int __stdcall TryEnterCriticalSection(::_RTL_CRITICAL_SECTION *);
  52. extern "C" __declspec(dllimport) void __stdcall LeaveCriticalSection(::_RTL_CRITICAL_SECTION *);
  53. extern "C" __declspec(dllimport) void __stdcall DeleteCriticalSection(::_RTL_CRITICAL_SECTION *);
  54. typedef ::_RTL_CRITICAL_SECTION rtl_critical_section;
  55. #else // #ifndef BOOST_USE_WINDOWS_H
  56. typedef ::CRITICAL_SECTION critical_section;
  57. #if BOOST_PLAT_WINDOWS_RUNTIME
  58. using ::InitializeCriticalSectionEx;
  59. #else
  60. using ::InitializeCriticalSection;
  61. #endif
  62. using ::EnterCriticalSection;
  63. using ::TryEnterCriticalSection;
  64. using ::LeaveCriticalSection;
  65. using ::DeleteCriticalSection;
  66. typedef ::CRITICAL_SECTION rtl_critical_section;
  67. #endif // #ifndef BOOST_USE_WINDOWS_H
  68. } // namespace detail
  69. class mutex
  70. {
  71. private:
  72. boost::signals2::detail::critical_section cs_;
  73. mutex(mutex const &);
  74. mutex & operator=(mutex const &);
  75. public:
  76. mutex()
  77. {
  78. #if BOOST_PLAT_WINDOWS_RUNTIME
  79. boost::signals2::detail::InitializeCriticalSectionEx(reinterpret_cast< boost::signals2::detail::rtl_critical_section* >(&cs_), 4000, 0);
  80. #else
  81. boost::signals2::detail::InitializeCriticalSection(reinterpret_cast< boost::signals2::detail::rtl_critical_section* >(&cs_));
  82. #endif
  83. }
  84. ~mutex()
  85. {
  86. boost::signals2::detail::DeleteCriticalSection(reinterpret_cast< boost::signals2::detail::rtl_critical_section* >(&cs_));
  87. }
  88. void lock()
  89. {
  90. boost::signals2::detail::EnterCriticalSection(reinterpret_cast< boost::signals2::detail::rtl_critical_section* >(&cs_));
  91. }
  92. // TryEnterCriticalSection only exists on Windows NT 4.0 and later
  93. #if (defined(_WIN32_WINNT) && (_WIN32_WINNT >= 0x0400))
  94. bool try_lock()
  95. {
  96. return boost::signals2::detail::TryEnterCriticalSection(reinterpret_cast< boost::signals2::detail::rtl_critical_section* >(&cs_)) != 0;
  97. }
  98. #else
  99. bool try_lock()
  100. {
  101. BOOST_ASSERT(false);
  102. return false;
  103. }
  104. #endif
  105. void unlock()
  106. {
  107. boost::signals2::detail::LeaveCriticalSection(reinterpret_cast< boost::signals2::detail::rtl_critical_section* >(&cs_));
  108. }
  109. };
  110. } // namespace signals2
  111. } // namespace boost
  112. #endif // #ifndef BOOST_SIGNALS2_LWM_WIN32_CS_HPP