Core: Rework TaskScheduler (WIP)
This commit is contained in:
@@ -8,28 +8,45 @@
|
||||
#define NAZARA_CORE_TASKSCHEDULER_HPP
|
||||
|
||||
#include <NazaraUtils/Prerequisites.hpp>
|
||||
#include <Nazara/Core/Functor.hpp>
|
||||
#include <Nazara/Core/Config.hpp>
|
||||
#include <atomic>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <random>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
class NAZARA_CORE_API TaskScheduler
|
||||
{
|
||||
public:
|
||||
TaskScheduler() = delete;
|
||||
~TaskScheduler() = delete;
|
||||
using Task = std::function<void()>;
|
||||
|
||||
template<typename F> static void AddTask(F function);
|
||||
template<typename F, typename... Args> static void AddTask(F function, Args&&... args);
|
||||
template<typename C> static void AddTask(void (C::*function)(), C* object);
|
||||
static unsigned int GetWorkerCount();
|
||||
static bool Initialize();
|
||||
static void Run();
|
||||
static void SetWorkerCount(unsigned int workerCount);
|
||||
static void Uninitialize();
|
||||
static void WaitForTasks();
|
||||
TaskScheduler(unsigned int workerCount = 0);
|
||||
TaskScheduler(const TaskScheduler&) = delete;
|
||||
TaskScheduler(TaskScheduler&&) = default;
|
||||
~TaskScheduler();
|
||||
|
||||
void AddTask(Task&& task);
|
||||
|
||||
unsigned int GetWorkerCount() const;
|
||||
|
||||
void WaitForTasks();
|
||||
|
||||
TaskScheduler& operator=(const TaskScheduler&) = delete;
|
||||
TaskScheduler& operator=(TaskScheduler&&) = default;
|
||||
|
||||
private:
|
||||
static void AddTaskFunctor(AbstractFunctor* taskFunctor);
|
||||
class Worker;
|
||||
friend Worker;
|
||||
|
||||
Worker& GetWorker(unsigned int workerIndex);
|
||||
void NotifyWorkerActive();
|
||||
void NotifyWorkerIdle();
|
||||
|
||||
std::atomic_bool m_idle;
|
||||
std::atomic_uint m_idleWorkerCount;
|
||||
std::minstd_rand m_randomGenerator;
|
||||
std::vector<Worker> m_workers;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -6,49 +6,6 @@
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
/*!
|
||||
* \ingroup core
|
||||
* \class Nz::TaskScheduler
|
||||
* \brief Core class that represents a thread pool
|
||||
*/
|
||||
|
||||
/*!
|
||||
* \brief Adds a task to the pending list
|
||||
*
|
||||
* \param function Task that the pool will execute
|
||||
*/
|
||||
|
||||
template<typename F>
|
||||
void TaskScheduler::AddTask(F function)
|
||||
{
|
||||
AddTaskFunctor(new FunctorWithoutArgs<F>(function));
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Adds a task to the pending list
|
||||
*
|
||||
* \param function Task that the pool will execute
|
||||
* \param args Arguments of the function
|
||||
*/
|
||||
|
||||
template<typename F, typename... Args>
|
||||
void TaskScheduler::AddTask(F function, Args&&... args)
|
||||
{
|
||||
AddTaskFunctor(new FunctorWithArgs<F, Args...>(function, std::forward<Args>(args)...));
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Adds a task to the pending list
|
||||
*
|
||||
* \param function Task that the pool will execute
|
||||
* \param object Object on which the method will be called
|
||||
*/
|
||||
|
||||
template<typename C>
|
||||
void TaskScheduler::AddTask(void (C::*function)(), C* object)
|
||||
{
|
||||
AddTaskFunctor(new MemberWithoutArgs<C>(function, object));
|
||||
}
|
||||
}
|
||||
|
||||
#include <Nazara/Core/DebugOff.hpp>
|
||||
|
||||
Reference in New Issue
Block a user