mutex.hpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * Copyright 2010 Vicente J. Botet Escriba
  3. * Copyright 2015, 2017 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_MUTEX_HPP_INCLUDED_
  9. #define BOOST_WINAPI_MUTEX_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 ) && BOOST_WINAPI_PARTITION_APP_SYSTEM
  16. extern "C" {
  17. #if !defined( BOOST_NO_ANSI_APIS )
  18. BOOST_WINAPI_IMPORT boost::winapi::HANDLE_ BOOST_WINAPI_WINAPI_CC
  19. CreateMutexA(
  20. ::_SECURITY_ATTRIBUTES* lpMutexAttributes,
  21. boost::winapi::BOOL_ bInitialOwner,
  22. boost::winapi::LPCSTR_ lpName);
  23. #endif
  24. BOOST_WINAPI_IMPORT_EXCEPT_WM boost::winapi::HANDLE_ BOOST_WINAPI_WINAPI_CC
  25. CreateMutexW(
  26. ::_SECURITY_ATTRIBUTES* lpMutexAttributes,
  27. boost::winapi::BOOL_ bInitialOwner,
  28. boost::winapi::LPCWSTR_ lpName);
  29. } // extern "C"
  30. #endif // !defined( BOOST_USE_WINDOWS_H ) && BOOST_WINAPI_PARTITION_APP_SYSTEM
  31. #if !defined( BOOST_USE_WINDOWS_H )
  32. extern "C" {
  33. #if !defined( BOOST_NO_ANSI_APIS )
  34. #if BOOST_USE_WINAPI_VERSION >= BOOST_WINAPI_VERSION_WIN6
  35. BOOST_WINAPI_IMPORT boost::winapi::HANDLE_ BOOST_WINAPI_WINAPI_CC
  36. CreateMutexExA(
  37. ::_SECURITY_ATTRIBUTES* lpMutexAttributes,
  38. boost::winapi::LPCSTR_ lpName,
  39. boost::winapi::DWORD_ dwFlags,
  40. boost::winapi::DWORD_ dwDesiredAccess);
  41. #endif
  42. BOOST_WINAPI_IMPORT boost::winapi::HANDLE_ BOOST_WINAPI_WINAPI_CC
  43. OpenMutexA(
  44. boost::winapi::DWORD_ dwDesiredAccess,
  45. boost::winapi::BOOL_ bInheritHandle,
  46. boost::winapi::LPCSTR_ lpName);
  47. #endif // !defined( BOOST_NO_ANSI_APIS )
  48. #if BOOST_USE_WINAPI_VERSION >= BOOST_WINAPI_VERSION_WIN6
  49. BOOST_WINAPI_IMPORT boost::winapi::HANDLE_ BOOST_WINAPI_WINAPI_CC
  50. CreateMutexExW(
  51. ::_SECURITY_ATTRIBUTES* lpMutexAttributes,
  52. boost::winapi::LPCWSTR_ lpName,
  53. boost::winapi::DWORD_ dwFlags,
  54. boost::winapi::DWORD_ dwDesiredAccess);
  55. #endif
  56. BOOST_WINAPI_IMPORT boost::winapi::HANDLE_ BOOST_WINAPI_WINAPI_CC
  57. OpenMutexW(
  58. boost::winapi::DWORD_ dwDesiredAccess,
  59. boost::winapi::BOOL_ bInheritHandle,
  60. boost::winapi::LPCWSTR_ lpName);
  61. BOOST_WINAPI_IMPORT_EXCEPT_WM boost::winapi::BOOL_ BOOST_WINAPI_WINAPI_CC
  62. ReleaseMutex(boost::winapi::HANDLE_ hMutex);
  63. } // extern "C"
  64. #endif
  65. namespace boost {
  66. namespace winapi {
  67. #if !defined( BOOST_NO_ANSI_APIS )
  68. using ::OpenMutexA;
  69. #endif
  70. using ::OpenMutexW;
  71. using ::ReleaseMutex;
  72. #if defined( BOOST_USE_WINDOWS_H )
  73. BOOST_CONSTEXPR_OR_CONST DWORD_ MUTEX_ALL_ACCESS_ = MUTEX_ALL_ACCESS;
  74. BOOST_CONSTEXPR_OR_CONST DWORD_ MUTEX_MODIFY_STATE_ = MUTEX_MODIFY_STATE;
  75. #if BOOST_USE_WINAPI_VERSION >= BOOST_WINAPI_VERSION_WIN6
  76. BOOST_CONSTEXPR_OR_CONST DWORD_ CREATE_MUTEX_INITIAL_OWNER_ = CREATE_MUTEX_INITIAL_OWNER;
  77. #endif
  78. #else // defined( BOOST_USE_WINDOWS_H )
  79. BOOST_CONSTEXPR_OR_CONST DWORD_ MUTEX_ALL_ACCESS_ = 0x001F0001;
  80. BOOST_CONSTEXPR_OR_CONST DWORD_ MUTEX_MODIFY_STATE_ = 0x00000001;
  81. #if BOOST_USE_WINAPI_VERSION >= BOOST_WINAPI_VERSION_WIN6
  82. BOOST_CONSTEXPR_OR_CONST DWORD_ CREATE_MUTEX_INITIAL_OWNER_ = 0x00000001;
  83. #endif
  84. #endif // defined( BOOST_USE_WINDOWS_H )
  85. BOOST_CONSTEXPR_OR_CONST DWORD_ mutex_all_access = MUTEX_ALL_ACCESS_;
  86. BOOST_CONSTEXPR_OR_CONST DWORD_ mutex_modify_state = MUTEX_MODIFY_STATE_;
  87. #if BOOST_USE_WINAPI_VERSION >= BOOST_WINAPI_VERSION_WIN6
  88. BOOST_CONSTEXPR_OR_CONST DWORD_ create_mutex_initial_owner = CREATE_MUTEX_INITIAL_OWNER_;
  89. #endif
  90. #if !defined( BOOST_NO_ANSI_APIS )
  91. BOOST_FORCEINLINE HANDLE_ CreateMutexA(SECURITY_ATTRIBUTES_* lpMutexAttributes, BOOL_ bInitialOwner, LPCSTR_ lpName)
  92. {
  93. #if !BOOST_WINAPI_PARTITION_APP_SYSTEM && BOOST_USE_WINAPI_VERSION >= BOOST_WINAPI_VERSION_WIN6
  94. const DWORD_ flags = bInitialOwner ? create_mutex_initial_owner : 0u;
  95. return ::CreateMutexExA(reinterpret_cast< ::_SECURITY_ATTRIBUTES* >(lpMutexAttributes), lpName, flags, mutex_all_access);
  96. #else
  97. return ::CreateMutexA(reinterpret_cast< ::_SECURITY_ATTRIBUTES* >(lpMutexAttributes), bInitialOwner, lpName);
  98. #endif
  99. }
  100. #if BOOST_USE_WINAPI_VERSION >= BOOST_WINAPI_VERSION_WIN6
  101. BOOST_FORCEINLINE HANDLE_ CreateMutexExA(
  102. SECURITY_ATTRIBUTES_* lpMutexAttributes,
  103. LPCSTR_ lpName,
  104. DWORD_ dwFlags,
  105. DWORD_ dwDesiredAccess)
  106. {
  107. return ::CreateMutexExA(reinterpret_cast< ::_SECURITY_ATTRIBUTES* >(lpMutexAttributes), lpName, dwFlags, dwDesiredAccess);
  108. }
  109. #endif
  110. #endif
  111. BOOST_FORCEINLINE HANDLE_ CreateMutexW(SECURITY_ATTRIBUTES_* lpMutexAttributes, BOOL_ bInitialOwner, LPCWSTR_ lpName)
  112. {
  113. #if !BOOST_WINAPI_PARTITION_APP_SYSTEM && BOOST_USE_WINAPI_VERSION >= BOOST_WINAPI_VERSION_WIN6
  114. const DWORD_ flags = bInitialOwner ? create_mutex_initial_owner : 0u;
  115. return ::CreateMutexExW(reinterpret_cast< ::_SECURITY_ATTRIBUTES* >(lpMutexAttributes), lpName, flags, mutex_all_access);
  116. #else
  117. return ::CreateMutexW(reinterpret_cast< ::_SECURITY_ATTRIBUTES* >(lpMutexAttributes), bInitialOwner, lpName);
  118. #endif
  119. }
  120. #if BOOST_USE_WINAPI_VERSION >= BOOST_WINAPI_VERSION_WIN6
  121. BOOST_FORCEINLINE HANDLE_ CreateMutexExW(
  122. SECURITY_ATTRIBUTES_* lpMutexAttributes,
  123. LPCWSTR_ lpName,
  124. DWORD_ dwFlags,
  125. DWORD_ dwDesiredAccess)
  126. {
  127. return ::CreateMutexExW(reinterpret_cast< ::_SECURITY_ATTRIBUTES* >(lpMutexAttributes), lpName, dwFlags, dwDesiredAccess);
  128. }
  129. #endif
  130. #if !defined( BOOST_NO_ANSI_APIS )
  131. BOOST_FORCEINLINE HANDLE_ create_mutex(SECURITY_ATTRIBUTES_* lpAttributes, BOOL_ bInitialOwner, LPCSTR_ lpName)
  132. {
  133. return winapi::CreateMutexA(lpAttributes, bInitialOwner, lpName);
  134. }
  135. BOOST_FORCEINLINE HANDLE_ open_mutex(DWORD_ dwDesiredAccess, BOOL_ bInheritHandle, LPCSTR_ lpName)
  136. {
  137. return ::OpenMutexA(dwDesiredAccess, bInheritHandle, lpName);
  138. }
  139. #endif
  140. BOOST_FORCEINLINE HANDLE_ create_mutex(SECURITY_ATTRIBUTES_* lpAttributes, BOOL_ bInitialOwner, LPCWSTR_ lpName)
  141. {
  142. return winapi::CreateMutexW(lpAttributes, bInitialOwner, lpName);
  143. }
  144. BOOST_FORCEINLINE HANDLE_ open_mutex(DWORD_ dwDesiredAccess, BOOL_ bInheritHandle, LPCWSTR_ lpName)
  145. {
  146. return ::OpenMutexW(dwDesiredAccess, bInheritHandle, lpName);
  147. }
  148. BOOST_FORCEINLINE HANDLE_ create_anonymous_mutex(SECURITY_ATTRIBUTES_* lpAttributes, BOOL_ bInitialOwner)
  149. {
  150. return winapi::CreateMutexW(lpAttributes, bInitialOwner, 0);
  151. }
  152. }
  153. }
  154. #include <boost/winapi/detail/footer.hpp>
  155. #endif // BOOST_WINAPI_MUTEX_HPP_INCLUDED_