monado/tests/tests_worker.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

133 lines
2.3 KiB
C++
Raw Normal View History

2022-03-03 23:16:57 +00:00
// Copyright 2022, Collabora, Ltd.
// SPDX-License-Identifier: BSL-1.0
/*!
* @file
* @brief Thread pool tests.
* @author Ryan Pavlik <ryan.pavlik@collabora.com>
*/
#include <util/u_worker.hpp>
#include "catch/catch.hpp"
#include <thread>
#include <chrono>
using namespace std::chrono_literals;
using namespace xrt::auxiliary::util;
TEST_CASE("TaskCollection")
{
2022-11-20 12:21:13 +00:00
SharedThreadPool pool{2, 3, "Test"};
2022-03-03 23:16:57 +00:00
bool calledA[] = {
false,
false,
false,
};
bool calledB[] = {
false,
false,
false,
};
std::vector<TaskCollection::Functor> funcsA = {
[&] { calledA[0] = true; },
[&] { calledA[1] = true; },
[&] { calledA[2] = true; },
};
std::vector<TaskCollection::Functor> funcsB = {
[&] { calledB[0] = true; },
[&] { calledB[1] = true; },
[&] { calledB[2] = true; },
};
SharedThreadGroup groupA{pool};
SharedThreadGroup groupB{pool};
CHECK(!calledA[0]);
CHECK(!calledA[1]);
CHECK(!calledA[2]);
TaskCollection collectionA{groupA, funcsA};
SECTION("Sequential wait")
{
collectionA.waitAll();
CHECK(calledA[0]);
CHECK(calledA[1]);
CHECK(calledA[2]);
CHECK(!calledB[0]);
CHECK(!calledB[1]);
CHECK(!calledB[2]);
{
TaskCollection collectionB{groupB, funcsB};
}
CHECK(calledB[0]);
CHECK(calledB[1]);
CHECK(calledB[2]);
}
SECTION("Simultaneous dispatch, reversed wait")
{
CHECK(!calledB[0]);
CHECK(!calledB[1]);
CHECK(!calledB[2]);
{
TaskCollection collectionB{groupB, funcsB};
}
CHECK(calledB[0]);
CHECK(calledB[1]);
CHECK(calledB[2]);
collectionA.waitAll();
CHECK(calledA[0]);
CHECK(calledA[1]);
CHECK(calledA[2]);
}
SECTION("Simultaneous dispatch, reversed wait")
{
CHECK(!calledB[0]);
CHECK(!calledB[1]);
CHECK(!calledB[2]);
{
TaskCollection collectionB{
groupB,
{
[&] {
std::this_thread::sleep_for(500ms);
calledB[0] = true;
},
[&] {
std::this_thread::sleep_for(500ms);
calledB[1] = true;
},
[&] {
std::this_thread::sleep_for(500ms);
calledB[2] = true;
},
},
};
}
CHECK(calledB[0]);
CHECK(calledB[1]);
CHECK(calledB[2]);
collectionA.waitAll();
CHECK(calledA[0]);
CHECK(calledA[1]);
CHECK(calledA[2]);
}
}