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).
248 lines
6.2 KiB
C++
248 lines
6.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/.
|
|
*/
|
|
|
|
#include "stdafx.h"
|
|
#include "utilities/workerpool.h"
|
|
|
|
#include "utilities/Globals.h"
|
|
#include "utilities/utilities.h"
|
|
|
|
#include <random>
|
|
#if defined(_M_X64) || defined(_M_IX86) || defined(__x86_64__) || defined(__i386__)
|
|
#include <immintrin.h>
|
|
#define EU07_WORKER_PAUSE() _mm_pause()
|
|
#else
|
|
#define EU07_WORKER_PAUSE() std::this_thread::yield()
|
|
#endif
|
|
|
|
namespace
|
|
{
|
|
|
|
// a while of looking out for something: true once Time is up, the clock read each 64 calls only
|
|
class spin_budget
|
|
{
|
|
public:
|
|
explicit spin_budget(std::chrono::microseconds const Time) : m_until{std::chrono::steady_clock::now() + Time} {}
|
|
bool spent()
|
|
{
|
|
EU07_WORKER_PAUSE();
|
|
return ((++m_turns & 63u) == 0) && std::chrono::steady_clock::now() >= m_until;
|
|
}
|
|
|
|
private:
|
|
std::chrono::steady_clock::time_point m_until;
|
|
unsigned m_turns{0};
|
|
};
|
|
|
|
} // namespace
|
|
|
|
worker_pool::~worker_pool()
|
|
{
|
|
m_stop = true;
|
|
// a job of no seats, which the threads looking out for one see, and the sleeping ones are woken for
|
|
m_ticket.store(((m_ticket.load() >> seat_bits) + 1) << seat_bits);
|
|
for (auto &data : m_threads)
|
|
{
|
|
if (data->sleeping.exchange(false))
|
|
{
|
|
data->start.release();
|
|
}
|
|
}
|
|
for (auto &data : m_threads)
|
|
{
|
|
data->thread.join();
|
|
}
|
|
}
|
|
|
|
std::size_t worker_pool::capacity()
|
|
{
|
|
if (false == Global.performance_multithreading)
|
|
{
|
|
return 1;
|
|
}
|
|
return std::max<std::size_t>(1, std::thread::hardware_concurrency());
|
|
}
|
|
|
|
void worker_pool::grow(std::size_t const Workers)
|
|
{
|
|
// no thread of the pool takes part in a job now, nor looks at the threads
|
|
m_threads.reserve(Workers);
|
|
auto const seen{m_ticket.load() >> seat_bits};
|
|
while (m_threads.size() < Workers)
|
|
{
|
|
auto &data{*m_threads.emplace_back(std::make_unique<thread_data>())};
|
|
data.thread = std::thread(&worker_pool::worker, this, std::ref(data), seen);
|
|
}
|
|
}
|
|
|
|
void worker_pool::run(std::size_t const Count, std::size_t const Threads, std::function<void(std::size_t)> const &Work)
|
|
{
|
|
auto const threads{std::min({Threads, capacity(), Count, static_cast<std::size_t>(seat_mask)})};
|
|
if (threads <= 1)
|
|
{
|
|
for (std::size_t index = 0; index < Count; ++index)
|
|
{
|
|
Work(index);
|
|
}
|
|
return;
|
|
}
|
|
grow(threads - 1);
|
|
|
|
// the job is set before its ticket: the threads take it from there
|
|
m_work = &Work;
|
|
m_count = Count;
|
|
m_next.store(0, std::memory_order_relaxed);
|
|
m_finished.store(0, std::memory_order_relaxed);
|
|
{
|
|
std::scoped_lock lock(m_mutex);
|
|
m_exception = nullptr;
|
|
}
|
|
auto const seats{static_cast<std::uint64_t>(threads - 1)};
|
|
auto const job{(m_ticket.load(std::memory_order_relaxed) >> seat_bits) + 1};
|
|
m_ticket.store((job << seat_bits) | seats);
|
|
wake(job, 2);
|
|
// the calling thread works too
|
|
work(Work, Count);
|
|
|
|
// the seats nobody took are closed, then the threads which took one are waited for
|
|
auto ticket{m_ticket.load()};
|
|
while (false == m_ticket.compare_exchange_weak(ticket, job << seat_bits))
|
|
{
|
|
}
|
|
auto const joined{static_cast<std::size_t>(seats - (ticket & seat_mask))};
|
|
spin_budget budget{spin_time};
|
|
for (auto finished{m_finished.load(std::memory_order_acquire)}; finished != joined; finished = m_finished.load(std::memory_order_acquire))
|
|
{
|
|
if (budget.spent())
|
|
{
|
|
m_finished.wait(finished, std::memory_order_acquire);
|
|
}
|
|
}
|
|
m_work = nullptr;
|
|
std::exception_ptr exception;
|
|
{
|
|
std::scoped_lock lock(m_mutex);
|
|
std::swap(exception, m_exception);
|
|
}
|
|
if (exception)
|
|
{
|
|
std::rethrow_exception(exception);
|
|
}
|
|
}
|
|
|
|
void worker_pool::wake(std::uint64_t const Job, std::size_t Count)
|
|
{
|
|
for (auto &data : m_threads)
|
|
{
|
|
if (Count == 0)
|
|
{
|
|
return;
|
|
}
|
|
auto const ticket{m_ticket.load()};
|
|
if ((ticket >> seat_bits) != Job || (ticket & seat_mask) == 0)
|
|
{
|
|
return;
|
|
}
|
|
// the flag taken by one waker only: one release for each sleep
|
|
if (data->sleeping.load() && data->sleeping.exchange(false))
|
|
{
|
|
data->start.release();
|
|
--Count;
|
|
}
|
|
}
|
|
}
|
|
|
|
void worker_pool::work(std::function<void(std::size_t)> const &Work, std::size_t const Count)
|
|
{
|
|
try
|
|
{
|
|
for (auto index{m_next++}; index < Count; index = m_next++)
|
|
{
|
|
Work(index);
|
|
}
|
|
}
|
|
catch (...)
|
|
{
|
|
std::scoped_lock lock(m_mutex);
|
|
if (false == static_cast<bool>(m_exception))
|
|
{
|
|
m_exception = std::current_exception();
|
|
}
|
|
// the indices left are taken by nobody
|
|
m_next = Count;
|
|
}
|
|
}
|
|
|
|
std::uint64_t worker_pool::next_job(thread_data &Data, std::uint64_t const Seen)
|
|
{
|
|
auto ticket{m_ticket.load(std::memory_order_acquire)};
|
|
spin_budget budget{spin_time};
|
|
while ((ticket >> seat_bits) == Seen)
|
|
{
|
|
if (false == budget.spent())
|
|
{
|
|
ticket = m_ticket.load(std::memory_order_acquire);
|
|
continue;
|
|
}
|
|
// to sleep: the flag first, then a last look. a job set after it finds the flag and wakes the thread, one set before it
|
|
// is seen here. a waker which took the flag meanwhile has released the semaphore, which is taken then
|
|
Data.sleeping.store(true);
|
|
ticket = m_ticket.load();
|
|
if ((ticket >> seat_bits) == Seen || false == Data.sleeping.exchange(false))
|
|
{
|
|
Data.start.acquire();
|
|
}
|
|
ticket = m_ticket.load(std::memory_order_acquire);
|
|
budget = spin_budget{spin_time};
|
|
}
|
|
return ticket;
|
|
}
|
|
|
|
void worker_pool::worker(thread_data &Data, std::uint64_t Seen)
|
|
{
|
|
// draws of Random() in this thread go to an engine of its own: the one of the simulation can't be shared between threads
|
|
std::mt19937 randomengine(static_cast<std::mt19937::result_type>(true_random_seed())); // NOSONAR: randomness of the simulation, not of anything security-relevant
|
|
set_thread_random_engine(&randomengine);
|
|
|
|
while (true)
|
|
{
|
|
auto ticket{next_job(Data, Seen)};
|
|
Seen = ticket >> seat_bits;
|
|
if (m_stop)
|
|
{
|
|
break;
|
|
}
|
|
// a seat of the job, while there are any: the job stays as it is until the thread is done with it
|
|
auto seated{false};
|
|
while ((ticket >> seat_bits) == Seen && (ticket & seat_mask) > 0)
|
|
{
|
|
if (m_ticket.compare_exchange_weak(ticket, ticket - 1))
|
|
{
|
|
seated = true;
|
|
break;
|
|
}
|
|
}
|
|
if (false == seated)
|
|
{
|
|
continue;
|
|
}
|
|
wake(Seen, 2);
|
|
work(*m_work, m_count);
|
|
m_finished.fetch_add(1, std::memory_order_release);
|
|
m_finished.notify_one();
|
|
}
|
|
set_thread_random_engine(nullptr);
|
|
}
|
|
|
|
worker_pool &Workers()
|
|
{
|
|
static worker_pool pool;
|
|
return pool;
|
|
}
|