lwm_win32_cs.hpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. #endif
  21. namespace boost
  22. {
  23. namespace signals2
  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(critical_section *);
  40. extern "C" __declspec(dllimport) void __stdcall EnterCriticalSection(critical_section *);
  41. extern "C" __declspec(dllimport) bool __stdcall TryEnterCriticalSection(critical_section *);
  42. extern "C" __declspec(dllimport) void __stdcall LeaveCriticalSection(critical_section *);
  43. extern "C" __declspec(dllimport) void __stdcall DeleteCriticalSection(critical_section *);
  44. #else
  45. typedef ::CRITICAL_SECTION critical_section;
  46. #endif // #ifndef BOOST_USE_WINDOWS_H
  47. class mutex
  48. {
  49. private:
  50. critical_section cs_;
  51. mutex(mutex const &);
  52. mutex & operator=(mutex const &);
  53. public:
  54. mutex()
  55. {
  56. InitializeCriticalSection(&cs_);
  57. }
  58. ~mutex()
  59. {
  60. DeleteCriticalSection(&cs_);
  61. }
  62. void lock()
  63. {
  64. EnterCriticalSection(&cs_);
  65. }
  66. // TryEnterCriticalSection only exists on Windows NT 4.0 and later
  67. #if (defined(_WIN32_WINNT) && (_WIN32_WINNT >= 0x0400))
  68. bool try_lock()
  69. {
  70. return TryEnterCriticalSection(&cs_) != 0;
  71. }
  72. #else
  73. bool try_lock()
  74. {
  75. BOOST_ASSERT(false);
  76. return false;
  77. }
  78. #endif
  79. void unlock()
  80. {
  81. LeaveCriticalSection(&cs_);
  82. }
  83. };
  84. } // namespace signals2
  85. } // namespace boost
  86. #endif // #ifndef BOOST_SIGNALS2_LWM_WIN32_CS_HPP