Core/TaskScheduler: Remove jthread and stop_token

jthread/stop_token are not yet implemented in libc++ and on Apple Clang
This commit is contained in:
SirLynix 2024-01-31 21:04:42 +01:00
parent 47283776e6
commit 9db333fa80
2 changed files with 20 additions and 10 deletions

View File

@ -23,7 +23,7 @@ namespace Nz
TaskScheduler(unsigned int workerCount = 0);
TaskScheduler(const TaskScheduler&) = delete;
TaskScheduler(TaskScheduler&&) = default;
TaskScheduler(TaskScheduler&&) = delete;
~TaskScheduler();
void AddTask(Task&& task);
@ -33,7 +33,7 @@ namespace Nz
void WaitForTasks();
TaskScheduler& operator=(const TaskScheduler&) = delete;
TaskScheduler& operator=(TaskScheduler&&) = default;
TaskScheduler& operator=(TaskScheduler&&) = delete;
private:
class Worker;

View File

@ -8,8 +8,8 @@
#include <NazaraUtils/StackArray.hpp>
#include <condition_variable>
#include <mutex>
#include <new>
#include <random>
#include <stop_token>
#include <thread>
#include <Nazara/Core/Debug.hpp>
@ -22,13 +22,14 @@ namespace Nz
{
public:
Worker(TaskScheduler& owner, unsigned int workerIndex) :
m_running(true),
m_owner(owner),
m_workerIndex(workerIndex)
{
m_thread = std::jthread([this](std::stop_token stopToken)
m_thread = std::thread([this]
{
SetCurrentThreadName(fmt::format("NzWorker #{0}", m_workerIndex).c_str());
Run(stopToken);
Run();
});
}
@ -40,6 +41,14 @@ namespace Nz
NAZARA_UNREACHABLE();
}
~Worker()
{
m_running = false;
m_conditionVariable.notify_one();
m_thread.join();
}
bool AddTask(Task&& task)
{
std::unique_lock lock(m_mutex, std::defer_lock);
@ -53,7 +62,7 @@ namespace Nz
return true;
}
void Run(std::stop_token& stopToken)
void Run()
{
StackArray<unsigned int> randomWorkerIndices = NazaraStackArrayNoInit(unsigned int, m_owner.GetWorkerCount() - 1);
{
@ -82,10 +91,10 @@ namespace Nz
idle = true;
}
m_conditionVariable.wait(m_mutex, stopToken, [this] { return !m_tasks.empty(); });
m_conditionVariable.wait(lock, [this] { return !m_running || !m_tasks.empty(); });
}
if (stopToken.stop_requested())
if (!m_running)
break;
auto ExecuteTask = [&](TaskScheduler::Task& task)
@ -150,9 +159,10 @@ namespace Nz
}
private:
std::condition_variable_any m_conditionVariable;
std::atomic_bool m_running;
std::condition_variable m_conditionVariable;
std::mutex m_mutex;
std::jthread m_thread;
std::thread m_thread;
std::vector<TaskScheduler::Task> m_tasks;
TaskScheduler& m_owner;
unsigned int m_workerIndex;