semaphore.hpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // semaphore.hpp --------------------------------------------------------------//
  2. // Copyright 2010 Vicente J. Botet Escriba
  3. // Copyright 2015 Andrey Semashev
  4. // Distributed under the Boost Software License, Version 1.0.
  5. // See http://www.boost.org/LICENSE_1_0.txt
  6. #ifndef BOOST_DETAIL_WINAPI_SEMAPHORE_HPP
  7. #define BOOST_DETAIL_WINAPI_SEMAPHORE_HPP
  8. #include <boost/detail/winapi/basic_types.hpp>
  9. #include <boost/predef/platform.h>
  10. #ifdef BOOST_HAS_PRAGMA_ONCE
  11. #pragma once
  12. #endif
  13. #if !defined( BOOST_USE_WINDOWS_H )
  14. extern "C" {
  15. #if !defined( BOOST_NO_ANSI_APIS )
  16. #if !defined( BOOST_PLAT_WINDOWS_RUNTIME_AVALIABLE )
  17. BOOST_SYMBOL_IMPORT boost::detail::winapi::HANDLE_ WINAPI
  18. CreateSemaphoreA(
  19. ::_SECURITY_ATTRIBUTES* lpSemaphoreAttributes,
  20. boost::detail::winapi::LONG_ lInitialCount,
  21. boost::detail::winapi::LONG_ lMaximumCount,
  22. boost::detail::winapi::LPCSTR_ lpName);
  23. #endif
  24. #if BOOST_USE_WINAPI_VERSION >= BOOST_WINAPI_VERSION_WIN6
  25. BOOST_SYMBOL_IMPORT boost::detail::winapi::HANDLE_ WINAPI
  26. CreateSemaphoreExA(
  27. ::_SECURITY_ATTRIBUTES* lpSemaphoreAttributes,
  28. boost::detail::winapi::LONG_ lInitialCount,
  29. boost::detail::winapi::LONG_ lMaximumCount,
  30. boost::detail::winapi::LPCSTR_ lpName,
  31. boost::detail::winapi::DWORD_ dwFlags,
  32. boost::detail::winapi::DWORD_ dwDesiredAccess);
  33. #endif
  34. BOOST_SYMBOL_IMPORT boost::detail::winapi::HANDLE_ WINAPI
  35. OpenSemaphoreA(
  36. boost::detail::winapi::DWORD_ dwDesiredAccess,
  37. boost::detail::winapi::BOOL_ bInheritHandle,
  38. boost::detail::winapi::LPCSTR_ lpName);
  39. #endif
  40. BOOST_SYMBOL_IMPORT boost::detail::winapi::HANDLE_ WINAPI
  41. CreateSemaphoreW(
  42. ::_SECURITY_ATTRIBUTES* lpSemaphoreAttributes,
  43. boost::detail::winapi::LONG_ lInitialCount,
  44. boost::detail::winapi::LONG_ lMaximumCount,
  45. boost::detail::winapi::LPCWSTR_ lpName);
  46. #if BOOST_USE_WINAPI_VERSION >= BOOST_WINAPI_VERSION_WIN6
  47. BOOST_SYMBOL_IMPORT boost::detail::winapi::HANDLE_ WINAPI
  48. CreateSemaphoreExW(
  49. ::_SECURITY_ATTRIBUTES* lpSemaphoreAttributes,
  50. boost::detail::winapi::LONG_ lInitialCount,
  51. boost::detail::winapi::LONG_ lMaximumCount,
  52. boost::detail::winapi::LPCWSTR_ lpName,
  53. boost::detail::winapi::DWORD_ dwFlags,
  54. boost::detail::winapi::DWORD_ dwDesiredAccess);
  55. #endif
  56. BOOST_SYMBOL_IMPORT boost::detail::winapi::HANDLE_ WINAPI
  57. OpenSemaphoreW(
  58. boost::detail::winapi::DWORD_ dwDesiredAccess,
  59. boost::detail::winapi::BOOL_ bInheritHandle,
  60. boost::detail::winapi::LPCWSTR_ lpName);
  61. BOOST_SYMBOL_IMPORT boost::detail::winapi::BOOL_ WINAPI
  62. ReleaseSemaphore(
  63. boost::detail::winapi::HANDLE_ hSemaphore,
  64. boost::detail::winapi::LONG_ lReleaseCount,
  65. boost::detail::winapi::LPLONG_ lpPreviousCount);
  66. }
  67. #endif
  68. namespace boost {
  69. namespace detail {
  70. namespace winapi {
  71. #if !defined( BOOST_NO_ANSI_APIS )
  72. using ::OpenSemaphoreA;
  73. #endif
  74. using ::OpenSemaphoreW;
  75. using ::ReleaseSemaphore;
  76. #if defined( BOOST_USE_WINDOWS_H )
  77. const DWORD_ SEMAPHORE_ALL_ACCESS_ = SEMAPHORE_ALL_ACCESS;
  78. const DWORD_ SEMAPHORE_MODIFY_STATE_ = SEMAPHORE_MODIFY_STATE;
  79. #else // defined( BOOST_USE_WINDOWS_H )
  80. const DWORD_ SEMAPHORE_ALL_ACCESS_ = 0x001F0003;
  81. const DWORD_ SEMAPHORE_MODIFY_STATE_ = 0x00000002;
  82. #endif // defined( BOOST_USE_WINDOWS_H )
  83. // Undocumented and not present in Windows SDK. Enables NtQuerySemaphore.
  84. // http://undocumented.ntinternals.net/index.html?page=UserMode%2FUndocumented%20Functions%2FNT%20Objects%2FEvent%2FNtQueryEvent.html
  85. const DWORD_ SEMAPHORE_QUERY_STATE_ = 0x00000001;
  86. const DWORD_ semaphore_all_access = SEMAPHORE_ALL_ACCESS_;
  87. const DWORD_ semaphore_modify_state = SEMAPHORE_MODIFY_STATE_;
  88. #if !defined( BOOST_NO_ANSI_APIS )
  89. BOOST_FORCEINLINE HANDLE_ CreateSemaphoreA(SECURITY_ATTRIBUTES_* lpSemaphoreAttributes, LONG_ lInitialCount, LONG_ lMaximumCount, LPCSTR_ lpName)
  90. {
  91. #if BOOST_PLAT_WINDOWS_RUNTIME && BOOST_USE_WINAPI_VERSION >= BOOST_WINAPI_VERSION_WIN6
  92. return ::CreateSemaphoreExA(reinterpret_cast< ::_SECURITY_ATTRIBUTES* >(lpSemaphoreAttributes), lInitialCount, lMaximumCount, lpName, 0, semaphore_all_access);
  93. #else
  94. return ::CreateSemaphoreA(reinterpret_cast< ::_SECURITY_ATTRIBUTES* >(lpSemaphoreAttributes), lInitialCount, lMaximumCount, lpName);
  95. #endif
  96. }
  97. #if BOOST_USE_WINAPI_VERSION >= BOOST_WINAPI_VERSION_WIN6
  98. BOOST_FORCEINLINE HANDLE_ CreateSemaphoreExA(SECURITY_ATTRIBUTES_* lpSemaphoreAttributes, LONG_ lInitialCount, LONG_ lMaximumCount, LPCSTR_ lpName, DWORD_ dwFlags, DWORD_ dwDesiredAccess)
  99. {
  100. return ::CreateSemaphoreExA(reinterpret_cast< ::_SECURITY_ATTRIBUTES* >(lpSemaphoreAttributes), lInitialCount, lMaximumCount, lpName, dwFlags, dwDesiredAccess);
  101. }
  102. #endif
  103. #endif
  104. BOOST_FORCEINLINE HANDLE_ CreateSemaphoreW(SECURITY_ATTRIBUTES_* lpSemaphoreAttributes, LONG_ lInitialCount, LONG_ lMaximumCount, LPCWSTR_ lpName)
  105. {
  106. #if BOOST_PLAT_WINDOWS_RUNTIME && BOOST_USE_WINAPI_VERSION >= BOOST_WINAPI_VERSION_WIN6
  107. return ::CreateSemaphoreExW(reinterpret_cast< ::_SECURITY_ATTRIBUTES* >(lpSemaphoreAttributes), lInitialCount, lMaximumCount, lpName, 0, semaphore_all_access);
  108. #else
  109. return ::CreateSemaphoreW(reinterpret_cast< ::_SECURITY_ATTRIBUTES* >(lpSemaphoreAttributes), lInitialCount, lMaximumCount, lpName);
  110. #endif
  111. }
  112. #if BOOST_USE_WINAPI_VERSION >= BOOST_WINAPI_VERSION_WIN6
  113. BOOST_FORCEINLINE HANDLE_ CreateSemaphoreExW(SECURITY_ATTRIBUTES_* lpSemaphoreAttributes, LONG_ lInitialCount, LONG_ lMaximumCount, LPCWSTR_ lpName, DWORD_ dwFlags, DWORD_ dwDesiredAccess)
  114. {
  115. return ::CreateSemaphoreExW(reinterpret_cast< ::_SECURITY_ATTRIBUTES* >(lpSemaphoreAttributes), lInitialCount, lMaximumCount, lpName, dwFlags, dwDesiredAccess);
  116. }
  117. #endif
  118. #if !defined( BOOST_NO_ANSI_APIS )
  119. BOOST_FORCEINLINE HANDLE_ create_semaphore(SECURITY_ATTRIBUTES_* lpSemaphoreAttributes, LONG_ lInitialCount, LONG_ lMaximumCount, LPCSTR_ lpName)
  120. {
  121. return winapi::CreateSemaphoreA(lpSemaphoreAttributes, lInitialCount, lMaximumCount, lpName);
  122. }
  123. BOOST_FORCEINLINE HANDLE_ open_semaphore(DWORD_ dwDesiredAccess, BOOL_ bInheritHandle, LPCSTR_ lpName)
  124. {
  125. return ::OpenSemaphoreA(dwDesiredAccess, bInheritHandle, lpName);
  126. }
  127. #endif
  128. BOOST_FORCEINLINE HANDLE_ create_semaphore(SECURITY_ATTRIBUTES_* lpSemaphoreAttributes, LONG_ lInitialCount, LONG_ lMaximumCount, LPCWSTR_ lpName)
  129. {
  130. return winapi::CreateSemaphoreW(lpSemaphoreAttributes, lInitialCount, lMaximumCount, lpName);
  131. }
  132. BOOST_FORCEINLINE HANDLE_ open_semaphore(DWORD_ dwDesiredAccess, BOOL_ bInheritHandle, LPCWSTR_ lpName)
  133. {
  134. return ::OpenSemaphoreW(dwDesiredAccess, bInheritHandle, lpName);
  135. }
  136. BOOST_FORCEINLINE HANDLE_ create_anonymous_semaphore(SECURITY_ATTRIBUTES_* lpSemaphoreAttributes, LONG_ lInitialCount, LONG_ lMaximumCount)
  137. {
  138. return winapi::CreateSemaphoreW(lpSemaphoreAttributes, lInitialCount, lMaximumCount, 0);
  139. }
  140. }
  141. }
  142. }
  143. #endif // BOOST_DETAIL_WINAPI_SEMAPHORE_HPP