00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #ifndef ASIO_DETAIL_WIN_MUTEX_HPP
00012 #define ASIO_DETAIL_WIN_MUTEX_HPP
00013
00014 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
00015 # pragma once
00016 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
00017
00018 #include "asio/detail/push_options.hpp"
00019
00020 #include "asio/detail/push_options.hpp"
00021 #include <boost/config.hpp>
00022 #include "asio/detail/pop_options.hpp"
00023
00024 #if defined(BOOST_WINDOWS)
00025
00026 #include "asio/system_error.hpp"
00027 #include "asio/detail/noncopyable.hpp"
00028 #include "asio/detail/socket_types.hpp"
00029 #include "asio/detail/scoped_lock.hpp"
00030
00031 #include "asio/detail/push_options.hpp"
00032 #include <boost/throw_exception.hpp>
00033 #include "asio/detail/pop_options.hpp"
00034
00035 namespace asio {
00036 namespace detail {
00037
00038 class win_mutex
00039 : private noncopyable
00040 {
00041 public:
00042 typedef asio::detail::scoped_lock<win_mutex> scoped_lock;
00043
00044
00045 win_mutex()
00046 {
00047 int error = do_init();
00048 if (error != 0)
00049 {
00050 asio::system_error e(
00051 asio::error_code(error, asio::native_ecat),
00052 "mutex");
00053 boost::throw_exception(e);
00054 }
00055 }
00056
00057
00058 ~win_mutex()
00059 {
00060 ::DeleteCriticalSection(&crit_section_);
00061 }
00062
00063
00064 void lock()
00065 {
00066 int error = do_lock();
00067 if (error != 0)
00068 {
00069 asio::system_error e(
00070 asio::error_code(error, asio::native_ecat),
00071 "mutex");
00072 boost::throw_exception(e);
00073 }
00074 }
00075
00076
00077 void unlock()
00078 {
00079 ::LeaveCriticalSection(&crit_section_);
00080 }
00081
00082 private:
00083
00084
00085
00086 int do_init()
00087 {
00088 #if defined(__MINGW32__)
00089
00090
00091 ::InitializeCriticalSection(&crit_section_);
00092 return 0;
00093 #else
00094 __try
00095 {
00096 ::InitializeCriticalSection(&crit_section_);
00097 }
00098 __except(GetExceptionCode() == STATUS_NO_MEMORY
00099 ? EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH)
00100 {
00101 return ERROR_OUTOFMEMORY;
00102 }
00103
00104 return 0;
00105 #endif
00106 }
00107
00108
00109
00110
00111 int do_lock()
00112 {
00113 #if defined(__MINGW32__)
00114
00115
00116 ::EnterCriticalSection(&crit_section_);
00117 return 0;
00118 #else
00119 __try
00120 {
00121 ::EnterCriticalSection(&crit_section_);
00122 }
00123 __except(GetExceptionCode() == STATUS_INVALID_HANDLE
00124 || GetExceptionCode() == STATUS_NO_MEMORY
00125 ? EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH)
00126 {
00127 if (GetExceptionCode() == STATUS_NO_MEMORY)
00128 return ERROR_OUTOFMEMORY;
00129 return ERROR_INVALID_HANDLE;
00130 }
00131
00132 return 0;
00133 #endif
00134 }
00135
00136 ::CRITICAL_SECTION crit_section_;
00137 };
00138
00139 }
00140 }
00141
00142 #endif // defined(BOOST_WINDOWS)
00143
00144 #include "asio/detail/pop_options.hpp"
00145
00146 #endif // ASIO_DETAIL_WIN_MUTEX_HPP