2 // detail/impl/win_mutex.ipp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2016 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 ::InitializeCriticalSectionEx(&crit_section_, 0x80000000, 0);
50 if (!::InitializeCriticalSectionAndSpinCount(&crit_section_, 0x80000000))
51 return ::GetLastError();
57 # if defined(UNDER_CE)
58 ::InitializeCriticalSection(&crit_section_);
59 # elif defined(BOOST_ASIO_WINDOWS_APP)
60 if (!::InitializeCriticalSectionEx(&crit_section_, 0, 0))
61 return ::GetLastError();
63 if (!::InitializeCriticalSectionAndSpinCount(&crit_section_, 0x80000000))
64 return ::GetLastError();
67 __except(GetExceptionCode() == STATUS_NO_MEMORY
68 ? EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH)
70 return ERROR_OUTOFMEMORY;
81 #include <boost/asio/detail/pop_options.hpp>
83 #endif // defined(BOOST_ASIO_WINDOWS)
85 #endif // BOOST_ASIO_DETAIL_IMPL_WIN_MUTEX_IPP