2020-11-07 13:42:04 +01:00
|
|
|
/*****************************************************************************
|
2026-01-05 14:42:59 +01:00
|
|
|
* Copyright (c) 2014-2026 OpenRCT2 developers
|
2020-11-07 13:42:04 +01:00
|
|
|
*
|
|
|
|
|
* For a complete list of all authors, please refer to contributors.md
|
|
|
|
|
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
|
|
|
|
|
*
|
|
|
|
|
* OpenRCT2 is licensed under the GNU General Public License version 3.
|
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include "JobPool.h"
|
|
|
|
|
|
|
|
|
|
#include <cassert>
|
|
|
|
|
|
2026-02-20 11:39:00 -08:00
|
|
|
#if defined(__EMSCRIPTEN__) && !defined(__EMSCRIPTEN_PTHREADS__)
|
|
|
|
|
static constexpr bool kJobPoolSingleThreaded = true;
|
|
|
|
|
#else
|
|
|
|
|
static constexpr bool kJobPoolSingleThreaded = false;
|
|
|
|
|
#endif
|
|
|
|
|
|
2020-11-07 13:42:04 +01:00
|
|
|
JobPool::TaskData::TaskData(std::function<void()> workFn, std::function<void()> completionFn)
|
2025-01-09 14:34:05 +01:00
|
|
|
: WorkFn(std::move(workFn))
|
|
|
|
|
, CompletionFn(std::move(completionFn))
|
2020-11-07 13:42:04 +01:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
JobPool::JobPool(size_t maxThreads)
|
|
|
|
|
{
|
2026-02-20 11:39:00 -08:00
|
|
|
if constexpr (kJobPoolSingleThreaded)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-09 14:34:05 +01:00
|
|
|
maxThreads = std::min<size_t>(maxThreads, std::max(1u, std::thread::hardware_concurrency()));
|
2020-11-07 13:42:04 +01:00
|
|
|
for (size_t n = 0; n < maxThreads; n++)
|
|
|
|
|
{
|
|
|
|
|
_threads.emplace_back(&JobPool::ProcessQueue, this);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
JobPool::~JobPool()
|
|
|
|
|
{
|
|
|
|
|
{
|
2025-10-01 08:13:53 +02:00
|
|
|
std::lock_guard lock(_mutex);
|
2020-11-07 13:42:04 +01:00
|
|
|
_shouldStop = true;
|
|
|
|
|
}
|
2025-01-09 14:34:05 +01:00
|
|
|
_condPending.notify_all();
|
2020-11-07 13:42:04 +01:00
|
|
|
|
2021-01-12 06:14:15 +09:00
|
|
|
for (auto& th : _threads)
|
2020-11-07 13:42:04 +01:00
|
|
|
{
|
|
|
|
|
assert(th.joinable() != false);
|
|
|
|
|
th.join();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void JobPool::AddTask(std::function<void()> workFn, std::function<void()> completionFn)
|
|
|
|
|
{
|
2026-02-20 11:39:00 -08:00
|
|
|
if constexpr (kJobPoolSingleThreaded)
|
|
|
|
|
{
|
|
|
|
|
workFn();
|
|
|
|
|
if (completionFn)
|
|
|
|
|
{
|
|
|
|
|
completionFn();
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-09 14:34:05 +01:00
|
|
|
{
|
2025-10-01 08:13:53 +02:00
|
|
|
std::lock_guard lock(_mutex);
|
2025-01-09 14:34:05 +01:00
|
|
|
_pending.emplace_back(workFn, completionFn);
|
|
|
|
|
}
|
2020-11-07 13:42:04 +01:00
|
|
|
_condPending.notify_one();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void JobPool::Join(std::function<void()> reportFn)
|
|
|
|
|
{
|
2025-10-01 08:13:53 +02:00
|
|
|
std::unique_lock lock(_mutex);
|
2020-11-07 13:42:04 +01:00
|
|
|
while (true)
|
|
|
|
|
{
|
|
|
|
|
// Wait for the queue to become empty or having completed tasks.
|
|
|
|
|
_condComplete.wait(lock, [this]() { return (_pending.empty() && _processing == 0) || !_completed.empty(); });
|
|
|
|
|
|
|
|
|
|
// Dispatch all completion callbacks if there are any.
|
|
|
|
|
while (!_completed.empty())
|
|
|
|
|
{
|
2025-01-09 14:34:05 +01:00
|
|
|
auto taskData = std::move(_completed.front());
|
2020-11-07 13:42:04 +01:00
|
|
|
_completed.pop_front();
|
|
|
|
|
|
|
|
|
|
if (taskData.CompletionFn)
|
|
|
|
|
{
|
|
|
|
|
lock.unlock();
|
|
|
|
|
|
|
|
|
|
taskData.CompletionFn();
|
|
|
|
|
|
|
|
|
|
lock.lock();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (reportFn)
|
|
|
|
|
{
|
|
|
|
|
lock.unlock();
|
|
|
|
|
|
|
|
|
|
reportFn();
|
|
|
|
|
|
|
|
|
|
lock.lock();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If everything is empty and no more work has to be done we can stop waiting.
|
|
|
|
|
if (_completed.empty() && _pending.empty() && _processing == 0)
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-09 14:34:05 +01:00
|
|
|
bool JobPool::IsBusy()
|
2020-11-07 13:42:04 +01:00
|
|
|
{
|
2025-10-01 08:13:53 +02:00
|
|
|
std::lock_guard lock(_mutex);
|
2025-01-09 14:34:05 +01:00
|
|
|
return _processing != 0 || !_pending.empty();
|
2024-04-24 18:31:00 +02:00
|
|
|
}
|
|
|
|
|
|
2020-11-07 13:42:04 +01:00
|
|
|
void JobPool::ProcessQueue()
|
|
|
|
|
{
|
2025-10-01 08:13:53 +02:00
|
|
|
std::unique_lock lock(_mutex);
|
2020-11-07 13:42:04 +01:00
|
|
|
do
|
|
|
|
|
{
|
2021-01-10 18:23:35 +00:00
|
|
|
// Wait for work or cancellation.
|
2020-11-07 13:42:04 +01:00
|
|
|
_condPending.wait(lock, [this]() { return _shouldStop || !_pending.empty(); });
|
|
|
|
|
|
|
|
|
|
if (!_pending.empty())
|
|
|
|
|
{
|
|
|
|
|
_processing++;
|
|
|
|
|
|
2025-01-09 14:34:05 +01:00
|
|
|
auto taskData = std::move(_pending.front());
|
2020-11-07 13:42:04 +01:00
|
|
|
_pending.pop_front();
|
|
|
|
|
|
|
|
|
|
lock.unlock();
|
|
|
|
|
|
|
|
|
|
taskData.WorkFn();
|
|
|
|
|
|
|
|
|
|
lock.lock();
|
|
|
|
|
|
2021-01-09 05:59:55 +09:00
|
|
|
_completed.push_back(std::move(taskData));
|
2020-11-07 13:42:04 +01:00
|
|
|
|
|
|
|
|
_processing--;
|
|
|
|
|
_condComplete.notify_one();
|
|
|
|
|
}
|
|
|
|
|
} while (!_shouldStop);
|
|
|
|
|
}
|