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