File: conformance_task_group.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 (283 lines) | stat: -rw-r--r-- 9,323 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*
    Copyright (c) 2021-2022 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.
*/

#include "oneapi/tbb/task_group.h"
#include "oneapi/tbb/task_arena.h"
#include "common/test.h"
#include "common/utils.h"

#include "common/spin_barrier.h"

#include <type_traits>
#include "oneapi/tbb/global_control.h"


//! \file conformance_task_group.cpp
//! \brief Test for [scheduler.task_group] specification

//! Test checks that for lost task handle
//! \brief \ref requirement
TEST_CASE("Task handle created but not run"){
    {
        oneapi::tbb::task_group tg;

        //This flag is intentionally made non-atomic for Thread Sanitizer
        //to raise a flag if implementation of task_group is incorrect
        bool run {false};

        auto h = tg.defer([&]{
            run = true;
        });
        CHECK_MESSAGE(run == false, "delayed task should not be run until run(task_handle) is called");
    }
}

//! Basic test for task handle
//! \brief \ref interface \ref requirement
TEST_CASE("Task handle run"){
    oneapi::tbb::task_handle h;

    oneapi::tbb::task_group tg;
    //This flag is intentionally made non-atomic for Thread Sanitizer
    //to raise a flag if implementation of task_group is incorrect
    bool run {false};

    h = tg.defer([&]{
        run = true;
    });
    CHECK_MESSAGE(run == false, "delayed task should not be run until run(task_handle) is called");
    tg.run(std::move(h));
    tg.wait();
    CHECK_MESSAGE(run == true, "Delayed task should be completed when task_group::wait exits");

    CHECK_MESSAGE(h == nullptr, "Delayed task can be executed only once");
}

//! Basic test for task handle
//! \brief \ref interface \ref requirement
TEST_CASE("Task handle run_and_wait"){
    oneapi::tbb::task_handle h;

    oneapi::tbb::task_group tg;
    //This flag is intentionally made non-atomic for Thread Sanitizer
    //to raise a flag if implementation of task_group is incorrect
    bool run {false};

    h = tg.defer([&]{
        run = true;
    });
    CHECK_MESSAGE(run == false, "delayed task should not be run until run(task_handle) is called");
    tg.run_and_wait(std::move(h));
    CHECK_MESSAGE(run == true, "Delayed task should be completed when task_group::wait exits");

    CHECK_MESSAGE(h == nullptr, "Delayed task can be executed only once");
}

//! Test for empty check
//! \brief \ref interface
TEST_CASE("Task handle empty check"){
    oneapi::tbb::task_group tg;

    oneapi::tbb::task_handle h;

    bool empty = (h == nullptr);
    CHECK_MESSAGE(empty, "default constructed task_handle should be empty");

    h = tg.defer([]{});

    CHECK_MESSAGE(h != nullptr, "delayed task returned by task_group::delayed should not be empty");
}

//! Test for comparison operations
//! \brief \ref interface
TEST_CASE("Task handle comparison/empty checks"){
    oneapi::tbb::task_group tg;

    oneapi::tbb::task_handle h;

    bool empty =  ! static_cast<bool>(h);
    CHECK_MESSAGE(empty, "default constructed task_handle should be empty");
    CHECK_MESSAGE(h == nullptr, "default constructed task_handle should be empty");
    CHECK_MESSAGE(nullptr == h, "default constructed task_handle should be empty");

    h = tg.defer([]{});

    CHECK_MESSAGE(h != nullptr, "deferred task returned by task_group::defer() should not be empty");
    CHECK_MESSAGE(nullptr != h, "deferred task returned by task_group::defer() should not be empty");

}

