win_static_mutex.ipp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. //
  2. // detail/impl/win_static_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_STATIC_MUTEX_IPP
  11. #define BOOST_ASIO_DETAIL_IMPL_WIN_STATIC_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 <cstdio>
  18. #include <boost/asio/detail/throw_error.hpp>
  19. #include <boost/asio/detail/win_static_mutex.hpp>
  20. #include <boost/asio/error.hpp>
  21. #include <boost/asio/detail/push_options.hpp>
  22. namespace boost {
  23. namespace asio {
  24. namespace detail {
  25. void win_static_mutex::init()
  26. {
  27. int error = do_init();
  28. boost::system::error_code ec(error,
  29. boost::asio::error::get_system_category());
  30. boost::asio::detail::throw_error(ec, "static_mutex");
  31. }
  32. int win_static_mutex::do_init()
  33. {
  34. using namespace std; // For sprintf.
  35. wchar_t mutex_name[128];
  36. #if defined(BOOST_ASIO_HAS_SECURE_RTL)
  37. swprintf_s(
  38. #else // defined(BOOST_ASIO_HAS_SECURE_RTL)
  39. _snwprintf(
  40. #endif // defined(BOOST_ASIO_HAS_SECURE_RTL)
  41. mutex_name, 128, L"asio-58CCDC44-6264-4842-90C2-F3C545CB8AA7-%u-%p",
  42. static_cast<unsigned int>(::GetCurrentProcessId()), this);
  43. #if defined(BOOST_ASIO_WINDOWS_APP)
  44. HANDLE mutex = ::CreateMutexExW(0, mutex_name, CREATE_MUTEX_INITIAL_OWNER, 0);
  45. #else // defined(BOOST_ASIO_WINDOWS_APP)
  46. HANDLE mutex = ::CreateMutexW(0, TRUE, mutex_name);
  47. #endif // defined(BOOST_ASIO_WINDOWS_APP)
  48. DWORD last_error = ::GetLastError();
  49. if (mutex == 0)
  50. return ::GetLastError();
  51. if (last_error == ERROR_ALREADY_EXISTS)
  52. {
  53. #if defined(BOOST_ASIO_WINDOWS_APP)
  54. ::WaitForSingleObjectEx(mutex, INFINITE, false);
  55. #else // defined(BOOST_ASIO_WINDOWS_APP)
  56. ::WaitForSingleObject(mutex, INFINITE);
  57. #endif // defined(BOOST_ASIO_WINDOWS_APP)
  58. }
  59. if (initialised_)
  60. {
  61. ::ReleaseMutex(mutex);
  62. ::CloseHandle(mutex);
  63. return 0;
  64. }
  65. #if defined(__MINGW32__)
  66. // Not sure if MinGW supports structured exception handling, so for now
  67. // we'll just call the Windows API and hope.
  68. # if defined(UNDER_CE)
  69. ::InitializeCriticalSection(&crit_section_);
  70. # else
  71. if (!::InitializeCriticalSectionAndSpinCount(&crit_section_, 0x80000000))
  72. {
  73. last_error = ::GetLastError();
  74. ::ReleaseMutex(mutex);
  75. ::CloseHandle(mutex);
  76. return last_error;
  77. }
  78. # endif
  79. #else
  80. __try
  81. {
  82. # if defined(UNDER_CE)
  83. ::InitializeCriticalSection(&crit_section_);
  84. # elif defined(BOOST_ASIO_WINDOWS_APP)
  85. if (!::InitializeCriticalSectionEx(&crit_section_, 0, 0))
  86. {
  87. last_error = ::GetLastError();
  88. ::ReleaseMutex(mutex);
  89. ::CloseHandle(mutex);
  90. return last_error;
  91. }
  92. # else
  93. if (!::InitializeCriticalSectionAndSpinCount(&crit_section_, 0x80000000))
  94. {
  95. last_error = ::GetLastError();
  96. ::ReleaseMutex(mutex);
  97. ::CloseHandle(mutex);
  98. return last_error;
  99. }
  100. # endif
  101. }
  102. __except(GetExceptionCode() == STATUS_NO_MEMORY
  103. ? EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH)
  104. {
  105. ::ReleaseMutex(mutex);
  106. ::CloseHandle(mutex);
  107. return ERROR_OUTOFMEMORY;
  108. }
  109. #endif
  110. initialised_ = true;
  111. ::ReleaseMutex(mutex);
  112. ::CloseHandle(mutex);
  113. return 0;
  114. }
  115. } // namespace detail
  116. } // namespace asio
  117. } // namespace boost
  118. #include <boost/asio/detail/pop_options.hpp>
  119. #endif // defined(BOOST_ASIO_WINDOWS)
  120. #endif // BOOST_ASIO_DETAIL_IMPL_WIN_STATIC_MUTEX_IPP