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_SEMAPHORE_HPP_INCLUDED_
10 #define BOOST_WINAPI_SEMAPHORE_HPP_INCLUDED_
11
12 #include <boost/winapi/basic_types.hpp>
13 #include <boost/winapi/detail/header.hpp>
14
15 #ifdef BOOST_HAS_PRAGMA_ONCE
16 #pragma once
17 #endif
18
19 #if !defined( BOOST_USE_WINDOWS_H )
20 extern "C" {
21
22 #if BOOST_WINAPI_PARTITION_APP_SYSTEM
23 #if !defined( BOOST_NO_ANSI_APIS )
24
25 BOOST_WINAPI_IMPORT boost::winapi::HANDLE_ BOOST_WINAPI_WINAPI_CC
26 CreateSemaphoreA(
27 ::_SECURITY_ATTRIBUTES* lpSemaphoreAttributes,
28 boost::winapi::LONG_ lInitialCount,
29 boost::winapi::LONG_ lMaximumCount,
30 boost::winapi::LPCSTR_ lpName);
31
32 #if BOOST_USE_WINAPI_VERSION >= BOOST_WINAPI_VERSION_WIN6
33 BOOST_WINAPI_IMPORT boost::winapi::HANDLE_ BOOST_WINAPI_WINAPI_CC
34 CreateSemaphoreExA(
35 ::_SECURITY_ATTRIBUTES* lpSemaphoreAttributes,
36 boost::winapi::LONG_ lInitialCount,
37 boost::winapi::LONG_ lMaximumCount,
38 boost::winapi::LPCSTR_ lpName,
39 boost::winapi::DWORD_ dwFlags,
40 boost::winapi::DWORD_ dwDesiredAccess);
41 #endif
42
43 #endif // !defined( BOOST_NO_ANSI_APIS )
44
45 BOOST_WINAPI_IMPORT_EXCEPT_WM boost::winapi::HANDLE_ BOOST_WINAPI_WINAPI_CC
46 CreateSemaphoreW(
47 ::_SECURITY_ATTRIBUTES* lpSemaphoreAttributes,
48 boost::winapi::LONG_ lInitialCount,
49 boost::winapi::LONG_ lMaximumCount,
50 boost::winapi::LPCWSTR_ lpName);
51
52 #if BOOST_USE_WINAPI_VERSION >= BOOST_WINAPI_VERSION_WIN6
53 BOOST_WINAPI_IMPORT_EXCEPT_WM boost::winapi::HANDLE_ BOOST_WINAPI_WINAPI_CC
54 CreateSemaphoreExW(
55 ::_SECURITY_ATTRIBUTES* lpSemaphoreAttributes,
56 boost::winapi::LONG_ lInitialCount,
57 boost::winapi::LONG_ lMaximumCount,
58 boost::winapi::LPCWSTR_ lpName,
59 boost::winapi::DWORD_ dwFlags,
60 boost::winapi::DWORD_ dwDesiredAccess);
61 #endif
62
63 BOOST_WINAPI_IMPORT_EXCEPT_WM boost::winapi::BOOL_ BOOST_WINAPI_WINAPI_CC
64 ReleaseSemaphore(
65 boost::winapi::HANDLE_ hSemaphore,
66 boost::winapi::LONG_ lReleaseCount,
67 boost::winapi::LPLONG_ lpPreviousCount);
68
69 #endif // BOOST_WINAPI_PARTITION_APP_SYSTEM
70
71 #if BOOST_WINAPI_PARTITION_DESKTOP_SYSTEM
72
73 #if !defined( BOOST_NO_ANSI_APIS )
74 BOOST_WINAPI_IMPORT boost::winapi::HANDLE_ BOOST_WINAPI_WINAPI_CC
75 OpenSemaphoreA(
76 boost::winapi::DWORD_ dwDesiredAccess,
77 boost::winapi::BOOL_ bInheritHandle,
78 boost::winapi::LPCSTR_ lpName);
79 #endif // !defined( BOOST_NO_ANSI_APIS )
80
81 BOOST_WINAPI_IMPORT boost::winapi::HANDLE_ BOOST_WINAPI_WINAPI_CC
82 OpenSemaphoreW(
83 boost::winapi::DWORD_ dwDesiredAccess,
84 boost::winapi::BOOL_ bInheritHandle,
85 boost::winapi::LPCWSTR_ lpName);
86
87 #endif // BOOST_WINAPI_PARTITION_DESKTOP_SYSTEM
88
89 } // extern "C"
90 #endif // !defined( BOOST_USE_WINDOWS_H )
91
92 namespace boost {
93 namespace winapi {
94
95 #if BOOST_WINAPI_PARTITION_APP_SYSTEM
96
97 using ::ReleaseSemaphore;
98
99 #if defined( BOOST_USE_WINDOWS_H )
100
101 BOOST_CONSTEXPR_OR_CONST DWORD_ SEMAPHORE_ALL_ACCESS_ = SEMAPHORE_ALL_ACCESS;
102 BOOST_CONSTEXPR_OR_CONST DWORD_ SEMAPHORE_MODIFY_STATE_ = SEMAPHORE_MODIFY_STATE;
103
104 #else // defined( BOOST_USE_WINDOWS_H )
105
106 BOOST_CONSTEXPR_OR_CONST DWORD_ SEMAPHORE_ALL_ACCESS_ = 0x001F0003;
107 BOOST_CONSTEXPR_OR_CONST DWORD_ SEMAPHORE_MODIFY_STATE_ = 0x00000002;
108
109 #endif // defined( BOOST_USE_WINDOWS_H )
110
111 // Undocumented and not present in Windows SDK. Enables NtQuerySemaphore.
112 // http://undocumented.ntinternals.net/index.html?page=UserMode%2FUndocumented%20Functions%2FNT%20Objects%2FEvent%2FNtQueryEvent.html
113 BOOST_CONSTEXPR_OR_CONST DWORD_ SEMAPHORE_QUERY_STATE_ = 0x00000001;
114
115 BOOST_CONSTEXPR_OR_CONST DWORD_ semaphore_all_access = SEMAPHORE_ALL_ACCESS_;
116 BOOST_CONSTEXPR_OR_CONST DWORD_ semaphore_modify_state = SEMAPHORE_MODIFY_STATE_;
117
118
119 #if !defined( BOOST_NO_ANSI_APIS )
CreateSemaphoreA(SECURITY_ATTRIBUTES_ * lpSemaphoreAttributes,LONG_ lInitialCount,LONG_ lMaximumCount,LPCSTR_ lpName)120 BOOST_FORCEINLINE HANDLE_ CreateSemaphoreA(SECURITY_ATTRIBUTES_* lpSemaphoreAttributes, LONG_ lInitialCount, LONG_ lMaximumCount, LPCSTR_ lpName)
121 {
122 return ::CreateSemaphoreA(reinterpret_cast< ::_SECURITY_ATTRIBUTES* >(lpSemaphoreAttributes), lInitialCount, lMaximumCount, lpName);
123 }
124
125 #if BOOST_USE_WINAPI_VERSION >= BOOST_WINAPI_VERSION_WIN6
CreateSemaphoreExA(SECURITY_ATTRIBUTES_ * lpSemaphoreAttributes,LONG_ lInitialCount,LONG_ lMaximumCount,LPCSTR_ lpName,DWORD_ dwFlags,DWORD_ dwDesiredAccess)126 BOOST_FORCEINLINE HANDLE_ CreateSemaphoreExA(SECURITY_ATTRIBUTES_* lpSemaphoreAttributes, LONG_ lInitialCount, LONG_ lMaximumCount, LPCSTR_ lpName, DWORD_ dwFlags, DWORD_ dwDesiredAccess)
127 {
128 return ::CreateSemaphoreExA(reinterpret_cast< ::_SECURITY_ATTRIBUTES* >(lpSemaphoreAttributes), lInitialCount, lMaximumCount, lpName, dwFlags, dwDesiredAccess);
129 }
130 #endif
131 #endif // !defined( BOOST_NO_ANSI_APIS )
132
CreateSemaphoreW(SECURITY_ATTRIBUTES_ * lpSemaphoreAttributes,LONG_ lInitialCount,LONG_ lMaximumCount,LPCWSTR_ lpName)133 BOOST_FORCEINLINE HANDLE_ CreateSemaphoreW(SECURITY_ATTRIBUTES_* lpSemaphoreAttributes, LONG_ lInitialCount, LONG_ lMaximumCount, LPCWSTR_ lpName)
134 {
135 return ::CreateSemaphoreW(reinterpret_cast< ::_SECURITY_ATTRIBUTES* >(lpSemaphoreAttributes), lInitialCount, lMaximumCount, lpName);
136 }
137
138 #if BOOST_USE_WINAPI_VERSION >= BOOST_WINAPI_VERSION_WIN6
CreateSemaphoreExW(SECURITY_ATTRIBUTES_ * lpSemaphoreAttributes,LONG_ lInitialCount,LONG_ lMaximumCount,LPCWSTR_ lpName,DWORD_ dwFlags,DWORD_ dwDesiredAccess)139 BOOST_FORCEINLINE HANDLE_ CreateSemaphoreExW(SECURITY_ATTRIBUTES_* lpSemaphoreAttributes, LONG_ lInitialCount, LONG_ lMaximumCount, LPCWSTR_ lpName, DWORD_ dwFlags, DWORD_ dwDesiredAccess)
140 {
141 return ::CreateSemaphoreExW(reinterpret_cast< ::_SECURITY_ATTRIBUTES* >(lpSemaphoreAttributes), lInitialCount, lMaximumCount, lpName, dwFlags, dwDesiredAccess);
142 }
143 #endif
144
145 #if !defined( BOOST_NO_ANSI_APIS )
create_semaphore(SECURITY_ATTRIBUTES_ * lpSemaphoreAttributes,LONG_ lInitialCount,LONG_ lMaximumCount,LPCSTR_ lpName)146 BOOST_FORCEINLINE HANDLE_ create_semaphore(SECURITY_ATTRIBUTES_* lpSemaphoreAttributes, LONG_ lInitialCount, LONG_ lMaximumCount, LPCSTR_ lpName)
147 {
148 return winapi::CreateSemaphoreA(lpSemaphoreAttributes, lInitialCount, lMaximumCount, lpName);
149 }
150 #endif
151
create_semaphore(SECURITY_ATTRIBUTES_ * lpSemaphoreAttributes,LONG_ lInitialCount,LONG_ lMaximumCount,LPCWSTR_ lpName)152 BOOST_FORCEINLINE HANDLE_ create_semaphore(SECURITY_ATTRIBUTES_* lpSemaphoreAttributes, LONG_ lInitialCount, LONG_ lMaximumCount, LPCWSTR_ lpName)
153 {
154 return winapi::CreateSemaphoreW(lpSemaphoreAttributes, lInitialCount, lMaximumCount, lpName);
155 }
156
create_anonymous_semaphore(SECURITY_ATTRIBUTES_ * lpSemaphoreAttributes,LONG_ lInitialCount,LONG_ lMaximumCount)157 BOOST_FORCEINLINE HANDLE_ create_anonymous_semaphore(SECURITY_ATTRIBUTES_* lpSemaphoreAttributes, LONG_ lInitialCount, LONG_ lMaximumCount)
158 {
159 return winapi::CreateSemaphoreW(lpSemaphoreAttributes, lInitialCount, lMaximumCount, 0);
160 }
161
162 #endif // BOOST_WINAPI_PARTITION_APP_SYSTEM
163
164 #if BOOST_WINAPI_PARTITION_DESKTOP_SYSTEM
165
166 #if !defined( BOOST_NO_ANSI_APIS )
167 using ::OpenSemaphoreA;
168
open_semaphore(DWORD_ dwDesiredAccess,BOOL_ bInheritHandle,LPCSTR_ lpName)169 BOOST_FORCEINLINE HANDLE_ open_semaphore(DWORD_ dwDesiredAccess, BOOL_ bInheritHandle, LPCSTR_ lpName)
170 {
171 return ::OpenSemaphoreA(dwDesiredAccess, bInheritHandle, lpName);
172 }
173 #endif // !defined( BOOST_NO_ANSI_APIS )
174
175 using ::OpenSemaphoreW;
176
open_semaphore(DWORD_ dwDesiredAccess,BOOL_ bInheritHandle,LPCWSTR_ lpName)177 BOOST_FORCEINLINE HANDLE_ open_semaphore(DWORD_ dwDesiredAccess, BOOL_ bInheritHandle, LPCWSTR_ lpName)
178 {
179 return ::OpenSemaphoreW(dwDesiredAccess, bInheritHandle, lpName);
180 }
181
182 #endif // BOOST_WINAPI_PARTITION_DESKTOP_SYSTEM
183
184 }
185 }
186
187 #include <boost/winapi/detail/footer.hpp>
188
189 #endif // BOOST_WINAPI_SEMAPHORE_HPP_INCLUDED_
190