Skip to content

Commit a7ecf37

Browse files
authored
Improve task_group::wait_for_task testing (#2001)
1 parent 40a0c42 commit a7ecf37

File tree

1 file changed

+53
-3
lines changed

1 file changed

+53
-3
lines changed

test/tbb/test_task_group.cpp

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2160,10 +2160,9 @@ enum class wait_for_function_tag {
21602160

21612161
tbb::task_group_status wait_for(tbb::task_completion_handle& task, tbb::task_group& tg, tbb::task_arena& arena, wait_for_function_tag tag) {
21622162
tbb::task_group_status status = tbb::task_group_status::not_complete;
2163-
if (tag == wait_for_function_tag::group_wait_task) {
2163+
if (tag == wait_for_function_tag::group_wait_task || tag == wait_for_function_tag::group_run_and_wait_task) {
21642164
status = tg.wait_for_task(task);
21652165
} else {
2166-
CHECK_MESSAGE(tag != wait_for_function_tag::group_run_and_wait_task, "Unsupported wait_for_function_tag");
21672166
if (tag == wait_for_function_tag::arena_wait_for) {
21682167
status = arena.wait_for(task);
21692168
}
@@ -2215,6 +2214,8 @@ void test_single_task_wait_with_no_dependencies(bool do_cancellation, wait_for_f
22152214
if (do_cancellation) tg.cancel();
22162215
});
22172216

2217+
tbb::task_completion_handle first_block_comp_handle = first_block_task;
2218+
22182219
tbb::task_group_status task_status = run_and_wait_for(std::move(first_block_task), tg, arena, tag);
22192220

22202221
tbb::task_group_status expected_task_status = tbb::task_group_status::not_complete;
@@ -2234,6 +2235,13 @@ void test_single_task_wait_with_no_dependencies(bool do_cancellation, wait_for_f
22342235
CHECK_MESSAGE(items[i] == expected_item, "Incorrect item processing result");
22352236
}
22362237

2238+
// Test that waiting on the completed task returns the same status
2239+
for (std::size_t i = 0; i < 10; ++i) {
2240+
CHECK_MESSAGE(expected_task_status == wait_for(first_block_comp_handle, tg, arena, tag),
2241+
"Incorrect task status returned");
2242+
}
2243+
2244+
22372245
tbb::task_group_status group_status = arena.wait_for(tg);
22382246
CHECK_MESSAGE(group_status == expected_group_status, "Incorrect group status returned");
22392247

@@ -2401,12 +2409,53 @@ void test_single_task_wait_transferring(bool do_cancellation, wait_for_function_
24012409
}
24022410
}
24032411

2412+
void test_concurrent_single_task_wait(bool do_cancellation, wait_for_function_tag tag) {
2413+
tbb::task_group tg;
2414+
tbb::task_arena arena;
2415+
2416+
tbb::task_handle task;
2417+
tbb::task_completion_handle comp_handle;
2418+
2419+
arena.execute([&] {
2420+
task = tg.defer([&] {
2421+
CHECK_MESSAGE(!do_cancellation, "Task should not be called in case of group cancellation");
2422+
});
2423+
comp_handle = task;
2424+
});
2425+
2426+
tbb::task_group_status expected_status = do_cancellation ? tbb::task_group_status::canceled
2427+
: tbb::task_group_status::task_complete;
2428+
2429+
int num_threads = tbb::this_task_arena::max_concurrency();
2430+
utils::SpinBarrier barrier(num_threads);
2431+
2432+
utils::NativeParallelFor(num_threads,
2433+
[&](std::size_t index) {
2434+
tbb::task_group_status status;
2435+
barrier.wait();
2436+
if (index == 0) {
2437+
// Short sleep to increase the number of threads entered the wait_for function
2438+
utils::Sleep(10);
2439+
2440+
if (do_cancellation) {
2441+
tg.cancel();
2442+
}
2443+
2444+
status = run_and_wait_for(std::move(task), tg, arena, tag);
2445+
} else {
2446+
status = wait_for(comp_handle, tg, arena, tag);
2447+
}
2448+
CHECK_MESSAGE(status == expected_status, "Incorrect status returned");
2449+
});
2450+
}
2451+
24042452
void test_single_task_wait_base(bool cancellation, wait_for_function_tag tag) {
24052453
for (unsigned num_threads = MinThread; num_threads <= MaxThread; ++num_threads) {
24062454
tbb::global_control ctl(tbb::global_control::max_allowed_parallelism, num_threads);
24072455
test_single_task_wait_with_no_dependencies(cancellation, tag);
24082456
test_single_task_wait_with_dependencies(cancellation, tag);
24092457
test_single_task_wait_transferring(cancellation, tag);
2458+
test_concurrent_single_task_wait(cancellation, tag);
24102459
}
24112460
}
24122461

@@ -2482,7 +2531,7 @@ void test_get_status_of() {
24822531
CHECK_MESSAGE(tg_ptr->get_status_of(comp_handle) == tbb::task_group_status::not_complete,
24832532
"Incorrect task_group_status returned");
24842533
++placeholder;
2485-
};
2534+
};
24862535

24872536
{
24882537
tbb::task_group tg;
@@ -2529,6 +2578,7 @@ TEST_CASE("test single task wait") {
25292578
test_single_task_wait(/*cancel = */true);
25302579
}
25312580

2581+
//! \brief \ref interface \ref requirement
25322582
TEST_CASE("test task_group::get_status_of") {
25332583
test_get_status_of();
25342584
}

0 commit comments

Comments
 (0)