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