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