lwm_win32_cs.hpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. //
  7. // Distributed under the Boost Software License, Version 1.0. (See
  8. // accompanying file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. //
  11. #ifndef BOOST_SIGNALS2_LWM_WIN32_CS_HPP
  12. #define BOOST_SIGNALS2_LWM_WIN32_CS_HPP
  13. // MS compatible compilers support #pragma once
  14. #if defined(_MSC_VER) && (_MSC_VER >= 1020)
  15. # pragma once
  16. #endif
  17. #include <boost/assert.hpp>
  18. #ifdef BOOST_USE_WINDOWS_H
  19. #include <windows.h>
  20. #else
  21. struct _RTL_CRITICAL_SECTION;
  22. #endif
  23. namespace boost
  24. {
  25. namespace signals2
  26. {
  27. namespace detail
  28. {
  29. #ifndef BOOST_USE_WINDOWS_H
  30. struct critical_section
  31. {
  32. struct critical_section_debug * DebugInfo;
  33. long LockCount;
  34. long RecursionCount;
  35. void * OwningThread;
  36. void * LockSemaphore;
  37. #if defined(_WIN64)
  38. unsigned __int64 SpinCount;
  39. #else
  40. unsigned long SpinCount;
  41. #endif
  42. };
  43. extern "C" __declspec(dllimport) void __stdcall InitializeCriticalSection(::_RTL_CRITICAL_SECTION *);
  44. extern "C" __declspec(dllimport) void __stdcall EnterCriticalSection(::_RTL_CRITICAL_SECTION *);
  45. extern "C" __declspec(dllimport) int __stdcall TryEnterCriticalSection(::_RTL_CRITICAL_SECTION *);
  46. extern "C" __declspec(dllimport) void __stdcall LeaveCriticalSection(::_RTL_CRITICAL_SECTION *);
  47. extern "C" __declspec(dllimport) void __stdcall DeleteCriticalSection(::_RTL_CRITICAL_SECTION *);
  48. typedef ::_RTL_CRITICAL_SECTION rtl_critical_section;
  49. #else // #ifndef BOOST_USE_WINDOWS_H
  50. typedef ::CRITICAL_SECTION critical_section;
  51. using ::InitializeCriticalSection;
  52. using ::EnterCriticalSection;
  53. using ::TryEnterCriticalSection;
  54. using ::LeaveCriticalSection;
  55. using ::DeleteCriticalSection;
  56. typedef ::CRITICAL_SECTION rtl_critical_section;
  57. #endif // #ifndef BOOST_USE_WINDOWS_H
  58. } // namespace detail
  59. class mutex
  60. {
  61. private:
  62. boost::signals2::detail::critical_section cs_;
  63. mutex(mutex const &);
  64. mutex & operator=(mutex const &);
  65. public:
  66. mutex()
  67. {
  68. boost::signals2::detail::InitializeCriticalSection(reinterpret_cast< boost::signals2::detail::rtl_critical_section* >(&cs_));
  69. }
  70. ~mutex()
  71. {
  72. boost::signals2::detail::DeleteCriticalSection(reinterpret_cast< boost::signals2::detail::rtl_critical_section* >(&cs_));
  73. }
  74. void lock()
  75. {
  76. boost::signals2::detail::EnterCriticalSection(reinterpret_cast< boost::signals2::detail::rtl_critical_section* >(&cs_));
  77. }
  78. // TryEnterCriticalSection only exists on Windows NT 4.0 and later
  79. #if (defined(_WIN32_WINNT) && (_WIN32_WINNT >= 0x0400))
  80. bool try_lock()
  81. {
  82. return boost::signals2::detail::TryEnterCriticalSection(reinterpret_cast< boost::signals2::detail::rtl_critical_section* >(&cs_)) != 0;
  83. }
  84. #else
  85. bool try_lock()
  86. {
  87. BOOST_ASSERT(false);
  88. return false;
  89. }
  90. #endif
  91. void unlock()
  92. {
  93. boost::signals2::detail::LeaveCriticalSection(reinterpret_cast< boost::signals2::detail::rtl_critical_section* >(&cs_));
  94. }
  95. };
  96. } // namespace signals2
  97. } // namespace boost
  98. #endif // #ifndef BOOST_SIGNALS2_LWM_WIN32_CS_HPP