lwm_win32_cs.hpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #ifndef BOOST_SMART_PTR_DETAIL_LWM_WIN32_CS_HPP_INCLUDED
  2. #define BOOST_SMART_PTR_DETAIL_LWM_WIN32_CS_HPP_INCLUDED
  3. // MS compatible compilers support #pragma once
  4. #if defined(_MSC_VER) && (_MSC_VER >= 1020)
  5. # pragma once
  6. #endif
  7. //
  8. // boost/detail/lwm_win32_cs.hpp
  9. //
  10. // Copyright (c) 2002, 2003 Peter Dimov
  11. // Copyright (c) Microsoft Corporation 2014
  12. //
  13. // Distributed under the Boost Software License, Version 1.0. (See
  14. // accompanying file LICENSE_1_0.txt or copy at
  15. // http://www.boost.org/LICENSE_1_0.txt)
  16. //
  17. #include <boost/predef.h>
  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 detail
  26. {
  27. #ifndef BOOST_USE_WINDOWS_H
  28. struct critical_section
  29. {
  30. struct critical_section_debug * DebugInfo;
  31. long LockCount;
  32. long RecursionCount;
  33. void * OwningThread;
  34. void * LockSemaphore;
  35. #if defined(_WIN64)
  36. unsigned __int64 SpinCount;
  37. #else
  38. unsigned long SpinCount;
  39. #endif
  40. };
  41. #if BOOST_PLAT_WINDOWS_RUNTIME
  42. extern "C" __declspec(dllimport) void __stdcall InitializeCriticalSectionEx(::_RTL_CRITICAL_SECTION *, unsigned long, unsigned long);
  43. #else
  44. extern "C" __declspec(dllimport) void __stdcall InitializeCriticalSection(::_RTL_CRITICAL_SECTION *);
  45. #endif
  46. extern "C" __declspec(dllimport) void __stdcall EnterCriticalSection(::_RTL_CRITICAL_SECTION *);
  47. extern "C" __declspec(dllimport) void __stdcall LeaveCriticalSection(::_RTL_CRITICAL_SECTION *);
  48. extern "C" __declspec(dllimport) void __stdcall DeleteCriticalSection(::_RTL_CRITICAL_SECTION *);
  49. typedef ::_RTL_CRITICAL_SECTION rtl_critical_section;
  50. #else // #ifndef BOOST_USE_WINDOWS_H
  51. typedef ::CRITICAL_SECTION critical_section;
  52. #if BOOST_PLAT_WINDOWS_RUNTIME
  53. using ::InitializeCriticalSectionEx;
  54. #else
  55. using ::InitializeCriticalSection;
  56. #endif
  57. using ::EnterCriticalSection;
  58. using ::LeaveCriticalSection;
  59. using ::DeleteCriticalSection;
  60. typedef ::CRITICAL_SECTION rtl_critical_section;
  61. #endif // #ifndef BOOST_USE_WINDOWS_H
  62. class lightweight_mutex
  63. {
  64. private:
  65. critical_section cs_;
  66. lightweight_mutex(lightweight_mutex const &);
  67. lightweight_mutex & operator=(lightweight_mutex const &);
  68. public:
  69. lightweight_mutex()
  70. {
  71. #if BOOST_PLAT_WINDOWS_RUNTIME
  72. boost::detail::InitializeCriticalSectionEx(reinterpret_cast< rtl_critical_section* >(&cs_), 4000, 0);
  73. #else
  74. boost::detail::InitializeCriticalSection(reinterpret_cast< rtl_critical_section* >(&cs_));
  75. #endif
  76. }
  77. ~lightweight_mutex()
  78. {
  79. boost::detail::DeleteCriticalSection(reinterpret_cast< rtl_critical_section* >(&cs_));
  80. }
  81. class scoped_lock;
  82. friend class scoped_lock;
  83. class scoped_lock
  84. {
  85. private:
  86. lightweight_mutex & m_;
  87. scoped_lock(scoped_lock const &);
  88. scoped_lock & operator=(scoped_lock const &);
  89. public:
  90. explicit scoped_lock(lightweight_mutex & m): m_(m)
  91. {
  92. boost::detail::EnterCriticalSection(reinterpret_cast< rtl_critical_section* >(&m_.cs_));
  93. }
  94. ~scoped_lock()
  95. {
  96. boost::detail::LeaveCriticalSection(reinterpret_cast< rtl_critical_section* >(&m_.cs_));
  97. }
  98. };
  99. };
  100. } // namespace detail
  101. } // namespace boost
  102. #endif // #ifndef BOOST_SMART_PTR_DETAIL_LWM_WIN32_CS_HPP_INCLUDED