win_static_mutex.ipp 2.8 KB

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