/* Copyright (c) 2005-2021 Intel Corporation Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ #define TBB_PREVIEW_MUTEXES 1 #include "test_mutex.h" #include #include "oneapi/tbb/mutex.h" #include #include "oneapi/tbb/rw_mutex.h" #include #include #include #include #include #include #include //! \file test_mutex.cpp //! \brief Test for [mutex.spin_mutex mutex.spin_rw_mutex mutex.queuing_mutex mutex.queuing_rw_mutexmutex.speculative_spin_mutex mutex.speculative_spin_rw_mutex] specifications // TODO: Investigate why RTM doesn't work on some macOS. // TODO: Consider adding Thread Sanitizer (note that accesses inside the transaction // considered as races by Thread Sanitizer) #if __TBB_TSX_INTRINSICS_PRESENT && !__APPLE__ && !__TBB_USE_THREAD_SANITIZER inline static bool IsInsideTx() { return _xtest() != 0; } bool have_TSX() { bool result = false; const int rtm_ebx_mask = 1 << 11; #if _MSC_VER int info[4] = { 0,0,0,0 }; const int reg_ebx = 1; __cpuidex(info, 7, 0); result = (info[reg_ebx] & rtm_ebx_mask) != 0; #elif __GNUC__ || __SUNPRO_CC int32_t reg_ebx = 0; int32_t reg_eax = 7; int32_t reg_ecx = 0; __asm__ __volatile__("movl %%ebx, %%esi\n" "cpuid\n" "movl %%ebx, %0\n" "movl %%esi, %%ebx\n" : "=a"(reg_ebx) : "0" (reg_eax), "c" (reg_ecx) : "esi", #if __TBB_x86_64 "ebx", #endif "edx" ); result = (reg_ebx & rtm_ebx_mask) != 0; #endif return result; } //! Function object for use with parallel_for.h to see if a transaction is actually attempted. std::atomic n_transactions_attempted; template struct AddOne_CheckTransaction { AddOne_CheckTransaction& operator=(const AddOne_CheckTransaction&) = delete; AddOne_CheckTransaction(const AddOne_CheckTransaction&) = default; AddOne_CheckTransaction() = default; C& counter; /** Increments counter once for each iteration in the iteration space. */ void operator()(tbb::blocked_range& range) const { for (std::size_t i = range.begin(); i != range.end(); ++i) { bool transaction_attempted = false; { typename C::mutex_type::scoped_lock lock(counter.mutex); if (IsInsideTx()) transaction_attempted = true; counter.value = counter.value + 1; } if (transaction_attempted) ++n_transactions_attempted; tbb::detail::machine_pause(static_cast(i)); } } AddOne_CheckTransaction(C& counter_) : counter(counter_) {} }; /* TestTransaction() checks if a speculative mutex actually uses transactions. */ template void TestTransaction(const char* name) { utils::Counter counter; constexpr int n = 550; n_transactions_attempted = 0; for (int i = 0; i < 5 && n_transactions_attempted.load(std::memory_order_relaxed) == 0; ++i) { counter.value = 0; tbb::parallel_for(tbb::blocked_range(0, n, 2), AddOne_CheckTransaction>(counter)); REQUIRE(counter.value == n); } REQUIRE_MESSAGE(n_transactions_attempted.load(std::memory_order_relaxed), "ERROR for " << name << ": transactions were never attempted"); } //! \brief \ref error_guessing TEST_CASE("Transaction test") { if (have_TSX()) { TestTransaction("Speculative Spin Mutex"); TestTransaction("Speculative Spin RW Mutex"); } } #endif /* __TBB_TSX_TESTING_ENABLED_FOR_THIS_COMPILER */ //! \brief \ref error_guessing TEST_CASE("test upgrade/downgrade with spin_rw_mutex") { test_rwm_upgrade_downgrade(); } //! \brief \ref error_guessing TEST_CASE("test upgrade/downgrade with queueing_rw_mutex") { test_rwm_upgrade_downgrade(); } //! \brief \ref error_guessing TEST_CASE("test upgrade/downgrade with speculative_spin_rw_mutex") { test_rwm_upgrade_downgrade(); } //! \brief \ref error_guessing TEST_CASE("test spin_mutex with native threads") { test_with_native_threads::test(); } //! \brief \ref error_guessing TEST_CASE("test queuing_mutex with native threads") { test_with_native_threads::test(); } //! \brief \ref error_guessing TEST_CASE("test spin_rw_mutex with native threads") { test_with_native_threads::test(); test_with_native_threads::test_rw(); } //! \brief \ref error_guessing TEST_CASE("test queuing_rw_mutex with native threads") { test_with_native_threads::test(); test_with_native_threads::test_rw(); } //! Test scoped_lock::is_writer getter //! \brief \ref error_guessing TEST_CASE("scoped_lock::is_writer") { TestIsWriter("spin_rw_mutex"); TestIsWriter("queuing_rw_mutex"); TestIsWriter("speculative_spin_rw_mutex"); TestIsWriter("null_rw_mutex"); TestIsWriter("rw_mutex"); } #if __TBB_CPP20_CONCEPTS_PRESENT template concept mutexes = (... && tbb::detail::scoped_lockable); template concept rw_mutexes = (... && tbb::detail::rw_scoped_lockable); //! \brief \ref error_guessing TEST_CASE("internal mutex concepts") { static_assert(mutexes); static_assert(rw_mutexes); } #endif // __TBB_CPP20_CONCEPTS_PRESENT