5 // Copyright (c) 2003-2008 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_WIN_MUTEX_HPP
12 #define BOOST_ASIO_DETAIL_WIN_MUTEX_HPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/push_options.hpp>
20 #include <boost/asio/detail/push_options.hpp>
21 #include <boost/config.hpp>
22 #include <boost/system/system_error.hpp>
23 #include <boost/asio/detail/pop_options.hpp>
25 #if defined(BOOST_WINDOWS)
27 #include <boost/asio/error.hpp>
28 #include <boost/asio/detail/noncopyable.hpp>
29 #include <boost/asio/detail/socket_types.hpp>
30 #include <boost/asio/detail/scoped_lock.hpp>
32 #include <boost/asio/detail/push_options.hpp>
33 #include <boost/throw_exception.hpp>
34 #include <boost/asio/detail/pop_options.hpp>
44 typedef boost::asio::detail::scoped_lock
<win_mutex
> scoped_lock
;
49 int error
= do_init();
52 boost::system::system_error
e(
53 boost::system::error_code(error
,
54 boost::asio::error::get_system_category()),
56 boost::throw_exception(e
);
63 ::DeleteCriticalSection(&crit_section_
);
69 int error
= do_lock();
72 boost::system::system_error
e(
73 boost::system::error_code(error
,
74 boost::asio::error::get_system_category()),
76 boost::throw_exception(e
);
83 ::LeaveCriticalSection(&crit_section_
);
87 // Initialisation must be performed in a separate function to the constructor
88 // since the compiler does not support the use of structured exceptions and
89 // C++ exceptions in the same function.
92 #if defined(__MINGW32__)
93 // Not sure if MinGW supports structured exception handling, so for now
94 // we'll just call the Windows API and hope.
95 ::InitializeCriticalSection(&crit_section_
);
100 ::InitializeCriticalSection(&crit_section_
);
102 __except(GetExceptionCode() == STATUS_NO_MEMORY
103 ? EXCEPTION_EXECUTE_HANDLER
: EXCEPTION_CONTINUE_SEARCH
)
105 return ERROR_OUTOFMEMORY
;
112 // Locking must be performed in a separate function to lock() since the
113 // compiler does not support the use of structured exceptions and C++
114 // exceptions in the same function.
117 #if defined(__MINGW32__)
118 // Not sure if MinGW supports structured exception handling, so for now
119 // we'll just call the Windows API and hope.
120 ::EnterCriticalSection(&crit_section_
);
125 ::EnterCriticalSection(&crit_section_
);
127 __except(GetExceptionCode() == STATUS_INVALID_HANDLE
128 || GetExceptionCode() == STATUS_NO_MEMORY
129 ? EXCEPTION_EXECUTE_HANDLER
: EXCEPTION_CONTINUE_SEARCH
)
131 if (GetExceptionCode() == STATUS_NO_MEMORY
)
132 return ERROR_OUTOFMEMORY
;
133 return ERROR_INVALID_HANDLE
;
140 ::CRITICAL_SECTION crit_section_
;
143 } // namespace detail
147 #endif // defined(BOOST_WINDOWS)
149 #include <boost/asio/detail/pop_options.hpp>
151 #endif // BOOST_ASIO_DETAIL_WIN_MUTEX_HPP