File: test_concurrent_monitor.cpp

package info (click to toggle)
onetbb 2022.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 19,440 kB
  • sloc: cpp: 129,228; ansic: 9,745; python: 808; xml: 183; objc: 176; makefile: 66; sh: 66; awk: 41; javascript: 37
file content (101 lines) | stat: -rw-r--r-- 3,043 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
    Copyright (c) 2020-2021 Intel Corporation

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
*/

#if _WIN32 || _WIN64
#define _CRT_SECURE_NO_WARNINGS
#endif

#if _MSC_VER && !defined(__INTEL_COMPILER)
// structure was padded due to alignment specifier
#pragma warning( disable: 4324 )
#endif

#include "common/test.h"
#include "common/utils.h"
#include "common/utils_concurrency_limit.h"
#include "common/spin_barrier.h"

#include "tbb/global_control.h"
#include "tbb/task_arena.h"
#include "../../src/tbb/concurrent_monitor.h"
#include "../../src/tbb/misc.cpp"

//! \file test_concurrent_monitor.cpp
//! \brief Test for [internal] functionality

#if TBB_USE_EXCEPTIONS
//! \brief \ref error_guessing
TEST_CASE("Stress test") {
    enum class notification_types {
        notify,
        notify_one,
        notify_all,
        notify_number
    };

    std::size_t threads_number = utils::get_platform_max_threads();

    // Need to prolong lifetime of the exposed concurrent_monitor
    tbb::task_scheduler_handle handler{tbb::attach{}};

    utils::SpinBarrier barrier(threads_number);

    tbb::detail::r1::concurrent_monitor test_monitor;
    {
        tbb::task_arena arena(static_cast<int>(threads_number - 1), 0);


        std::size_t iter_on_operation = 1000;
        std::size_t operation_number = std::size_t(notification_types::notify_number) * iter_on_operation;

        auto thread_func = [&, operation_number] {
            for (std::size_t i = 0; i < operation_number; ++i) {
                tbb::detail::r1::concurrent_monitor::thread_context context{std::uintptr_t(1)};
                test_monitor.prepare_wait(context);
                barrier.wait();
                test_monitor.cancel_wait(context);
            }
        };

        for (std::size_t i = 0; i < threads_number - 1; ++i) {
            arena.enqueue(thread_func);
        }

        for (std::size_t i = 0; i < operation_number; ++i) {
            barrier.wait();
            switch (i / iter_on_operation) {
                case 0:
                {
                    test_monitor.notify([] ( std::uintptr_t ) { return true; });
                    break;
                }
                case 1:
                {
                    test_monitor.notify_one();
                    break;
                }
                case 2:
                {
                    test_monitor.notify_all();
                    break;
                }
            };
        }
    }

    tbb::finalize(handler);
}
#endif // TBB_USE_EXCEPTIONS