_x86_eliding_mutex_impl.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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__x86_eliding_mutex_impl_H
  14. #define __TBB__x86_eliding_mutex_impl_H
  15. #ifndef __TBB_spin_mutex_H
  16. #error Do not #include this internal file directly; use public TBB headers instead.
  17. #endif
  18. #if ( __TBB_x86_32 || __TBB_x86_64 )
  19. namespace tbb {
  20. namespace interface7 {
  21. namespace internal {
  22. template<typename Mutex, bool is_rw>
  23. class padded_mutex;
  24. //! An eliding lock that occupies a single byte.
  25. /** A x86_eliding_mutex is an HLE-enabled spin mutex. It is recommended to
  26. put the mutex on a cache line that is not shared by the data it protects.
  27. It should be used for locking short critical sections where the lock is
  28. contended but the data it protects are not. If zero-initialized, the
  29. mutex is considered unheld.
  30. @ingroup synchronization */
  31. class x86_eliding_mutex : tbb::internal::mutex_copy_deprecated_and_disabled {
  32. //! 0 if lock is released, 1 if lock is acquired.
  33. __TBB_atomic_flag flag;
  34. friend class padded_mutex<x86_eliding_mutex, false>;
  35. public:
  36. //! Construct unacquired lock.
  37. /** Equivalent to zero-initialization of *this. */
  38. x86_eliding_mutex() : flag(0) {}
  39. // bug in gcc 3.x.x causes syntax error in spite of the friend declaration above.
  40. // Make the scoped_lock public in that case.
  41. #if __TBB_USE_X86_ELIDING_MUTEX || __TBB_GCC_VERSION < 40000
  42. #else
  43. // by default we will not provide the scoped_lock interface. The user
  44. // should use the padded version of the mutex. scoped_lock is used in
  45. // padded_mutex template.
  46. private:
  47. #endif
  48. // scoped_lock in padded_mutex<> is the interface to use.
  49. //! Represents acquisition of a mutex.
  50. class scoped_lock : tbb::internal::no_copy {
  51. private:
  52. //! Points to currently held mutex, or NULL if no lock is held.
  53. x86_eliding_mutex* my_mutex;
  54. public:
  55. //! Construct without acquiring a mutex.
  56. scoped_lock() : my_mutex(NULL) {}
  57. //! Construct and acquire lock on a mutex.
  58. scoped_lock( x86_eliding_mutex& m ) : my_mutex(NULL) { acquire(m); }
  59. //! Acquire lock.
  60. void acquire( x86_eliding_mutex& m ) {
  61. __TBB_ASSERT( !my_mutex, "already holding a lock" );
  62. my_mutex=&m;
  63. my_mutex->lock();
  64. }
  65. //! Try acquiring lock (non-blocking)
  66. /** Return true if lock acquired; false otherwise. */
  67. bool try_acquire( x86_eliding_mutex& m ) {
  68. __TBB_ASSERT( !my_mutex, "already holding a lock" );
  69. bool result = m.try_lock();
  70. if( result ) {
  71. my_mutex = &m;
  72. }
  73. return result;
  74. }
  75. //! Release lock
  76. void release() {
  77. __TBB_ASSERT( my_mutex, "release on scoped_lock that is not holding a lock" );
  78. my_mutex->unlock();
  79. my_mutex = NULL;
  80. }
  81. //! Destroy lock. If holding a lock, releases the lock first.
  82. ~scoped_lock() {
  83. if( my_mutex ) {
  84. release();
  85. }
  86. }
  87. };
  88. #if __TBB_USE_X86_ELIDING_MUTEX || __TBB_GCC_VERSION < 40000
  89. #else
  90. public:
  91. #endif /* __TBB_USE_X86_ELIDING_MUTEX */
  92. // Mutex traits
  93. static const bool is_rw_mutex = false;
  94. static const bool is_recursive_mutex = false;
  95. static const bool is_fair_mutex = false;
  96. // ISO C++0x compatibility methods
  97. //! Acquire lock
  98. void lock() {
  99. __TBB_LockByteElided(flag);
  100. }
  101. //! Try acquiring lock (non-blocking)
  102. /** Return true if lock acquired; false otherwise. */
  103. bool try_lock() {
  104. return __TBB_TryLockByteElided(flag);
  105. }
  106. //! Release lock
  107. void unlock() {
  108. __TBB_UnlockByteElided( flag );
  109. }
  110. }; // end of x86_eliding_mutex
  111. } // namespace internal
  112. } // namespace interface7
  113. } // namespace tbb
  114. #endif /* ( __TBB_x86_32 || __TBB_x86_64 ) */
  115. #endif /* __TBB__x86_eliding_mutex_impl_H */