win_mutex.ipp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. //
  2. // detail/impl/win_mutex.ipp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2018 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_IMPL_WIN_MUTEX_IPP
  11. #define BOOST_ASIO_DETAIL_IMPL_WIN_MUTEX_IPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <boost/asio/detail/config.hpp>
  16. #if defined(BOOST_ASIO_WINDOWS)
  17. #include <boost/asio/detail/throw_error.hpp>
  18. #include <boost/asio/detail/win_mutex.hpp>
  19. #include <boost/asio/error.hpp>
  20. #include <boost/asio/detail/push_options.hpp>
  21. namespace boost {
  22. namespace asio {
  23. namespace detail {
  24. win_mutex::win_mutex()
  25. {
  26. int error = do_init();
  27. boost::system::error_code ec(error,
  28. boost::asio::error::get_system_category());
  29. boost::asio::detail::throw_error(ec, "mutex");
  30. }
  31. int win_mutex::do_init()
  32. {
  33. #if defined(__MINGW32__)
  34. // Not sure if MinGW supports structured exception handling, so for now
  35. // we'll just call the Windows API and hope.
  36. # if defined(UNDER_CE)
  37. ::InitializeCriticalSection(&crit_section_);
  38. # elif defined(BOOST_ASIO_WINDOWS_APP)
  39. if (!::InitializeCriticalSectionEx(&crit_section_, 0, 0))
  40. return ::GetLastError();
  41. # else
  42. if (!::InitializeCriticalSectionAndSpinCount(&crit_section_, 0x80000000))
  43. return ::GetLastError();
  44. # endif
  45. return 0;
  46. #else
  47. __try
  48. {
  49. # if defined(UNDER_CE)
  50. ::InitializeCriticalSection(&crit_section_);
  51. # elif defined(BOOST_ASIO_WINDOWS_APP)
  52. if (!::InitializeCriticalSectionEx(&crit_section_, 0, 0))
  53. return ::GetLastError();
  54. # else
  55. if (!::InitializeCriticalSectionAndSpinCount(&crit_section_, 0x80000000))
  56. return ::GetLastError();
  57. # endif
  58. }
  59. __except(GetExceptionCode() == STATUS_NO_MEMORY
  60. ? EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH)
  61. {
  62. return ERROR_OUTOFMEMORY;
  63. }
  64. return 0;
  65. #endif
  66. }
  67. } // namespace detail
  68. } // namespace asio
  69. } // namespace boost
  70. #include <boost/asio/detail/pop_options.hpp>
  71. #endif // defined(BOOST_ASIO_WINDOWS)
  72. #endif // BOOST_ASIO_DETAIL_IMPL_WIN_MUTEX_IPP