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