1 // semaphore.hpp --------------------------------------------------------------//
3 // Copyright 2010 Vicente J. Botet Escriba
4 // Copyright 2015 Andrey Semashev
6 // Distributed under the Boost Software License, Version 1.0.
7 // See http://www.boost.org/LICENSE_1_0.txt
10 #ifndef BOOST_DETAIL_WINAPI_SEMAPHORE_HPP
11 #define BOOST_DETAIL_WINAPI_SEMAPHORE_HPP
13 #include <boost/detail/winapi/basic_types.hpp>
14 #include <boost/predef/platform.h>
16 #ifdef BOOST_HAS_PRAGMA_ONCE
20 #if !defined( BOOST_USE_WINDOWS_H )
22 #if !defined( BOOST_NO_ANSI_APIS )
23 #if !defined( BOOST_PLAT_WINDOWS_RUNTIME_AVALIABLE )
24 BOOST_SYMBOL_IMPORT boost::detail::winapi::HANDLE_ WINAPI
26 ::_SECURITY_ATTRIBUTES* lpSemaphoreAttributes,
27 boost::detail::winapi::LONG_ lInitialCount,
28 boost::detail::winapi::LONG_ lMaximumCount,
29 boost::detail::winapi::LPCSTR_ lpName);
32 #if BOOST_USE_WINAPI_VERSION >= BOOST_WINAPI_VERSION_WIN6
33 BOOST_SYMBOL_IMPORT boost::detail::winapi::HANDLE_ WINAPI
35 ::_SECURITY_ATTRIBUTES* lpSemaphoreAttributes,
36 boost::detail::winapi::LONG_ lInitialCount,
37 boost::detail::winapi::LONG_ lMaximumCount,
38 boost::detail::winapi::LPCSTR_ lpName,
39 boost::detail::winapi::DWORD_ dwFlags,
40 boost::detail::winapi::DWORD_ dwDesiredAccess);
43 BOOST_SYMBOL_IMPORT boost::detail::winapi::HANDLE_ WINAPI
45 boost::detail::winapi::DWORD_ dwDesiredAccess,
46 boost::detail::winapi::BOOL_ bInheritHandle,
47 boost::detail::winapi::LPCSTR_ lpName);
50 BOOST_SYMBOL_IMPORT boost::detail::winapi::HANDLE_ WINAPI
52 ::_SECURITY_ATTRIBUTES* lpSemaphoreAttributes,
53 boost::detail::winapi::LONG_ lInitialCount,
54 boost::detail::winapi::LONG_ lMaximumCount,
55 boost::detail::winapi::LPCWSTR_ lpName);
57 #if BOOST_USE_WINAPI_VERSION >= BOOST_WINAPI_VERSION_WIN6
58 BOOST_SYMBOL_IMPORT boost::detail::winapi::HANDLE_ WINAPI
60 ::_SECURITY_ATTRIBUTES* lpSemaphoreAttributes,
61 boost::detail::winapi::LONG_ lInitialCount,
62 boost::detail::winapi::LONG_ lMaximumCount,
63 boost::detail::winapi::LPCWSTR_ lpName,
64 boost::detail::winapi::DWORD_ dwFlags,
65 boost::detail::winapi::DWORD_ dwDesiredAccess);
68 BOOST_SYMBOL_IMPORT boost::detail::winapi::HANDLE_ WINAPI
70 boost::detail::winapi::DWORD_ dwDesiredAccess,
71 boost::detail::winapi::BOOL_ bInheritHandle,
72 boost::detail::winapi::LPCWSTR_ lpName);
74 BOOST_SYMBOL_IMPORT boost::detail::winapi::BOOL_ WINAPI
76 boost::detail::winapi::HANDLE_ hSemaphore,
77 boost::detail::winapi::LONG_ lReleaseCount,
78 boost::detail::winapi::LPLONG_ lpPreviousCount);
86 #if !defined( BOOST_NO_ANSI_APIS )
87 using ::OpenSemaphoreA;
89 using ::OpenSemaphoreW;
90 using ::ReleaseSemaphore;
92 #if defined( BOOST_USE_WINDOWS_H )
94 const DWORD_ SEMAPHORE_ALL_ACCESS_ = SEMAPHORE_ALL_ACCESS;
95 const DWORD_ SEMAPHORE_MODIFY_STATE_ = SEMAPHORE_MODIFY_STATE;
97 #else // defined( BOOST_USE_WINDOWS_H )
99 const DWORD_ SEMAPHORE_ALL_ACCESS_ = 0x001F0003;
100 const DWORD_ SEMAPHORE_MODIFY_STATE_ = 0x00000002;
102 #endif // defined( BOOST_USE_WINDOWS_H )
104 // Undocumented and not present in Windows SDK. Enables NtQuerySemaphore.
105 // http://undocumented.ntinternals.net/index.html?page=UserMode%2FUndocumented%20Functions%2FNT%20Objects%2FEvent%2FNtQueryEvent.html
106 const DWORD_ SEMAPHORE_QUERY_STATE_ = 0x00000001;
108 const DWORD_ semaphore_all_access = SEMAPHORE_ALL_ACCESS_;
109 const DWORD_ semaphore_modify_state = SEMAPHORE_MODIFY_STATE_;
112 #if !defined( BOOST_NO_ANSI_APIS )
113 BOOST_FORCEINLINE HANDLE_ CreateSemaphoreA(SECURITY_ATTRIBUTES_* lpSemaphoreAttributes, LONG_ lInitialCount, LONG_ lMaximumCount, LPCSTR_ lpName)
115 #if BOOST_PLAT_WINDOWS_RUNTIME && BOOST_USE_WINAPI_VERSION >= BOOST_WINAPI_VERSION_WIN6
116 return ::CreateSemaphoreExA(reinterpret_cast< ::_SECURITY_ATTRIBUTES* >(lpSemaphoreAttributes), lInitialCount, lMaximumCount, lpName, 0, semaphore_all_access);
118 return ::CreateSemaphoreA(reinterpret_cast< ::_SECURITY_ATTRIBUTES* >(lpSemaphoreAttributes), lInitialCount, lMaximumCount, lpName);
122 #if BOOST_USE_WINAPI_VERSION >= BOOST_WINAPI_VERSION_WIN6
123 BOOST_FORCEINLINE HANDLE_ CreateSemaphoreExA(SECURITY_ATTRIBUTES_* lpSemaphoreAttributes, LONG_ lInitialCount, LONG_ lMaximumCount, LPCSTR_ lpName, DWORD_ dwFlags, DWORD_ dwDesiredAccess)
125 return ::CreateSemaphoreExA(reinterpret_cast< ::_SECURITY_ATTRIBUTES* >(lpSemaphoreAttributes), lInitialCount, lMaximumCount, lpName, dwFlags, dwDesiredAccess);
130 BOOST_FORCEINLINE HANDLE_ CreateSemaphoreW(SECURITY_ATTRIBUTES_* lpSemaphoreAttributes, LONG_ lInitialCount, LONG_ lMaximumCount, LPCWSTR_ lpName)
132 #if BOOST_PLAT_WINDOWS_RUNTIME && BOOST_USE_WINAPI_VERSION >= BOOST_WINAPI_VERSION_WIN6
133 return ::CreateSemaphoreExW(reinterpret_cast< ::_SECURITY_ATTRIBUTES* >(lpSemaphoreAttributes), lInitialCount, lMaximumCount, lpName, 0, semaphore_all_access);
135 return ::CreateSemaphoreW(reinterpret_cast< ::_SECURITY_ATTRIBUTES* >(lpSemaphoreAttributes), lInitialCount, lMaximumCount, lpName);
139 #if BOOST_USE_WINAPI_VERSION >= BOOST_WINAPI_VERSION_WIN6
140 BOOST_FORCEINLINE HANDLE_ CreateSemaphoreExW(SECURITY_ATTRIBUTES_* lpSemaphoreAttributes, LONG_ lInitialCount, LONG_ lMaximumCount, LPCWSTR_ lpName, DWORD_ dwFlags, DWORD_ dwDesiredAccess)
142 return ::CreateSemaphoreExW(reinterpret_cast< ::_SECURITY_ATTRIBUTES* >(lpSemaphoreAttributes), lInitialCount, lMaximumCount, lpName, dwFlags, dwDesiredAccess);
146 #if !defined( BOOST_NO_ANSI_APIS )
147 BOOST_FORCEINLINE HANDLE_ create_semaphore(SECURITY_ATTRIBUTES_* lpSemaphoreAttributes, LONG_ lInitialCount, LONG_ lMaximumCount, LPCSTR_ lpName)
149 return winapi::CreateSemaphoreA(lpSemaphoreAttributes, lInitialCount, lMaximumCount, lpName);
152 BOOST_FORCEINLINE HANDLE_ open_semaphore(DWORD_ dwDesiredAccess, BOOL_ bInheritHandle, LPCSTR_ lpName)
154 return ::OpenSemaphoreA(dwDesiredAccess, bInheritHandle, lpName);
158 BOOST_FORCEINLINE HANDLE_ create_semaphore(SECURITY_ATTRIBUTES_* lpSemaphoreAttributes, LONG_ lInitialCount, LONG_ lMaximumCount, LPCWSTR_ lpName)
160 return winapi::CreateSemaphoreW(lpSemaphoreAttributes, lInitialCount, lMaximumCount, lpName);
163 BOOST_FORCEINLINE HANDLE_ open_semaphore(DWORD_ dwDesiredAccess, BOOL_ bInheritHandle, LPCWSTR_ lpName)
165 return ::OpenSemaphoreW(dwDesiredAccess, bInheritHandle, lpName);
168 BOOST_FORCEINLINE HANDLE_ create_anonymous_semaphore(SECURITY_ATTRIBUTES_* lpSemaphoreAttributes, LONG_ lInitialCount, LONG_ lMaximumCount)
170 return winapi::CreateSemaphoreW(lpSemaphoreAttributes, lInitialCount, lMaximumCount, 0);
177 #endif // BOOST_DETAIL_WINAPI_SEMAPHORE_HPP