win_static_mutex.ipp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. //
  2. // detail/impl/win_static_mutex.ipp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2013 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. HANDLE mutex = ::CreateMutexW(0, TRUE, mutex_name);
  44. DWORD last_error = ::GetLastError();
  45. if (mutex == 0)
  46. return ::GetLastError();
  47. if (last_error == ERROR_ALREADY_EXISTS)
  48. ::WaitForSingleObject(mutex, INFINITE);
  49. if (initialised_)
  50. {
  51. ::ReleaseMutex(mutex);
  52. ::CloseHandle(mutex);
  53. return 0;
  54. }
  55. #if defined(__MINGW32__)
  56. // Not sure if MinGW supports structured exception handling, so for now
  57. // we'll just call the Windows API and hope.
  58. # if defined(UNDER_CE)
  59. ::InitializeCriticalSection(&crit_section_);
  60. # else
  61. if (!::InitializeCriticalSectionAndSpinCount(&crit_section_, 0x80000000))
  62. {
  63. last_error = ::GetLastError();
  64. ::ReleaseMutex(mutex);
  65. ::CloseHandle(mutex);
  66. return last_error;
  67. }
  68. # endif
  69. #else
  70. __try
  71. {
  72. # if defined(UNDER_CE)
  73. ::InitializeCriticalSection(&crit_section_);
  74. # else
  75. if (!::InitializeCriticalSectionAndSpinCount(&crit_section_, 0x80000000))
  76. {
  77. last_error = ::GetLastError();
  78. ::ReleaseMutex(mutex);
  79. ::CloseHandle(mutex);
  80. return last_error;
  81. }
  82. # endif
  83. }
  84. __except(GetExceptionCode() == STATUS_NO_MEMORY
  85. ? EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH)
  86. {
  87. ::ReleaseMutex(mutex);
  88. ::CloseHandle(mutex);
  89. return ERROR_OUTOFMEMORY;
  90. }
  91. #endif
  92. initialised_ = true;
  93. ::ReleaseMutex(mutex);
  94. ::CloseHandle(mutex);
  95. return 0;
  96. }
  97. } // namespace detail
  98. } // namespace asio
  99. } // namespace boost
  100. #include <boost/asio/detail/pop_options.hpp>
  101. #endif // defined(BOOST_ASIO_WINDOWS)
  102. #endif // BOOST_ASIO_DETAIL_IMPL_WIN_STATIC_MUTEX_IPP