//! Test for task_handle being non copyable
//! \brief \ref interface
TEST_CASE("Task handle being non copyable"){
    static_assert(
              (!std::is_copy_constructible<oneapi::tbb::task_handle>::value)
            &&(!std::is_copy_assignable<oneapi::tbb::task_handle>::value),
            "oneapi::tbb::task_handle should be non copyable");
}
//! Test that task_handle prolongs task_group::wait
//! \brief \ref requirement
TEST_CASE("Task handle blocks wait"){
    //Forbid creation of worker threads to ensure that task described by the task_handle is not run until wait is called
    oneapi::tbb::global_control s(oneapi::tbb::global_control::max_allowed_parallelism, 1);
    oneapi::tbb::task_group tg;
    //explicit task_arena is needed to prevent a deadlock,
    //as both task_group::run() and task_group::wait() should be called in the same arena
    //to guarantee execution of the task spawned by run().
    oneapi::tbb::task_arena arena;

    //This flag is intentionally made non-atomic for Thread Sanitizer
    //to raise a flag if implementation of task_group is incorrect
    bool completed  {false};
    std::atomic<bool> start_wait {false};
    std::atomic<bool> thread_started{false};

    oneapi::tbb::task_handle h = tg.defer([&]{
        completed = true;
    });

    std::thread wait_thread {[&]{
        CHECK_MESSAGE(completed == false, "Deferred task should not be run until run(task_handle) is called");

        thread_started = true;
        utils::SpinWaitUntilEq(start_wait, true);
        arena.execute([&]{
            tg.wait();
            CHECK_MESSAGE(completed == true, "Deferred task should be completed when task_group::wait exits");
        });
    }};

    utils::SpinWaitUntilEq(thread_started, true);
    CHECK_MESSAGE(completed == false, "Deferred task should not be run until run(task_handle) is called");

    arena.execute([&]{
        tg.run(std::move(h));
    });
    CHECK_MESSAGE(completed == false, "Deferred task should not be run until run(task_handle) and wait is called");
    start_wait = true;
    wait_thread.join();
}

#if TBB_USE_EXCEPTIONS
//! The test for exception handling in task_handle
//! \brief \ref requirement
TEST_CASE("Task handle exception propagation"){
    oneapi::tbb::task_group tg;

    oneapi::tbb::task_handle h = tg.defer([&]{
        volatile bool suppress_unreachable_code_warning = true;
        if (suppress_unreachable_code_warning) {
            throw std::runtime_error{ "" };
        }
    });

    tg.run(std::move(h));

    CHECK_THROWS_AS(tg.wait(), std::runtime_error);
}
#endif // TBB_USE_EXCEPTIONS

//TODO: move to some common place to share with unit tests
namespace accept_task_group_context {

struct SelfRunner {
    tbb::task_group& m_tg;
    std::atomic<unsigned>& count;
    void operator()() const {
        unsigned previous_count = count.fetch_sub(1);
        if (previous_count > 1)
            m_tg.run( *this );
    }
};

template <typename TaskGroup, typename CancelF, typename WaitF>
void run_cancellation_use_case(CancelF&& cancel, WaitF&& wait) {
    std::atomic<bool> outer_cancelled{false};
    std::atomic<unsigned> count{13};

    tbb::task_group_context inner_ctx(tbb::task_group_context::isolated);
    TaskGroup inner_tg(inner_ctx);

    tbb::task_group outer_tg;
    auto outer_tg_task = [&] {
        inner_tg.run([&] {
            utils::SpinWaitUntilEq(outer_cancelled, true);
            inner_tg.run( SelfRunner{inner_tg, count} );
        });

        utils::try_call([&] {
            std::forward<CancelF>(cancel)(outer_tg);
        }).on_completion([&] {
            outer_cancelled = true;
        });
    };

    auto check = [&] {
        tbb::task_group_status outer_status = tbb::task_group_status::not_complete;
        outer_status = std::forward<WaitF>(wait)(outer_tg);
        CHECK_MESSAGE(
            outer_status == tbb::task_group_status::canceled,
            "Outer task group should have been cancelled."
        );

        tbb::task_group_status inner_status = inner_tg.wait();
        CHECK_MESSAGE(
            inner_status == tbb::task_group_status::complete,
            "Inner task group should have completed despite the cancellation of the outer one."
        );

        CHECK_MESSAGE(0 == count, "Some of the inner group tasks were not executed.");
    };

    outer_tg.run(outer_tg_task);
    check();
}

template <typename TaskGroup>
void test() {
    run_cancellation_use_case<TaskGroup>(
        [](tbb::task_group& outer) { outer.cancel(); },
        [](tbb::task_group& outer) { return outer.wait(); }
    );

#if TBB_USE_EXCEPTIONS
    run_cancellation_use_case<TaskGroup>(
        [](tbb::task_group& /*outer*/) {
            volatile bool suppress_unreachable_code_warning = true;
            if (suppress_unreachable_code_warning) {
                throw int();
            }
        },
        [](tbb::task_group& outer) {
            try {
                outer.wait();
                return tbb::task_group_status::complete;
            } catch(const int&) {
                return tbb::task_group_status::canceled;
            }
        }
    );
#endif
}

} // namespace accept_task_group_context

//! Respect task_group_context passed from outside
//! \brief \ref interface \ref requirement
TEST_CASE("Respect task_group_context passed from outside") {
    accept_task_group_context::test<tbb::task_group>();
}