1 /* 2 Copyright (c) 2020-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 #if _WIN32 || _WIN64 18 #define _CRT_SECURE_NO_WARNINGS 19 #endif 20 21 #if _MSC_VER && !defined(__INTEL_COMPILER) 22 // structure was padded due to alignment specifier 23 #pragma warning( disable: 4324 ) 24 #endif 25 26 #include "common/test.h" 27 #include "common/utils.h" 28 #include "common/utils_concurrency_limit.h" 29 #include "common/spin_barrier.h" 30 31 #include "tbb/global_control.h" 32 #include "tbb/task_arena.h" 33 #include "../../src/tbb/concurrent_monitor.h" 34 #include "../../src/tbb/misc.cpp" 35 36 //! \file test_concurrent_monitor.cpp 37 //! \brief Test for [internal] functionality 38 39 #if TBB_USE_EXCEPTIONS 40 //! \brief \ref error_guessing 41 TEST_CASE("Stress test") { 42 enum class notification_types { 43 notify, 44 notify_one, 45 notify_all, 46 notify_number 47 }; 48 49 std::size_t threads_number = utils::get_platform_max_threads(); 50 51 // Need to prolong lifetime of the exposed concurrent_monitor 52 tbb::task_scheduler_handle handler{tbb::attach{}}; 53 54 utils::SpinBarrier barrier(threads_number); 55 56 tbb::detail::r1::concurrent_monitor test_monitor; 57 { 58 tbb::task_arena arena(static_cast<int>(threads_number - 1), 0); 59 60 61 std::size_t iter_on_operation = 1000; 62 std::size_t operation_number = std::size_t(notification_types::notify_number) * iter_on_operation; 63 64 auto thread_func = [&, operation_number] { 65 for (std::size_t i = 0; i < operation_number; ++i) { 66 tbb::detail::r1::concurrent_monitor::thread_context context{std::uintptr_t(1)}; 67 test_monitor.prepare_wait(context); 68 barrier.wait(); 69 test_monitor.cancel_wait(context); 70 } 71 }; 72 73 for (std::size_t i = 0; i < threads_number - 1; ++i) { 74 arena.enqueue(thread_func); 75 } 76 77 for (std::size_t i = 0; i < operation_number; ++i) { 78 barrier.wait(); 79 switch (i / iter_on_operation) { 80 case 0: 81 { 82 test_monitor.notify([] ( std::uintptr_t ) { return true; }); 83 break; 84 } 85 case 1: 86 { 87 test_monitor.notify_one(); 88 break; 89 } 90 case 2: 91 { 92 test_monitor.notify_all(); 93 break; 94 } 95 }; 96 } 97 } 98 99 tbb::finalize(handler); 100 } 101 #endif // TBB_USE_EXCEPTIONS 102