1 // 2 // boost/signals2/detail/lwm_win32_cs.hpp 3 // 4 // Copyright (c) 2002, 2003 Peter Dimov 5 // Copyright (c) 2008 Frank Mori Hess 6 // Copyright (c) Microsoft Corporation 2014 7 // 8 // Distributed under the Boost Software License, Version 1.0. (See 9 // accompanying file LICENSE_1_0.txt or copy at 10 // http://www.boost.org/LICENSE_1_0.txt) 11 // 12 13 #ifndef BOOST_SIGNALS2_LWM_WIN32_CS_HPP 14 #define BOOST_SIGNALS2_LWM_WIN32_CS_HPP 15 16 // MS compatible compilers support #pragma once 17 18 #if defined(_MSC_VER) && (_MSC_VER >= 1020) 19 # pragma once 20 #endif 21 22 #include <boost/predef.h> 23 #include <boost/assert.hpp> 24 25 #ifdef BOOST_USE_WINDOWS_H 26 27 #include <windows.h> 28 29 #else 30 31 struct _RTL_CRITICAL_SECTION; 32 33 #endif 34 35 namespace boost 36 { 37 38 namespace signals2 39 { 40 41 namespace detail 42 { 43 44 #ifndef BOOST_USE_WINDOWS_H 45 46 struct critical_section 47 { 48 struct critical_section_debug * DebugInfo; 49 long LockCount; 50 long RecursionCount; 51 void * OwningThread; 52 void * LockSemaphore; 53 #if defined(_WIN64) 54 unsigned __int64 SpinCount; 55 #else 56 unsigned long SpinCount; 57 #endif 58 }; 59 60 #if BOOST_PLAT_WINDOWS_RUNTIME 61 extern "C" __declspec(dllimport) void __stdcall InitializeCriticalSectionEx(::_RTL_CRITICAL_SECTION *, unsigned long, unsigned long); 62 #else 63 extern "C" __declspec(dllimport) void __stdcall InitializeCriticalSection(::_RTL_CRITICAL_SECTION *); 64 #endif 65 extern "C" __declspec(dllimport) void __stdcall EnterCriticalSection(::_RTL_CRITICAL_SECTION *); 66 extern "C" __declspec(dllimport) int __stdcall TryEnterCriticalSection(::_RTL_CRITICAL_SECTION *); 67 extern "C" __declspec(dllimport) void __stdcall LeaveCriticalSection(::_RTL_CRITICAL_SECTION *); 68 extern "C" __declspec(dllimport) void __stdcall DeleteCriticalSection(::_RTL_CRITICAL_SECTION *); 69 70 typedef ::_RTL_CRITICAL_SECTION rtl_critical_section; 71 72 #else // #ifndef BOOST_USE_WINDOWS_H 73 74 typedef ::CRITICAL_SECTION critical_section; 75 76 #if BOOST_PLAT_WINDOWS_RUNTIME 77 using ::InitializeCriticalSectionEx; 78 #else 79 using ::InitializeCriticalSection; 80 #endif 81 using ::EnterCriticalSection; 82 using ::TryEnterCriticalSection; 83 using ::LeaveCriticalSection; 84 using ::DeleteCriticalSection; 85 86 typedef ::CRITICAL_SECTION rtl_critical_section; 87 88 #endif // #ifndef BOOST_USE_WINDOWS_H 89 90 } // namespace detail 91 92 class mutex 93 { 94 private: 95 96 boost::signals2::detail::critical_section cs_; 97 98 mutex(mutex const &); 99 mutex & operator=(mutex const &); 100 101 public: 102 mutex()103 mutex() 104 { 105 #if BOOST_PLAT_WINDOWS_RUNTIME 106 boost::signals2::detail::InitializeCriticalSectionEx(reinterpret_cast< boost::signals2::detail::rtl_critical_section* >(&cs_), 4000, 0); 107 #else 108 boost::signals2::detail::InitializeCriticalSection(reinterpret_cast< boost::signals2::detail::rtl_critical_section* >(&cs_)); 109 #endif 110 } 111 ~mutex()112 ~mutex() 113 { 114 boost::signals2::detail::DeleteCriticalSection(reinterpret_cast< boost::signals2::detail::rtl_critical_section* >(&cs_)); 115 } 116 lock()117 void lock() 118 { 119 boost::signals2::detail::EnterCriticalSection(reinterpret_cast< boost::signals2::detail::rtl_critical_section* >(&cs_)); 120 } 121 // TryEnterCriticalSection only exists on Windows NT 4.0 and later 122 #if (defined(_WIN32_WINNT) && (_WIN32_WINNT >= 0x0400)) try_lock()123 bool try_lock() 124 { 125 return boost::signals2::detail::TryEnterCriticalSection(reinterpret_cast< boost::signals2::detail::rtl_critical_section* >(&cs_)) != 0; 126 } 127 #else try_lock()128 bool try_lock() 129 { 130 BOOST_ASSERT(false); 131 return false; 132 } 133 #endif unlock()134 void unlock() 135 { 136 boost::signals2::detail::LeaveCriticalSection(reinterpret_cast< boost::signals2::detail::rtl_critical_section* >(&cs_)); 137 } 138 }; 139 140 } // namespace signals2 141 142 } // namespace boost 143 144 #endif // #ifndef BOOST_SIGNALS2_LWM_WIN32_CS_HPP 145