2 // detail/impl/win_static_mutex.ipp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
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)
11 #ifndef BOOST_ASIO_DETAIL_IMPL_WIN_STATIC_MUTEX_IPP
12 #define BOOST_ASIO_DETAIL_IMPL_WIN_STATIC_MUTEX_IPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/config.hpp>
20 #if defined(BOOST_ASIO_WINDOWS)
23 #include <boost/asio/detail/throw_error.hpp>
24 #include <boost/asio/detail/win_static_mutex.hpp>
25 #include <boost/asio/error.hpp>
27 #include <boost/asio/detail/push_options.hpp>
33 void win_static_mutex::init()
35 int error = do_init();
36 boost::system::error_code ec(error,
37 boost::asio::error::get_system_category());
38 boost::asio::detail::throw_error(ec, "static_mutex");
41 int win_static_mutex::do_init()
43 using namespace std; // For sprintf.
44 wchar_t mutex_name[128];
45 #if defined(BOOST_ASIO_HAS_SECURE_RTL)
47 #else // defined(BOOST_ASIO_HAS_SECURE_RTL)
49 #endif // defined(BOOST_ASIO_HAS_SECURE_RTL)
50 mutex_name, 128, L"asio-58CCDC44-6264-4842-90C2-F3C545CB8AA7-%u-%p",
51 static_cast<unsigned int>(::GetCurrentProcessId()), this);
53 #if defined(BOOST_ASIO_WINDOWS_APP)
54 HANDLE mutex = ::CreateMutexExW(0, mutex_name, CREATE_MUTEX_INITIAL_OWNER, 0);
55 #else // defined(BOOST_ASIO_WINDOWS_APP)
56 HANDLE mutex = ::CreateMutexW(0, TRUE, mutex_name);
57 #endif // defined(BOOST_ASIO_WINDOWS_APP)
58 DWORD last_error = ::GetLastError();
60 return ::GetLastError();
62 if (last_error == ERROR_ALREADY_EXISTS)
64 #if defined(BOOST_ASIO_WINDOWS_APP)
65 ::WaitForSingleObjectEx(mutex, INFINITE, false);
66 #else // defined(BOOST_ASIO_WINDOWS_APP)
67 ::WaitForSingleObject(mutex, INFINITE);
68 #endif // defined(BOOST_ASIO_WINDOWS_APP)
73 ::ReleaseMutex(mutex);
78 #if defined(__MINGW32__)
79 // Not sure if MinGW supports structured exception handling, so for now
80 // we'll just call the Windows API and hope.
81 # if defined(UNDER_CE)
82 ::InitializeCriticalSection(&crit_section_);
84 if (!::InitializeCriticalSectionAndSpinCount(&crit_section_, 0x80000000))
86 last_error = ::GetLastError();
87 ::ReleaseMutex(mutex);
95 # if defined(UNDER_CE)
96 ::InitializeCriticalSection(&crit_section_);
97 # elif defined(BOOST_ASIO_WINDOWS_APP)
98 if (!::InitializeCriticalSectionEx(&crit_section_, 0, 0))
100 last_error = ::GetLastError();
101 ::ReleaseMutex(mutex);
102 ::CloseHandle(mutex);
106 if (!::InitializeCriticalSectionAndSpinCount(&crit_section_, 0x80000000))
108 last_error = ::GetLastError();
109 ::ReleaseMutex(mutex);
110 ::CloseHandle(mutex);
115 __except(GetExceptionCode() == STATUS_NO_MEMORY
116 ? EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH)
118 ::ReleaseMutex(mutex);
119 ::CloseHandle(mutex);
120 return ERROR_OUTOFMEMORY;
125 ::ReleaseMutex(mutex);
126 ::CloseHandle(mutex);
130 } // namespace detail
134 #include <boost/asio/detail/pop_options.hpp>
136 #endif // defined(BOOST_ASIO_WINDOWS)
138 #endif // BOOST_ASIO_DETAIL_IMPL_WIN_STATIC_MUTEX_IPP