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