mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-12-03 11:15:23 +01:00
4569bff42c
The memory orders of the worker pool and of the settings pointer stay as they are; each line says why its order is enough. The settings pointer is set once, so it can't be const. The pool threads are std::jthread, which the destructor of the pool joins as before. wake() and next_job() are const, the test lambdas capture what they use, and the length of a vehicle found near another one comes from a helper, which keeps update_neighbours() under the cognitive complexity limit.
138 lines
4.2 KiB
C++
138 lines
4.2 KiB
C++
/*
|
|
This Source Code Form is subject to the
|
|
terms of the Mozilla Public License, v.
|
|
2.0. If a copy of the MPL was not
|
|
distributed with this file, You can
|
|
obtain one at
|
|
http://mozilla.org/MPL/2.0/.
|
|
*/
|
|
|
|
// the pool of threads the vehicle physics splits its work over (worker_pool): each index of a job done once, whichever
|
|
// threads take part, jobs close after one another and after the threads went to sleep, and an exception passed on
|
|
|
|
#include "stdafx.h"
|
|
#include "utilities/workerpool.h"
|
|
#include "utilities/Globals.h"
|
|
|
|
#include "doctest.h"
|
|
|
|
#include <algorithm>
|
|
#include <atomic>
|
|
#include <numeric>
|
|
#include <stdexcept>
|
|
#include <thread>
|
|
#include <vector>
|
|
|
|
namespace
|
|
{
|
|
|
|
// Global.performance_multithreading on for the test, as it was after it
|
|
struct multithreading_on
|
|
{
|
|
bool const was{Global.performance_multithreading};
|
|
multithreading_on() { Global.performance_multithreading = true; }
|
|
~multithreading_on() { Global.performance_multithreading = was; }
|
|
multithreading_on(multithreading_on const &) = delete;
|
|
multithreading_on &operator=(multithreading_on const &) = delete;
|
|
};
|
|
|
|
// runs a job on the pool and tells whether each of its indices was done exactly once
|
|
bool each_once(worker_pool &Pool, std::size_t const Count, std::size_t const Threads)
|
|
{
|
|
std::vector<std::atomic<int>> calls(Count);
|
|
Pool.run(Count, Threads, [&calls](std::size_t const Index) { calls[Index].fetch_add(1, std::memory_order_relaxed); });
|
|
return std::ranges::all_of(calls, [](std::atomic<int> const &Calls) { return Calls.load() == 1; });
|
|
}
|
|
|
|
} // namespace
|
|
|
|
TEST_CASE("worker pool: each index of a job is done once, by any number of threads")
|
|
{
|
|
multithreading_on const multithreading;
|
|
worker_pool pool;
|
|
auto const cores{worker_pool::capacity()};
|
|
for (std::size_t const threads : {std::size_t{1}, std::size_t{2}, std::size_t{3}, cores, cores + 5})
|
|
{
|
|
for (std::size_t const count : {std::size_t{0}, std::size_t{1}, std::size_t{2}, std::size_t{7}, std::size_t{64}, std::size_t{1000}})
|
|
{
|
|
CAPTURE(threads);
|
|
CAPTURE(count);
|
|
CHECK(each_once(pool, count, threads));
|
|
}
|
|
}
|
|
CHECK(pool.size() <= cores);
|
|
}
|
|
|
|
TEST_CASE("worker pool: jobs right after one another, and after the threads went to sleep")
|
|
{
|
|
multithreading_on const multithreading;
|
|
worker_pool pool;
|
|
auto const threads{worker_pool::capacity()};
|
|
// back to back, as the jobs of a frame come: the threads still look out for the next one
|
|
auto failures{0};
|
|
for (int job = 0; job < 2000; ++job)
|
|
{
|
|
if (false == each_once(pool, 1 + job % 97, 2 + job % threads))
|
|
{
|
|
++failures;
|
|
}
|
|
}
|
|
CHECK(failures == 0);
|
|
// with pauses longer than the threads look out for a job: they sleep, and are woken
|
|
for (int job = 0; job < 20; ++job)
|
|
{
|
|
std::this_thread::sleep_for(worker_pool::spin_time * 4);
|
|
CHECK(each_once(pool, 500, threads));
|
|
}
|
|
}
|
|
|
|
TEST_CASE("worker pool: the threads of the pool take part in the work")
|
|
{
|
|
multithreading_on const multithreading;
|
|
if (worker_pool::capacity() < 2)
|
|
{
|
|
return;
|
|
}
|
|
worker_pool pool;
|
|
// a job of two calls which wait for each other: it ends only if a thread of the pool takes one of them
|
|
std::atomic<int> arrived{0};
|
|
pool.run(2, 2, [&arrived](std::size_t) {
|
|
arrived.fetch_add(1);
|
|
while (arrived.load() < 2)
|
|
{
|
|
std::this_thread::yield();
|
|
}
|
|
});
|
|
CHECK(arrived.load() == 2);
|
|
// and again once it went to sleep
|
|
std::this_thread::sleep_for(worker_pool::spin_time * 4);
|
|
arrived = 0;
|
|
pool.run(2, 2, [&arrived](std::size_t) {
|
|
arrived.fetch_add(1);
|
|
while (arrived.load() < 2)
|
|
{
|
|
std::this_thread::yield();
|
|
}
|
|
});
|
|
CHECK(arrived.load() == 2);
|
|
}
|
|
|
|
TEST_CASE("worker pool: an exception of the work is passed on once the other calls are done, and the pool works on")
|
|
{
|
|
multithreading_on const multithreading;
|
|
worker_pool pool;
|
|
auto const threads{worker_pool::capacity()};
|
|
std::atomic<int> calls{0};
|
|
CHECK_THROWS_AS(pool.run(100, threads,
|
|
[&](std::size_t const Index) {
|
|
calls.fetch_add(1);
|
|
if (Index == 10)
|
|
{
|
|
throw std::runtime_error("work failed");
|
|
}
|
|
}),
|
|
std::runtime_error);
|
|
CHECK(calls.load() <= 100);
|
|
CHECK(each_once(pool, 300, threads));
|
|
}
|