• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2010 Vicente J. Botet Escriba
3  * Copyright 2015 Andrey Semashev
4  *
5  * Distributed under the Boost Software License, Version 1.0.
6  * See http://www.boost.org/LICENSE_1_0.txt
7  */
8 
9 #ifndef BOOST_WINAPI_CRITICAL_SECTION_HPP_INCLUDED_
10 #define BOOST_WINAPI_CRITICAL_SECTION_HPP_INCLUDED_
11 
12 #include <boost/winapi/basic_types.hpp>
13 #include <boost/winapi/detail/cast_ptr.hpp>
14 #include <boost/winapi/detail/header.hpp>
15 
16 #ifdef BOOST_HAS_PRAGMA_ONCE
17 #pragma once
18 #endif
19 
20 #if !defined( BOOST_USE_WINDOWS_H )
21 
22 extern "C" {
23 #if !defined( BOOST_WINAPI_IS_MINGW )
24 
25 // Windows CE uses a different name for the structure
26 #if defined (_WIN32_WCE)
27 struct CRITICAL_SECTION;
28 namespace boost {
29 namespace winapi {
30 namespace detail {
31     typedef CRITICAL_SECTION winsdk_critical_section;
32 }
33 }
34 }
35 #else
36 struct _RTL_CRITICAL_SECTION;
37 namespace boost {
38 namespace winapi {
39 namespace detail {
40     typedef _RTL_CRITICAL_SECTION winsdk_critical_section;
41 }
42 }
43 }
44 #endif
45 
46 #else
47 // MinGW uses a different name for the structure
48 struct _CRITICAL_SECTION;
49 
50 namespace boost {
51 namespace winapi {
52 namespace detail {
53     typedef _CRITICAL_SECTION winapi_critical_section;
54 }
55 }
56 }
57 #endif
58 
59 #if !defined( BOOST_WINAPI_IS_MINGW )
60 
61 #if BOOST_WINAPI_PARTITION_APP_SYSTEM
62 BOOST_WINAPI_IMPORT_EXCEPT_WM boost::winapi::VOID_ BOOST_WINAPI_WINAPI_CC
63 InitializeCriticalSection(boost::winapi::detail::winsdk_critical_section* lpCriticalSection);
64 #endif
65 
66 BOOST_WINAPI_IMPORT_EXCEPT_WM boost::winapi::VOID_ BOOST_WINAPI_WINAPI_CC
67 EnterCriticalSection(boost::winapi::detail::winsdk_critical_section* lpCriticalSection);
68 
69 BOOST_WINAPI_IMPORT_EXCEPT_WM boost::winapi::VOID_ BOOST_WINAPI_WINAPI_CC
70 LeaveCriticalSection(boost::winapi::detail::winsdk_critical_section* lpCriticalSection);
71 
72 #if BOOST_USE_WINAPI_VERSION >= 0x0403
73 #if BOOST_WINAPI_PARTITION_APP_SYSTEM
74 BOOST_WINAPI_IMPORT boost::winapi::BOOL_ BOOST_WINAPI_WINAPI_CC
75 InitializeCriticalSectionAndSpinCount(
76     boost::winapi::detail::winsdk_critical_section* lpCriticalSection,
77     boost::winapi::DWORD_ dwSpinCount);
78 
79 BOOST_WINAPI_IMPORT boost::winapi::DWORD_ BOOST_WINAPI_WINAPI_CC
80 SetCriticalSectionSpinCount(
81     boost::winapi::detail::winsdk_critical_section* lpCriticalSection,
82     boost::winapi::DWORD_ dwSpinCount);
83 #endif
84 
85 #if BOOST_USE_WINAPI_VERSION >= BOOST_WINAPI_VERSION_WIN6
86 BOOST_WINAPI_IMPORT boost::winapi::BOOL_ BOOST_WINAPI_WINAPI_CC
87 InitializeCriticalSectionEx(
88     boost::winapi::detail::winsdk_critical_section* lpCriticalSection,
89     boost::winapi::DWORD_ dwSpinCount,
90     boost::winapi::DWORD_ Flags);
91 #endif
92 #endif
93 
94 #if BOOST_USE_WINAPI_VERSION >= BOOST_WINAPI_VERSION_NT4
95 BOOST_WINAPI_IMPORT_EXCEPT_WM boost::winapi::BOOL_ BOOST_WINAPI_WINAPI_CC
96 TryEnterCriticalSection(boost::winapi::detail::winsdk_critical_section* lpCriticalSection);
97 #endif
98 
99 BOOST_WINAPI_IMPORT_EXCEPT_WM boost::winapi::VOID_ BOOST_WINAPI_WINAPI_CC
100 DeleteCriticalSection(boost::winapi::detail::winsdk_critical_section* lpCriticalSection);
101 
102 #else // defined( BOOST_WINAPI_IS_MINGW )
103 
104 BOOST_WINAPI_IMPORT_EXCEPT_WM boost::winapi::VOID_ BOOST_WINAPI_WINAPI_CC
105 InitializeCriticalSection(boost::winapi::detail::winapi_critical_section* lpCriticalSection);
106 
107 BOOST_WINAPI_IMPORT_EXCEPT_WM boost::winapi::VOID_ BOOST_WINAPI_WINAPI_CC
108 EnterCriticalSection(boost::winapi::detail::winapi_critical_section* lpCriticalSection);
109 
110 BOOST_WINAPI_IMPORT_EXCEPT_WM boost::winapi::VOID_ BOOST_WINAPI_WINAPI_CC
111 LeaveCriticalSection(boost::winapi::detail::winapi_critical_section* lpCriticalSection);
112 
113 #if BOOST_USE_WINAPI_VERSION >= 0x0403
114 BOOST_WINAPI_IMPORT boost::winapi::BOOL_ BOOST_WINAPI_WINAPI_CC
115 InitializeCriticalSectionAndSpinCount(
116     boost::winapi::detail::winapi_critical_section* lpCriticalSection,
117     boost::winapi::DWORD_ dwSpinCount);
118 
119 BOOST_WINAPI_IMPORT boost::winapi::DWORD_ BOOST_WINAPI_WINAPI_CC
120 SetCriticalSectionSpinCount(
121     boost::winapi::detail::winapi_critical_section* lpCriticalSection,
122     boost::winapi::DWORD_ dwSpinCount);
123 #endif
124 
125 #if BOOST_USE_WINAPI_VERSION >= BOOST_WINAPI_VERSION_WIN6
126 BOOST_WINAPI_IMPORT boost::winapi::BOOL_ BOOST_WINAPI_WINAPI_CC
127 InitializeCriticalSectionEx(
128     boost::winapi::detail::winapi_critical_section* lpCriticalSection,
129     boost::winapi::DWORD_ dwSpinCount,
130     boost::winapi::DWORD_ Flags);
131 #endif
132 
133 #if BOOST_USE_WINAPI_VERSION >= BOOST_WINAPI_VERSION_NT4
134 BOOST_WINAPI_IMPORT_EXCEPT_WM boost::winapi::BOOL_ BOOST_WINAPI_WINAPI_CC
135 TryEnterCriticalSection(boost::winapi::detail::winapi_critical_section* lpCriticalSection);
136 #endif
137 
138 BOOST_WINAPI_IMPORT_EXCEPT_WM boost::winapi::VOID_ BOOST_WINAPI_WINAPI_CC
139 DeleteCriticalSection(boost::winapi::detail::winapi_critical_section* lpCriticalSection);
140 
141 #endif // defined( BOOST_WINAPI_IS_MINGW )
142 } // extern "C"
143 #endif
144 
145 namespace boost {
146 namespace winapi {
147 
148 
149 #pragma pack(push, 8)
150 
151 #if !defined(_WIN32_WCE)
152 
153 struct _RTL_CRITICAL_SECTION_DEBUG;
154 
155 typedef struct BOOST_MAY_ALIAS _RTL_CRITICAL_SECTION {
156     _RTL_CRITICAL_SECTION_DEBUG* DebugInfo;
157     LONG_ LockCount;
158     LONG_ RecursionCount;
159     HANDLE_ OwningThread;
160     HANDLE_ LockSemaphore;
161     ULONG_PTR_ SpinCount;
162 } CRITICAL_SECTION_, *PCRITICAL_SECTION_;
163 
164 #else
165 
166 // Windows CE has different layout
167 typedef struct BOOST_MAY_ALIAS CRITICAL_SECTION {
168     unsigned int LockCount;
169     HANDLE OwnerThread;
170     HANDLE hCrit;
171     DWORD needtrap;
172     DWORD dwContentions;
173 } CRITICAL_SECTION_, *LPCRITICAL_SECTION_;
174 
175 #endif
176 
177 #pragma pack(pop)
178 
179 #if BOOST_WINAPI_PARTITION_APP_SYSTEM
InitializeCriticalSection(CRITICAL_SECTION_ * lpCriticalSection)180 BOOST_FORCEINLINE VOID_ InitializeCriticalSection(CRITICAL_SECTION_* lpCriticalSection)
181 {
182     ::InitializeCriticalSection(winapi::detail::cast_ptr(lpCriticalSection));
183 }
184 #endif
185 
EnterCriticalSection(CRITICAL_SECTION_ * lpCriticalSection)186 BOOST_FORCEINLINE VOID_ EnterCriticalSection(CRITICAL_SECTION_* lpCriticalSection)
187 {
188     ::EnterCriticalSection(winapi::detail::cast_ptr(lpCriticalSection));
189 }
190 
LeaveCriticalSection(CRITICAL_SECTION_ * lpCriticalSection)191 BOOST_FORCEINLINE VOID_ LeaveCriticalSection(CRITICAL_SECTION_* lpCriticalSection)
192 {
193     ::LeaveCriticalSection(winapi::detail::cast_ptr(lpCriticalSection));
194 }
195 
196 #if BOOST_USE_WINAPI_VERSION >= 0x0403
197 #if BOOST_WINAPI_PARTITION_APP_SYSTEM
InitializeCriticalSectionAndSpinCount(CRITICAL_SECTION_ * lpCriticalSection,DWORD_ dwSpinCount)198 BOOST_FORCEINLINE BOOL_ InitializeCriticalSectionAndSpinCount(CRITICAL_SECTION_* lpCriticalSection, DWORD_ dwSpinCount)
199 {
200     return ::InitializeCriticalSectionAndSpinCount(winapi::detail::cast_ptr(lpCriticalSection), dwSpinCount);
201 }
202 
SetCriticalSectionSpinCount(CRITICAL_SECTION_ * lpCriticalSection,DWORD_ dwSpinCount)203 BOOST_FORCEINLINE DWORD_ SetCriticalSectionSpinCount(CRITICAL_SECTION_* lpCriticalSection, DWORD_ dwSpinCount)
204 {
205     return ::SetCriticalSectionSpinCount(winapi::detail::cast_ptr(lpCriticalSection), dwSpinCount);
206 }
207 #endif
208 
209 // CRITICAL_SECTION_NO_DEBUG_INFO is defined for WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_APP)
210 BOOST_CONSTEXPR_OR_CONST DWORD_ CRITICAL_SECTION_NO_DEBUG_INFO_ = 0x01000000;
211 BOOST_CONSTEXPR_OR_CONST DWORD_ CRITICAL_SECTION_FLAG_NO_DEBUG_INFO_ = CRITICAL_SECTION_NO_DEBUG_INFO_;
212 BOOST_CONSTEXPR_OR_CONST DWORD_ CRITICAL_SECTION_FLAG_DYNAMIC_SPIN_ = 0x02000000; // undocumented
213 BOOST_CONSTEXPR_OR_CONST DWORD_ CRITICAL_SECTION_FLAG_STATIC_INIT_ = 0x04000000; // undocumented
214 
215 #if BOOST_USE_WINAPI_VERSION >= BOOST_WINAPI_VERSION_WIN6
InitializeCriticalSectionEx(CRITICAL_SECTION_ * lpCriticalSection,DWORD_ dwSpinCount,DWORD_ Flags)216 BOOST_FORCEINLINE BOOL_ InitializeCriticalSectionEx(CRITICAL_SECTION_* lpCriticalSection, DWORD_ dwSpinCount, DWORD_ Flags)
217 {
218     return ::InitializeCriticalSectionEx(winapi::detail::cast_ptr(lpCriticalSection), dwSpinCount, Flags);
219 }
220 #endif // BOOST_USE_WINAPI_VERSION >= BOOST_WINAPI_VERSION_WIN6
221 #endif // BOOST_USE_WINAPI_VERSION >= 0x0403
222 
223 #if BOOST_USE_WINAPI_VERSION >= BOOST_WINAPI_VERSION_NT4
TryEnterCriticalSection(CRITICAL_SECTION_ * lpCriticalSection)224 BOOST_FORCEINLINE BOOL_ TryEnterCriticalSection(CRITICAL_SECTION_* lpCriticalSection)
225 {
226     return ::TryEnterCriticalSection(winapi::detail::cast_ptr(lpCriticalSection));
227 }
228 #endif // BOOST_USE_WINAPI_VERSION >= BOOST_WINAPI_VERSION_NT4
229 
DeleteCriticalSection(CRITICAL_SECTION_ * lpCriticalSection)230 BOOST_FORCEINLINE VOID_ DeleteCriticalSection(CRITICAL_SECTION_* lpCriticalSection)
231 {
232     ::DeleteCriticalSection(winapi::detail::cast_ptr(lpCriticalSection));
233 }
234 
235 }
236 }
237 
238 #include <boost/winapi/detail/footer.hpp>
239 
240 #endif // BOOST_WINAPI_CRITICAL_SECTION_HPP_INCLUDED_
241