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