1 /* 2 Copyright (c) 2005-2021 Intel Corporation 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 #include "test_mutex.h" 18 19 #include <tbb/spin_mutex.h> 20 #include "oneapi/tbb/mutex.h" 21 #include <tbb/spin_rw_mutex.h> 22 #include "oneapi/tbb/rw_mutex.h" 23 #include <tbb/queuing_mutex.h> 24 #include <tbb/queuing_rw_mutex.h> 25 #include <tbb/null_mutex.h> 26 #include <tbb/null_rw_mutex.h> 27 #include <tbb/parallel_for.h> 28 #include <oneapi/tbb/detail/_utils.h> 29 #include <oneapi/tbb/detail/_machine.h> 30 31 //! \file test_mutex.cpp 32 //! \brief Test for [mutex.spin_mutex mutex.spin_rw_mutex mutex.queuing_mutex mutex.queuing_rw_mutex mutex.mutex mutex.rw_mutex mutex.speculative_spin_mutex mutex.speculative_spin_rw_mutex] specifications 33 34 // TODO: Investigate why RTM doesn't work on some macOS. 35 // TODO: Consider adding Thread Sanitizer (note that accesses inside the transaction 36 // considered as races by Thread Sanitizer) 37 #if __TBB_TSX_INTRINSICS_PRESENT && !__APPLE__ && !__TBB_USE_THREAD_SANITIZER 38 39 inline static bool IsInsideTx() { 40 return _xtest() != 0; 41 } 42 43 bool have_TSX() { 44 bool result = false; 45 const int rtm_ebx_mask = 1 << 11; 46 #if _MSC_VER 47 int info[4] = { 0,0,0,0 }; 48 const int reg_ebx = 1; 49 __cpuidex(info, 7, 0); 50 result = (info[reg_ebx] & rtm_ebx_mask) != 0; 51 #elif __GNUC__ || __SUNPRO_CC 52 int32_t reg_ebx = 0; 53 int32_t reg_eax = 7; 54 int32_t reg_ecx = 0; 55 __asm__ __volatile__("movl %%ebx, %%esi\n" 56 "cpuid\n" 57 "movl %%ebx, %0\n" 58 "movl %%esi, %%ebx\n" 59 : "=a"(reg_ebx) : "0" (reg_eax), "c" (reg_ecx) : "esi", 60 #if __TBB_x86_64 61 "ebx", 62 #endif 63 "edx" 64 ); 65 result = (reg_ebx & rtm_ebx_mask) != 0; 66 #endif 67 return result; 68 } 69 70 //! Function object for use with parallel_for.h to see if a transaction is actually attempted. 71 std::atomic<std::size_t> n_transactions_attempted; 72 template<typename C> 73 struct AddOne_CheckTransaction { 74 75 AddOne_CheckTransaction& operator=(const AddOne_CheckTransaction&) = delete; 76 AddOne_CheckTransaction(const AddOne_CheckTransaction&) = default; 77 AddOne_CheckTransaction() = default; 78 79 C& counter; 80 /** Increments counter once for each iteration in the iteration space. */ 81 void operator()(tbb::blocked_range<size_t>& range) const { 82 for (std::size_t i = range.begin(); i != range.end(); ++i) { 83 bool transaction_attempted = false; 84 { 85 typename C::mutex_type::scoped_lock lock(counter.mutex); 86 if (IsInsideTx()) transaction_attempted = true; 87 counter.value = counter.value + 1; 88 } 89 if (transaction_attempted) ++n_transactions_attempted; 90 tbb::detail::machine_pause(static_cast<int32_t>(i)); 91 } 92 } 93 AddOne_CheckTransaction(C& counter_) : counter(counter_) {} 94 }; 95 96 /* TestTransaction() checks if a speculative mutex actually uses transactions. */ 97 template<typename M> 98 void TestTransaction(const char* name) 99 { 100 utils::Counter<M> counter; 101 constexpr int n = 550; 102 103 n_transactions_attempted = 0; 104 for (int i = 0; i < 5 && n_transactions_attempted.load(std::memory_order_relaxed) == 0; ++i) { 105 counter.value = 0; 106 tbb::parallel_for(tbb::blocked_range<std::size_t>(0, n, 2), AddOne_CheckTransaction<utils::Counter<M>>(counter)); 107 REQUIRE(counter.value == n); 108 } 109 REQUIRE_MESSAGE(n_transactions_attempted.load(std::memory_order_relaxed), "ERROR for " << name << ": transactions were never attempted"); 110 } 111 112 113 //! \brief \ref error_guessing 114 TEST_CASE("Transaction test") { 115 if (have_TSX()) { 116 TestTransaction<tbb::speculative_spin_mutex>("Speculative Spin Mutex"); 117 TestTransaction<tbb::speculative_spin_rw_mutex>("Speculative Spin RW Mutex"); 118 } 119 } 120 #endif /* __TBB_TSX_TESTING_ENABLED_FOR_THIS_COMPILER */ 121 122 //! \brief \ref error_guessing 123 TEST_CASE("test upgrade/downgrade with spin_rw_mutex") { 124 test_rwm_upgrade_downgrade<tbb::spin_rw_mutex>(); 125 } 126 127 //! \brief \ref error_guessing 128 TEST_CASE("test upgrade/downgrade with queueing_rw_mutex") { 129 test_rwm_upgrade_downgrade<tbb::queuing_rw_mutex>(); 130 } 131 132 //! \brief \ref error_guessing 133 TEST_CASE("test upgrade/downgrade with rw_mutex") { 134 test_rwm_upgrade_downgrade<tbb::rw_mutex>(); 135 } 136 137 //! \brief \ref error_guessing 138 TEST_CASE("test upgrade/downgrade with speculative_spin_rw_mutex") { 139 test_rwm_upgrade_downgrade<tbb::speculative_spin_rw_mutex>(); 140 } 141 142 //! \brief \ref error_guessing 143 TEST_CASE("test spin_mutex with native threads") { 144 test_with_native_threads::test<tbb::spin_mutex>(); 145 } 146 147 //! \brief \ref error_guessing 148 TEST_CASE("test queuing_mutex with native threads") { 149 test_with_native_threads::test<tbb::queuing_mutex>(); 150 } 151 152 //! \brief \ref error_guessing 153 TEST_CASE("test mutex with native threads") { 154 test_with_native_threads::test<tbb::mutex>(); 155 } 156 157 //! \brief \ref error_guessing 158 TEST_CASE("test spin_rw_mutex with native threads") { 159 test_with_native_threads::test<tbb::spin_rw_mutex>(); 160 test_with_native_threads::test_rw<tbb::spin_rw_mutex>(); 161 } 162 163 //! \brief \ref error_guessing 164 TEST_CASE("test queuing_rw_mutex with native threads") { 165 test_with_native_threads::test<tbb::queuing_rw_mutex>(); 166 test_with_native_threads::test_rw<tbb::queuing_rw_mutex>(); 167 } 168 169 //! \brief \ref error_guessing 170 TEST_CASE("test rw_mutex with native threads") { 171 test_with_native_threads::test<tbb::rw_mutex>(); 172 test_with_native_threads::test_rw<tbb::rw_mutex>(); 173 } 174 175 //! Test scoped_lock::is_writer getter 176 //! \brief \ref error_guessing 177 TEST_CASE("scoped_lock::is_writer") { 178 TestIsWriter<oneapi::tbb::spin_rw_mutex>("spin_rw_mutex"); 179 TestIsWriter<oneapi::tbb::queuing_rw_mutex>("queuing_rw_mutex"); 180 TestIsWriter<oneapi::tbb::speculative_spin_rw_mutex>("speculative_spin_rw_mutex"); 181 TestIsWriter<oneapi::tbb::null_rw_mutex>("null_rw_mutex"); 182 TestIsWriter<oneapi::tbb::rw_mutex>("rw_mutex"); 183 } 184 185 #if __TBB_CPP20_CONCEPTS_PRESENT 186 template <typename... Args> 187 concept mutexes = (... && tbb::detail::scoped_lockable<Args>); 188 189 template <typename... Args> 190 concept rw_mutexes = (... && tbb::detail::rw_scoped_lockable<Args>); 191 192 //! \brief \ref error_guessing 193 TEST_CASE("internal mutex concepts") { 194 static_assert(mutexes<tbb::spin_mutex, oneapi::tbb::mutex, tbb::speculative_spin_mutex, tbb::null_mutex, tbb::queuing_mutex, 195 tbb::spin_rw_mutex, oneapi::tbb::rw_mutex, tbb::speculative_spin_rw_mutex, tbb::null_rw_mutex, tbb::queuing_rw_mutex>); 196 static_assert(rw_mutexes<tbb::spin_rw_mutex, oneapi::tbb::rw_mutex, tbb::speculative_spin_rw_mutex, 197 tbb::null_rw_mutex, tbb::queuing_rw_mutex>); 198 } 199 #endif // __TBB_CPP20_CONCEPTS_PRESENT 200