File indexing completed on 2025-01-18 09:28:36
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #ifndef BOOST_ASIO_DETAIL_IMPL_WIN_STATIC_MUTEX_IPP
0012 #define BOOST_ASIO_DETAIL_IMPL_WIN_STATIC_MUTEX_IPP
0013
0014 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
0015 # pragma once
0016 #endif
0017
0018 #include <boost/asio/detail/config.hpp>
0019
0020 #if defined(BOOST_ASIO_WINDOWS)
0021
0022 #include <cstdio>
0023 #include <boost/asio/detail/throw_error.hpp>
0024 #include <boost/asio/detail/win_static_mutex.hpp>
0025 #include <boost/asio/error.hpp>
0026
0027 #include <boost/asio/detail/push_options.hpp>
0028
0029 namespace boost {
0030 namespace asio {
0031 namespace detail {
0032
0033 void win_static_mutex::init()
0034 {
0035 int error = do_init();
0036 boost::system::error_code ec(error,
0037 boost::asio::error::get_system_category());
0038 boost::asio::detail::throw_error(ec, "static_mutex");
0039 }
0040
0041 int win_static_mutex::do_init()
0042 {
0043 using namespace std;
0044 wchar_t mutex_name[128];
0045 #if defined(BOOST_ASIO_HAS_SECURE_RTL)
0046 swprintf_s(
0047 #else
0048 _snwprintf(
0049 #endif
0050 mutex_name, 128, L"asio-58CCDC44-6264-4842-90C2-F3C545CB8AA7-%u-%p",
0051 static_cast<unsigned int>(::GetCurrentProcessId()), this);
0052
0053 #if defined(BOOST_ASIO_WINDOWS_APP)
0054 HANDLE mutex = ::CreateMutexExW(0, mutex_name, CREATE_MUTEX_INITIAL_OWNER, 0);
0055 #else
0056 HANDLE mutex = ::CreateMutexW(0, TRUE, mutex_name);
0057 #endif
0058 DWORD last_error = ::GetLastError();
0059 if (mutex == 0)
0060 return ::GetLastError();
0061
0062 if (last_error == ERROR_ALREADY_EXISTS)
0063 {
0064 #if defined(BOOST_ASIO_WINDOWS_APP)
0065 ::WaitForSingleObjectEx(mutex, INFINITE, false);
0066 #else
0067 ::WaitForSingleObject(mutex, INFINITE);
0068 #endif
0069 }
0070
0071 if (initialised_)
0072 {
0073 ::ReleaseMutex(mutex);
0074 ::CloseHandle(mutex);
0075 return 0;
0076 }
0077
0078 #if defined(__MINGW32__)
0079
0080
0081 # if defined(UNDER_CE)
0082 ::InitializeCriticalSection(&crit_section_);
0083 # else
0084 if (!::InitializeCriticalSectionAndSpinCount(&crit_section_, 0x80000000))
0085 {
0086 last_error = ::GetLastError();
0087 ::ReleaseMutex(mutex);
0088 ::CloseHandle(mutex);
0089 return last_error;
0090 }
0091 # endif
0092 #else
0093 __try
0094 {
0095 # if defined(UNDER_CE)
0096 ::InitializeCriticalSection(&crit_section_);
0097 # elif defined(BOOST_ASIO_WINDOWS_APP)
0098 if (!::InitializeCriticalSectionEx(&crit_section_, 0, 0))
0099 {
0100 last_error = ::GetLastError();
0101 ::ReleaseMutex(mutex);
0102 ::CloseHandle(mutex);
0103 return last_error;
0104 }
0105 # else
0106 if (!::InitializeCriticalSectionAndSpinCount(&crit_section_, 0x80000000))
0107 {
0108 last_error = ::GetLastError();
0109 ::ReleaseMutex(mutex);
0110 ::CloseHandle(mutex);
0111 return last_error;
0112 }
0113 # endif
0114 }
0115 __except(GetExceptionCode() == STATUS_NO_MEMORY
0116 ? EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH)
0117 {
0118 ::ReleaseMutex(mutex);
0119 ::CloseHandle(mutex);
0120 return ERROR_OUTOFMEMORY;
0121 }
0122 #endif
0123
0124 initialised_ = true;
0125 ::ReleaseMutex(mutex);
0126 ::CloseHandle(mutex);
0127 return 0;
0128 }
0129
0130 }
0131 }
0132 }
0133
0134 #include <boost/asio/detail/pop_options.hpp>
0135
0136 #endif
0137
0138 #endif