Imported Upstream version 1.64.0
[platform/upstream/boost.git] / boost / asio / detail / impl / win_mutex.ipp
1 //
2 // detail/impl/win_mutex.ipp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2017 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
11 #ifndef BOOST_ASIO_DETAIL_IMPL_WIN_MUTEX_IPP
12 #define BOOST_ASIO_DETAIL_IMPL_WIN_MUTEX_IPP
13
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
15 # pragma once
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
17
18 #include <boost/asio/detail/config.hpp>
19
20 #if defined(BOOST_ASIO_WINDOWS)
21
22 #include <boost/asio/detail/throw_error.hpp>
23 #include <boost/asio/detail/win_mutex.hpp>
24 #include <boost/asio/error.hpp>
25
26 #include <boost/asio/detail/push_options.hpp>
27
28 namespace boost {
29 namespace asio {
30 namespace detail {
31
32 win_mutex::win_mutex()
33 {
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");
38 }
39
40 int win_mutex::do_init()
41 {
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();
50 # else
51   if (!::InitializeCriticalSectionAndSpinCount(&crit_section_, 0x80000000))
52     return ::GetLastError();
53 # endif
54   return 0;
55 #else
56   __try
57   {
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();
63 # else
64     if (!::InitializeCriticalSectionAndSpinCount(&crit_section_, 0x80000000))
65       return ::GetLastError();
66 # endif
67   }
68   __except(GetExceptionCode() == STATUS_NO_MEMORY
69       ? EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH)
70   {
71     return ERROR_OUTOFMEMORY;
72   }
73
74   return 0;
75 #endif
76 }
77
78 } // namespace detail
79 } // namespace asio
80 } // namespace boost
81
82 #include <boost/asio/detail/pop_options.hpp>
83
84 #endif // defined(BOOST_ASIO_WINDOWS)
85
86 #endif // BOOST_ASIO_DETAIL_IMPL_WIN_MUTEX_IPP