win_static_mutex.ipp 3.4 KB

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