lwm_win32_cs.hpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. #endif
  19. namespace boost
  20. {
  21. namespace detail
  22. {
  23. #ifndef BOOST_USE_WINDOWS_H
  24. struct critical_section
  25. {
  26. struct critical_section_debug * DebugInfo;
  27. long LockCount;
  28. long RecursionCount;
  29. void * OwningThread;
  30. void * LockSemaphore;
  31. #if defined(_WIN64)
  32. unsigned __int64 SpinCount;
  33. #else
  34. unsigned long SpinCount;
  35. #endif
  36. };
  37. extern "C" __declspec(dllimport) void __stdcall InitializeCriticalSection(critical_section *);
  38. extern "C" __declspec(dllimport) void __stdcall EnterCriticalSection(critical_section *);
  39. extern "C" __declspec(dllimport) void __stdcall LeaveCriticalSection(critical_section *);
  40. extern "C" __declspec(dllimport) void __stdcall DeleteCriticalSection(critical_section *);
  41. #else
  42. typedef ::CRITICAL_SECTION critical_section;
  43. #endif // #ifndef BOOST_USE_WINDOWS_H
  44. class lightweight_mutex
  45. {
  46. private:
  47. critical_section cs_;
  48. lightweight_mutex(lightweight_mutex const &);
  49. lightweight_mutex & operator=(lightweight_mutex const &);
  50. public:
  51. lightweight_mutex()
  52. {
  53. InitializeCriticalSection(&cs_);
  54. }
  55. ~lightweight_mutex()
  56. {
  57. DeleteCriticalSection(&cs_);
  58. }
  59. class scoped_lock;
  60. friend class scoped_lock;
  61. class scoped_lock
  62. {
  63. private:
  64. lightweight_mutex & m_;
  65. scoped_lock(scoped_lock const &);
  66. scoped_lock & operator=(scoped_lock const &);
  67. public:
  68. explicit scoped_lock(lightweight_mutex & m): m_(m)
  69. {
  70. EnterCriticalSection(&m_.cs_);
  71. }
  72. ~scoped_lock()
  73. {
  74. LeaveCriticalSection(&m_.cs_);
  75. }
  76. };
  77. };
  78. } // namespace detail
  79. } // namespace boost
  80. #endif // #ifndef BOOST_SMART_PTR_DETAIL_LWM_WIN32_CS_HPP_INCLUDED