lwm_win32_cs.hpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. //
  12. // Distributed under the Boost Software License, Version 1.0. (See
  13. // accompanying file LICENSE_1_0.txt or copy at
  14. // http://www.boost.org/LICENSE_1_0.txt)
  15. //
  16. #ifdef BOOST_USE_WINDOWS_H
  17. #include <windows.h>
  18. #else
  19. struct _RTL_CRITICAL_SECTION;
  20. #endif
  21. namespace boost
  22. {
  23. namespace detail
  24. {
  25. #ifndef BOOST_USE_WINDOWS_H
  26. struct critical_section
  27. {
  28. struct critical_section_debug * DebugInfo;
  29. long LockCount;
  30. long RecursionCount;
  31. void * OwningThread;
  32. void * LockSemaphore;
  33. #if defined(_WIN64)
  34. unsigned __int64 SpinCount;
  35. #else
  36. unsigned long SpinCount;
  37. #endif
  38. };
  39. extern "C" __declspec(dllimport) void __stdcall InitializeCriticalSection(::_RTL_CRITICAL_SECTION *);
  40. extern "C" __declspec(dllimport) void __stdcall EnterCriticalSection(::_RTL_CRITICAL_SECTION *);
  41. extern "C" __declspec(dllimport) void __stdcall LeaveCriticalSection(::_RTL_CRITICAL_SECTION *);
  42. extern "C" __declspec(dllimport) void __stdcall DeleteCriticalSection(::_RTL_CRITICAL_SECTION *);
  43. typedef ::_RTL_CRITICAL_SECTION rtl_critical_section;
  44. #else // #ifndef BOOST_USE_WINDOWS_H
  45. typedef ::CRITICAL_SECTION critical_section;
  46. using ::InitializeCriticalSection;
  47. using ::EnterCriticalSection;
  48. using ::LeaveCriticalSection;
  49. using ::DeleteCriticalSection;
  50. typedef ::CRITICAL_SECTION rtl_critical_section;
  51. #endif // #ifndef BOOST_USE_WINDOWS_H
  52. class lightweight_mutex
  53. {
  54. private:
  55. critical_section cs_;
  56. lightweight_mutex(lightweight_mutex const &);
  57. lightweight_mutex & operator=(lightweight_mutex const &);
  58. public:
  59. lightweight_mutex()
  60. {
  61. boost::detail::InitializeCriticalSection(reinterpret_cast< rtl_critical_section* >(&cs_));
  62. }
  63. ~lightweight_mutex()
  64. {
  65. boost::detail::DeleteCriticalSection(reinterpret_cast< rtl_critical_section* >(&cs_));
  66. }
  67. class scoped_lock;
  68. friend class scoped_lock;
  69. class scoped_lock
  70. {
  71. private:
  72. lightweight_mutex & m_;
  73. scoped_lock(scoped_lock const &);
  74. scoped_lock & operator=(scoped_lock const &);
  75. public:
  76. explicit scoped_lock(lightweight_mutex & m): m_(m)
  77. {
  78. boost::detail::EnterCriticalSection(reinterpret_cast< rtl_critical_section* >(&m_.cs_));
  79. }
  80. ~scoped_lock()
  81. {
  82. boost::detail::LeaveCriticalSection(reinterpret_cast< rtl_critical_section* >(&m_.cs_));
  83. }
  84. };
  85. };
  86. } // namespace detail
  87. } // namespace boost
  88. #endif // #ifndef BOOST_SMART_PTR_DETAIL_LWM_WIN32_CS_HPP_INCLUDED