You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

220 lines
7.3 KiB

  1. /*
  2. Copyright 2005-2014 Intel Corporation. All Rights Reserved.
  3. This file is part of Threading Building Blocks.
  4. Threading Building Blocks is free software; you can redistribute it
  5. and/or modify it under the terms of the GNU General Public License
  6. version 2 as published by the Free Software Foundation.
  7. Threading Building Blocks is distributed in the hope that it will be
  8. useful, but WITHOUT ANY WARRANTY; without even the implied warranty
  9. of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with Threading Building Blocks; if not, write to the Free Software
  13. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  14. As a special exception, you may use this file as part of a free software
  15. library without restriction. Specifically, if other files instantiate
  16. templates or use macros or inline functions from this file, or you compile
  17. this file and link it with other files to produce an executable, this
  18. file does not by itself cause the resulting executable to be covered by
  19. the GNU General Public License. This exception does not however
  20. invalidate any other reasons why the executable file might be covered by
  21. the GNU General Public License.
  22. */
  23. #ifndef __TBB_spin_mutex_H
  24. #define __TBB_spin_mutex_H
  25. #include <cstddef>
  26. #include <new>
  27. #include "aligned_space.h"
  28. #include "tbb_stddef.h"
  29. #include "tbb_machine.h"
  30. #include "tbb_profiling.h"
  31. #include "internal/_mutex_padding.h"
  32. namespace tbb {
  33. //! A lock that occupies a single byte.
  34. /** A spin_mutex is a spin mutex that fits in a single byte.
  35. It should be used only for locking short critical sections
  36. (typically less than 20 instructions) when fairness is not an issue.
  37. If zero-initialized, the mutex is considered unheld.
  38. @ingroup synchronization */
  39. class spin_mutex {
  40. //! 0 if lock is released, 1 if lock is acquired.
  41. __TBB_atomic_flag flag;
  42. public:
  43. //! Construct unacquired lock.
  44. /** Equivalent to zero-initialization of *this. */
  45. spin_mutex() : flag(0) {
  46. #if TBB_USE_THREADING_TOOLS
  47. internal_construct();
  48. #endif
  49. }
  50. //! Represents acquisition of a mutex.
  51. class scoped_lock : internal::no_copy {
  52. private:
  53. //! Points to currently held mutex, or NULL if no lock is held.
  54. spin_mutex* my_mutex;
  55. //! Value to store into spin_mutex::flag to unlock the mutex.
  56. /** This variable is no longer used. Instead, 0 and 1 are used to
  57. represent that the lock is free and acquired, respectively.
  58. We keep the member variable here to ensure backward compatibility */
  59. __TBB_Flag my_unlock_value;
  60. //! Like acquire, but with ITT instrumentation.
  61. void __TBB_EXPORTED_METHOD internal_acquire( spin_mutex& m );
  62. //! Like try_acquire, but with ITT instrumentation.
  63. bool __TBB_EXPORTED_METHOD internal_try_acquire( spin_mutex& m );
  64. //! Like release, but with ITT instrumentation.
  65. void __TBB_EXPORTED_METHOD internal_release();
  66. friend class spin_mutex;
  67. public:
  68. //! Construct without acquiring a mutex.
  69. scoped_lock() : my_mutex(NULL), my_unlock_value(0) {}
  70. //! Construct and acquire lock on a mutex.
  71. scoped_lock( spin_mutex& m ) : my_unlock_value(0) {
  72. internal::suppress_unused_warning(my_unlock_value);
  73. #if TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT
  74. my_mutex=NULL;
  75. internal_acquire(m);
  76. #else
  77. my_mutex=&m;
  78. __TBB_LockByte(m.flag);
  79. #endif /* TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT*/
  80. }
  81. //! Acquire lock.
  82. void acquire( spin_mutex& m ) {
  83. #if TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT
  84. internal_acquire(m);
  85. #else
  86. my_mutex = &m;
  87. __TBB_LockByte(m.flag);
  88. #endif /* TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT*/
  89. }
  90. //! Try acquiring lock (non-blocking)
  91. /** Return true if lock acquired; false otherwise. */
  92. bool try_acquire( spin_mutex& m ) {
  93. #if TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT
  94. return internal_try_acquire(m);
  95. #else
  96. bool result = __TBB_TryLockByte(m.flag);
  97. if( result )
  98. my_mutex = &m;
  99. return result;
  100. #endif /* TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT*/
  101. }
  102. //! Release lock
  103. void release() {
  104. #if TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT
  105. internal_release();
  106. #else
  107. __TBB_UnlockByte(my_mutex->flag);
  108. my_mutex = NULL;
  109. #endif /* TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT */
  110. }
  111. //! Destroy lock. If holding a lock, releases the lock first.
  112. ~scoped_lock() {
  113. if( my_mutex ) {
  114. #if TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT
  115. internal_release();
  116. #else
  117. __TBB_UnlockByte(my_mutex->flag);
  118. #endif /* TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT */
  119. }
  120. }
  121. };
  122. //! Internal constructor with ITT instrumentation.
  123. void __TBB_EXPORTED_METHOD internal_construct();
  124. // Mutex traits
  125. static const bool is_rw_mutex = false;
  126. static const bool is_recursive_mutex = false;
  127. static const bool is_fair_mutex = false;
  128. // ISO C++0x compatibility methods
  129. //! Acquire lock
  130. void lock() {
  131. #if TBB_USE_THREADING_TOOLS
  132. aligned_space<scoped_lock,1> tmp;
  133. new(tmp.begin()) scoped_lock(*this);
  134. #else
  135. __TBB_LockByte(flag);
  136. #endif /* TBB_USE_THREADING_TOOLS*/
  137. }
  138. //! Try acquiring lock (non-blocking)
  139. /** Return true if lock acquired; false otherwise. */
  140. bool try_lock() {
  141. #if TBB_USE_THREADING_TOOLS
  142. aligned_space<scoped_lock,1> tmp;
  143. return (new(tmp.begin()) scoped_lock)->internal_try_acquire(*this);
  144. #else
  145. return __TBB_TryLockByte(flag);
  146. #endif /* TBB_USE_THREADING_TOOLS*/
  147. }
  148. //! Release lock
  149. void unlock() {
  150. #if TBB_USE_THREADING_TOOLS
  151. aligned_space<scoped_lock,1> tmp;
  152. scoped_lock& s = *tmp.begin();
  153. s.my_mutex = this;
  154. s.internal_release();
  155. #else
  156. __TBB_store_with_release(flag, 0);
  157. #endif /* TBB_USE_THREADING_TOOLS */
  158. }
  159. friend class scoped_lock;
  160. }; // end of spin_mutex
  161. __TBB_DEFINE_PROFILING_SET_NAME(spin_mutex)
  162. } // namespace tbb
  163. #if ( __TBB_x86_32 || __TBB_x86_64 )
  164. #include "internal/_x86_eliding_mutex_impl.h"
  165. #endif
  166. namespace tbb {
  167. //! A cross-platform spin mutex with speculative lock acquisition.
  168. /** On platforms with proper HW support, this lock may speculatively execute
  169. its critical sections, using HW mechanisms to detect real data races and
  170. ensure atomicity of the critical sections. In particular, it uses
  171. Intel(R) Transactional Synchronization Extensions (Intel(R) TSX).
  172. Without such HW support, it behaves like a spin_mutex.
  173. It should be used for locking short critical sections where the lock is
  174. contended but the data it protects are not. If zero-initialized, the
  175. mutex is considered unheld.
  176. @ingroup synchronization */
  177. #if ( __TBB_x86_32 || __TBB_x86_64 )
  178. typedef interface7::internal::padded_mutex<interface7::internal::x86_eliding_mutex,false> speculative_spin_mutex;
  179. #else
  180. typedef interface7::internal::padded_mutex<spin_mutex,false> speculative_spin_mutex;
  181. #endif
  182. __TBB_DEFINE_PROFILING_SET_NAME(speculative_spin_mutex)
  183. } // namespace tbb
  184. #endif /* __TBB_spin_mutex_H */