mutex.hpp 6.1 KB

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