mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-12-03 11:15:23 +01:00
9cc1eb48a4
Each job of worker_pool released the semaphore of every thread it wanted from the calling thread, and the threads slept again right after it. A frame of the test mode runs four or five jobs (the neighbours, the facts, the steps, the hose exchange), so the main thread made some 40 wake calls a frame: 3.3 % of its time on Tomaszewo, and on linia 107 it waited 4.8 % of its time for threads woken late. Now a thread done with a job looks out for the next one for 50 us (spin_time), as the jobs of a frame come close after one another, and only then sleeps. A job is a ticket (its number and the seats left for the threads of the pool): the threads looking out take a seat each, and sleeping ones are woken for the seats left, two by the calling thread and two more by each thread which takes a seat, so the wakes go out as a tree. A thread going to sleep sets its flag before a last look at the ticket, and a waker takes the flag before it releases, so no job is missed and no semaphore released twice. The calling thread closes the seats nobody took once it is done, then waits for the threads which took one, looking out for them for a while before it sleeps on the count. Which thread does which index never mattered for the results, so they don't change. workerpool.cpp moved into the core library, for the new unit tests (each index once with any number of threads, jobs back to back and after the threads slept, the threads of the pool taking part, an exception passed on).
138 lines
4.1 KiB
C++
138 lines
4.1 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, [&](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, [&](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, [&](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));
|
|
}
|