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.

192 lines
6.1 KiB

  1. /*
  2. Copyright 2005-2013 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. namespace tbb {
  32. //! A lock that occupies a single byte.
  33. /** A spin_mutex is a spin mutex that fits in a single byte.
  34. It should be used only for locking short critical sections
  35. (typically less than 20 instructions) when fairness is not an issue.
  36. If zero-initialized, the mutex is considered unheld.
  37. @ingroup synchronization */
  38. class spin_mutex {
  39. //! 0 if lock is released, 1 if lock is acquired.
  40. __TBB_atomic_flag flag;
  41. public:
  42. //! Construct unacquired lock.
  43. /** Equivalent to zero-initialization of *this. */
  44. spin_mutex() : flag(0) {
  45. #if TBB_USE_THREADING_TOOLS
  46. internal_construct();
  47. #endif
  48. }
  49. //! Represents acquisition of a mutex.
  50. class scoped_lock : internal::no_copy {
  51. private:
  52. //! Points to currently held mutex, or NULL if no lock is held.
  53. spin_mutex* my_mutex;
  54. //! Value to store into spin_mutex::flag to unlock the mutex.
  55. /** This variable is no longer used. Instead, 0 and 1 are used to
  56. represent that the lock is free and acquired, respectively.
  57. We keep the member variable here to ensure backward compatibility */
  58. __TBB_Flag my_unlock_value;
  59. //! Like acquire, but with ITT instrumentation.
  60. void __TBB_EXPORTED_METHOD internal_acquire( spin_mutex& m );
  61. //! Like try_acquire, but with ITT instrumentation.
  62. bool __TBB_EXPORTED_METHOD internal_try_acquire( spin_mutex& m );
  63. //! Like release, but with ITT instrumentation.
  64. void __TBB_EXPORTED_METHOD internal_release();
  65. friend class spin_mutex;
  66. public:
  67. //! Construct without acquiring a mutex.
  68. scoped_lock() : my_mutex(NULL), my_unlock_value(0) {}
  69. //! Construct and acquire lock on a mutex.
  70. scoped_lock( spin_mutex& m ) : my_unlock_value(0) {
  71. #if TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT
  72. my_mutex=NULL;
  73. internal_acquire(m);
  74. #else
  75. __TBB_LockByte(m.flag);
  76. my_mutex=&m;
  77. #endif /* TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT*/
  78. }
  79. //! Acquire lock.
  80. void acquire( spin_mutex& m ) {
  81. #if TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT
  82. internal_acquire(m);
  83. #else
  84. __TBB_LockByte(m.flag);
  85. my_mutex = &m;
  86. #endif /* TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT*/
  87. }
  88. //! Try acquiring lock (non-blocking)
  89. /** Return true if lock acquired; false otherwise. */
  90. bool try_acquire( spin_mutex& m ) {
  91. #if TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT
  92. return internal_try_acquire(m);
  93. #else
  94. bool result = __TBB_TryLockByte(m.flag);
  95. if( result )
  96. my_mutex = &m;
  97. return result;
  98. #endif /* TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT*/
  99. }
  100. //! Release lock
  101. void release() {
  102. #if TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT
  103. internal_release();
  104. #else
  105. __TBB_UnlockByte(my_mutex->flag, 0);
  106. my_mutex = NULL;
  107. #endif /* TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT */
  108. }
  109. //! Destroy lock. If holding a lock, releases the lock first.
  110. ~scoped_lock() {
  111. if( my_mutex ) {
  112. #if TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT
  113. internal_release();
  114. #else
  115. __TBB_UnlockByte(my_mutex->flag, 0);
  116. #endif /* TBB_USE_THREADING_TOOLS||TBB_USE_ASSERT */
  117. }
  118. }
  119. };
  120. void __TBB_EXPORTED_METHOD internal_construct();
  121. // Mutex traits
  122. static const bool is_rw_mutex = false;
  123. static const bool is_recursive_mutex = false;
  124. static const bool is_fair_mutex = false;
  125. // ISO C++0x compatibility methods
  126. //! Acquire lock
  127. void lock() {
  128. #if TBB_USE_THREADING_TOOLS
  129. aligned_space<scoped_lock,1> tmp;
  130. new(tmp.begin()) scoped_lock(*this);
  131. #else
  132. __TBB_LockByte(flag);
  133. #endif /* TBB_USE_THREADING_TOOLS*/
  134. }
  135. //! Try acquiring lock (non-blocking)
  136. /** Return true if lock acquired; false otherwise. */
  137. bool try_lock() {
  138. #if TBB_USE_THREADING_TOOLS
  139. aligned_space<scoped_lock,1> tmp;
  140. return (new(tmp.begin()) scoped_lock)->internal_try_acquire(*this);
  141. #else
  142. return __TBB_TryLockByte(flag);
  143. #endif /* TBB_USE_THREADING_TOOLS*/
  144. }
  145. //! Release lock
  146. void unlock() {
  147. #if TBB_USE_THREADING_TOOLS
  148. aligned_space<scoped_lock,1> tmp;
  149. scoped_lock& s = *tmp.begin();
  150. s.my_mutex = this;
  151. s.internal_release();
  152. #else
  153. __TBB_store_with_release(flag, 0);
  154. #endif /* TBB_USE_THREADING_TOOLS */
  155. }
  156. friend class scoped_lock;
  157. };
  158. __TBB_DEFINE_PROFILING_SET_NAME(spin_mutex)
  159. } // namespace tbb
  160. #endif /* __TBB_spin_mutex_H */