/* 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 #if defined(_M_X64) || defined(_M_IX86) || defined(__x86_64__) || defined(__i386__) #include #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(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())}; 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 const &Work) { auto const threads{std::min({Threads, capacity(), Count, static_cast(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(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(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 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(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(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; }