fix doc example typo
[boost.git] / boost / asio / detail / win_mutex.hpp
blobf5470c4f26d1522023301c08af30ca6c698db51a
1 //
2 // win_mutex.hpp
3 // ~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2008 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 //
11 #ifndef BOOST_ASIO_DETAIL_WIN_MUTEX_HPP
12 #define BOOST_ASIO_DETAIL_WIN_MUTEX_HPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
15 # pragma once
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>
36 namespace boost {
37 namespace asio {
38 namespace detail {
40 class win_mutex
41 : private noncopyable
43 public:
44 typedef boost::asio::detail::scoped_lock<win_mutex> scoped_lock;
46 // Constructor.
47 win_mutex()
49 int error = do_init();
50 if (error != 0)
52 boost::system::system_error e(
53 boost::system::error_code(error,
54 boost::asio::error::get_system_category()),
55 "mutex");
56 boost::throw_exception(e);
60 // Destructor.
61 ~win_mutex()
63 ::DeleteCriticalSection(&crit_section_);
66 // Lock the mutex.
67 void lock()
69 int error = do_lock();
70 if (error != 0)
72 boost::system::system_error e(
73 boost::system::error_code(error,
74 boost::asio::error::get_system_category()),
75 "mutex");
76 boost::throw_exception(e);
80 // Unlock the mutex.
81 void unlock()
83 ::LeaveCriticalSection(&crit_section_);
86 private:
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.
90 int do_init()
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_);
96 return 0;
97 #else
98 __try
100 ::InitializeCriticalSection(&crit_section_);
102 __except(GetExceptionCode() == STATUS_NO_MEMORY
103 ? EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH)
105 return ERROR_OUTOFMEMORY;
108 return 0;
109 #endif
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.
115 int do_lock()
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_);
121 return 0;
122 #else
123 __try
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;
136 return 0;
137 #endif
140 ::CRITICAL_SECTION crit_section_;
143 } // namespace detail
144 } // namespace asio
145 } // namespace boost
147 #endif // defined(BOOST_WINDOWS)
149 #include <boost/asio/detail/pop_options.hpp>
151 #endif // BOOST_ASIO_DETAIL_WIN_MUTEX_HPP