win_mutex.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. //
  2. // win_mutex.hpp
  3. // ~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2008 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #ifndef BOOST_ASIO_DETAIL_WIN_MUTEX_HPP
  11. #define BOOST_ASIO_DETAIL_WIN_MUTEX_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <boost/asio/detail/push_options.hpp>
  16. #include <boost/asio/detail/push_options.hpp>
  17. #include <boost/config.hpp>
  18. #include <boost/system/system_error.hpp>
  19. #include <boost/asio/detail/pop_options.hpp>
  20. #if defined(BOOST_WINDOWS)
  21. #include <boost/asio/error.hpp>
  22. #include <boost/asio/detail/noncopyable.hpp>
  23. #include <boost/asio/detail/socket_types.hpp>
  24. #include <boost/asio/detail/scoped_lock.hpp>
  25. #include <boost/asio/detail/push_options.hpp>
  26. #include <boost/throw_exception.hpp>
  27. #include <boost/asio/detail/pop_options.hpp>
  28. namespace boost {
  29. namespace asio {
  30. namespace detail {
  31. class win_mutex
  32. : private noncopyable
  33. {
  34. public:
  35. typedef boost::asio::detail::scoped_lock<win_mutex> scoped_lock;
  36. // Constructor.
  37. win_mutex()
  38. {
  39. int error = do_init();
  40. if (error != 0)
  41. {
  42. boost::system::system_error e(
  43. boost::system::error_code(error,
  44. boost::asio::error::get_system_category()),
  45. "mutex");
  46. boost::throw_exception(e);
  47. }
  48. }
  49. // Destructor.
  50. ~win_mutex()
  51. {
  52. ::DeleteCriticalSection(&crit_section_);
  53. }
  54. // Lock the mutex.
  55. void lock()
  56. {
  57. int error = do_lock();
  58. if (error != 0)
  59. {
  60. boost::system::system_error e(
  61. boost::system::error_code(error,
  62. boost::asio::error::get_system_category()),
  63. "mutex");
  64. boost::throw_exception(e);
  65. }
  66. }
  67. // Unlock the mutex.
  68. void unlock()
  69. {
  70. ::LeaveCriticalSection(&crit_section_);
  71. }
  72. private:
  73. // Initialisation must be performed in a separate function to the constructor
  74. // since the compiler does not support the use of structured exceptions and
  75. // C++ exceptions in the same function.
  76. int do_init()
  77. {
  78. #if defined(__MINGW32__)
  79. // Not sure if MinGW supports structured exception handling, so for now
  80. // we'll just call the Windows API and hope.
  81. ::InitializeCriticalSection(&crit_section_);
  82. return 0;
  83. #else
  84. __try
  85. {
  86. ::InitializeCriticalSection(&crit_section_);
  87. }
  88. __except(GetExceptionCode() == STATUS_NO_MEMORY
  89. ? EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH)
  90. {
  91. return ERROR_OUTOFMEMORY;
  92. }
  93. return 0;
  94. #endif
  95. }
  96. // Locking must be performed in a separate function to lock() since the
  97. // compiler does not support the use of structured exceptions and C++
  98. // exceptions in the same function.
  99. int do_lock()
  100. {
  101. #if defined(__MINGW32__)
  102. // Not sure if MinGW supports structured exception handling, so for now
  103. // we'll just call the Windows API and hope.
  104. ::EnterCriticalSection(&crit_section_);
  105. return 0;
  106. #else
  107. __try
  108. {
  109. ::EnterCriticalSection(&crit_section_);
  110. }
  111. __except(GetExceptionCode() == STATUS_INVALID_HANDLE
  112. || GetExceptionCode() == STATUS_NO_MEMORY
  113. ? EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH)
  114. {
  115. if (GetExceptionCode() == STATUS_NO_MEMORY)
  116. return ERROR_OUTOFMEMORY;
  117. return ERROR_INVALID_HANDLE;
  118. }
  119. return 0;
  120. #endif
  121. }
  122. ::CRITICAL_SECTION crit_section_;
  123. };
  124. } // namespace detail
  125. } // namespace asio
  126. } // namespace boost
  127. #endif // defined(BOOST_WINDOWS)
  128. #include <boost/asio/detail/pop_options.hpp>
  129. #endif // BOOST_ASIO_DETAIL_WIN_MUTEX_HPP