spin_mutex.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. Copyright (c) 2005-2020 Intel Corporation
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. #ifndef __TBB_spin_mutex_H
  14. #define __TBB_spin_mutex_H
  15. #define __TBB_spin_mutex_H_include_area
  16. #include "internal/_warning_suppress_enable_notice.h"
  17. #include <cstddef>
  18. #include <new>
  19. #include "aligned_space.h"
  20. #include "tbb_stddef.h"
  21. #include "tbb_machine.h"
  22. #include "tbb_profiling.h"
  23. #include "internal/_mutex_padding.h"
  24. namespace tbb {
  25. //! A lock that occupies a single byte.
  26. /** A spin_mutex is a spin mutex that fits in a single byte.
  27. It should be used only for locking short critical sections
  28. (typically less than 20 instructions) when fairness is not an issue.
  29. If zero-initialized, the mutex is considered unheld.
  30. @ingroup synchronization */
  31. class spin_mutex : internal::mutex_copy_deprecated_and_disabled {
  32. //! 0 if lock is released, 1 if lock is acquired.
  33. __TBB_atomic_flag flag;
  34. public:
  35. //! Construct unacquired lock.
  36. /** Equivalent to zero-initialization of *this. */
  37. spin_mutex() : flag(0) {
  38. #if TBB_USE_THREADING_TOOLS
  39. internal_construct();
  40. #endif
  41. }
  42. //! Represents acquisition of a mutex.
  43. class scoped_lock : internal::no_copy {
  44. private:
  45. //! Points to currently held mutex, or NULL if no lock is held.
  46. spin_mutex* my_mutex;
  47. //! Value to store into spin_mutex::flag to unlock the mutex.
  48. /** This variable is no longer used. Instead, 0 and 1 are used to
  49. represent that the lock is free and acquired, respectively.
  50. We keep the member variable here to ensure backward compatibility */
  51. __TBB_Flag my_unlock_value;
  52. //! Like acquire, but with ITT instrumentation.
  53. void __TBB_EXPORTED_METHOD internal_acquire( spin_mutex& m );
  54. //! Like try_acquire, but with ITT instrumentation.
  55. bool __TBB_EXPORTED_METHOD internal_try_acquire( spin_mutex& m );
  56. //! Like release, but with ITT instrumentation.
  57. void __TBB_EXPORTED_METHOD internal_release();
  58. friend class spin_mutex;
  59. public:
  60. //! Construct without acquiring a mutex.
  61. scoped_lock() : my_mutex(NULL), my_unlock_value(0) {}
  62. //! Construct and acquire lock on a mutex.
  63. scoped_lock( spin_mutex& m ) : my_unlock_value(0) {
  64. internal::suppress_unused_warning(my_unlock_value);
  65. #if TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT
  66. my_mutex=NULL;
  67. internal_acquire(m);
  68. #else
  69. my_mutex=&m;
  70. __TBB_LockByte(m.flag);
  71. #endif /* TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT*/
  72. }
  73. //! Acquire lock.
  74. void acquire( spin_mutex& m ) {
  75. #if TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT
  76. internal_acquire(m);
  77. #else
  78. my_mutex = &m;
  79. __TBB_LockByte(m.flag);
  80. #endif /* TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT*/
  81. }
  82. //! Try acquiring lock (non-blocking)
  83. /** Return true if lock acquired; false otherwise. */
  84. bool try_acquire( spin_mutex& m ) {
  85. #if TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT
  86. return internal_try_acquire(m);
  87. #else
  88. bool result = __TBB_TryLockByte(m.flag);
  89. if( result )
  90. my_mutex = &m;
  91. return result;
  92. #endif /* TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT*/
  93. }
  94. //! Release lock
  95. void release() {
  96. #if TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT
  97. internal_release();
  98. #else
  99. __TBB_UnlockByte(my_mutex->flag);
  100. my_mutex = NULL;
  101. #endif /* TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT */
  102. }
  103. //! Destroy lock. If holding a lock, releases the lock first.
  104. ~scoped_lock() {
  105. if( my_mutex ) {
  106. #if TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT
  107. internal_release();
  108. #else
  109. __TBB_UnlockByte(my_mutex->flag);
  110. #endif /* TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT */
  111. }
  112. }
  113. };
  114. //! Internal constructor with ITT instrumentation.
  115. void __TBB_EXPORTED_METHOD internal_construct();
  116. // Mutex traits
  117. static const bool is_rw_mutex = false;
  118. static const bool is_recursive_mutex = false;
  119. static const bool is_fair_mutex = false;
  120. // ISO C++0x compatibility methods
  121. //! Acquire lock
  122. void lock() {
  123. #if TBB_USE_THREADING_TOOLS
  124. aligned_space<scoped_lock> tmp;
  125. new(tmp.begin()) scoped_lock(*this);
  126. #else
  127. __TBB_LockByte(flag);
  128. #endif /* TBB_USE_THREADING_TOOLS*/
  129. }
  130. //! Try acquiring lock (non-blocking)
  131. /** Return true if lock acquired; false otherwise. */
  132. bool try_lock() {
  133. #if TBB_USE_THREADING_TOOLS
  134. aligned_space<scoped_lock> tmp;
  135. return (new(tmp.begin()) scoped_lock)->internal_try_acquire(*this);
  136. #else
  137. return __TBB_TryLockByte(flag);
  138. #endif /* TBB_USE_THREADING_TOOLS*/
  139. }
  140. //! Release lock
  141. void unlock() {
  142. #if TBB_USE_THREADING_TOOLS
  143. aligned_space<scoped_lock> tmp;
  144. scoped_lock& s = *tmp.begin();
  145. s.my_mutex = this;
  146. s.internal_release();
  147. #else
  148. __TBB_UnlockByte(flag);
  149. #endif /* TBB_USE_THREADING_TOOLS */
  150. }
  151. friend class scoped_lock;
  152. }; // end of spin_mutex
  153. __TBB_DEFINE_PROFILING_SET_NAME(spin_mutex)
  154. } // namespace tbb
  155. #if ( __TBB_x86_32 || __TBB_x86_64 )
  156. #include "internal/_x86_eliding_mutex_impl.h"
  157. #endif
  158. namespace tbb {
  159. //! A cross-platform spin mutex with speculative lock acquisition.
  160. /** On platforms with proper HW support, this lock may speculatively execute
  161. its critical sections, using HW mechanisms to detect real data races and
  162. ensure atomicity of the critical sections. In particular, it uses
  163. Intel(R) Transactional Synchronization Extensions (Intel(R) TSX).
  164. Without such HW support, it behaves like a spin_mutex.
  165. It should be used for locking short critical sections where the lock is
  166. contended but the data it protects are not. If zero-initialized, the
  167. mutex is considered unheld.
  168. @ingroup synchronization */
  169. #if ( __TBB_x86_32 || __TBB_x86_64 )
  170. typedef interface7::internal::padded_mutex<interface7::internal::x86_eliding_mutex,false> speculative_spin_mutex;
  171. #else
  172. typedef interface7::internal::padded_mutex<spin_mutex,false> speculative_spin_mutex;
  173. #endif
  174. __TBB_DEFINE_PROFILING_SET_NAME(speculative_spin_mutex)
  175. } // namespace tbb
  176. #include "internal/_warning_suppress_disable_notice.h"
  177. #undef __TBB_spin_mutex_H_include_area
  178. #endif /* __TBB_spin_mutex_H */