2 // detail/impl/win_mutex.ipp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2017 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_MUTEX_IPP
12 #define BOOST_ASIO_DETAIL_IMPL_WIN_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)
22 #include <boost/asio/detail/throw_error.hpp>
23 #include <boost/asio/detail/win_mutex.hpp>
24 #include <boost/asio/error.hpp>
26 #include <boost/asio/detail/push_options.hpp>
32 win_mutex::win_mutex()
34 int error = do_init();
35 boost::system::error_code ec(error,
36 boost::asio::error::get_system_category());
37 boost::asio::detail::throw_error(ec, "mutex");
40 int win_mutex::do_init()
42 #if defined(__MINGW32__)
43 // Not sure if MinGW supports structured exception handling, so for now
44 // we'll just call the Windows API and hope.
45 # if defined(UNDER_CE)
46 ::InitializeCriticalSection(&crit_section_);
47 # elif defined(BOOST_ASIO_WINDOWS_APP)
48 if (!::InitializeCriticalSectionEx(&crit_section_, 0, 0))
49 return ::GetLastError();
51 if (!::InitializeCriticalSectionAndSpinCount(&crit_section_, 0x80000000))
52 return ::GetLastError();
58 # if defined(UNDER_CE)
59 ::InitializeCriticalSection(&crit_section_);
60 # elif defined(BOOST_ASIO_WINDOWS_APP)
61 if (!::InitializeCriticalSectionEx(&crit_section_, 0, 0))
62 return ::GetLastError();
64 if (!::InitializeCriticalSectionAndSpinCount(&crit_section_, 0x80000000))
65 return ::GetLastError();
68 __except(GetExceptionCode() == STATUS_NO_MEMORY
69 ? EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH)
71 return ERROR_OUTOFMEMORY;
82 #include <boost/asio/detail/pop_options.hpp>
84 #endif // defined(BOOST_ASIO_WINDOWS)
86 #endif // BOOST_ASIO_DETAIL_IMPL_WIN_MUTEX_IPP