semaphore.hpp 6.6 KB

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