Changeset View
Changeset View
Standalone View
Standalone View
source/ps/ThreadPool.h
Show All 13 Lines | |||||
* You should have received a copy of the GNU General Public License | * You should have received a copy of the GNU General Public License | ||||
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. | * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. | ||||
*/ | */ | ||||
#ifndef INCLUDED_THREADPOOL | #ifndef INCLUDED_THREADPOOL | ||||
#define INCLUDED_THREADPOOL | #define INCLUDED_THREADPOOL | ||||
#include "ps/Future.h" | #include "ps/Future.h" | ||||
#include "ps/ThreadPoolForward.h" | |||||
#include <memory> | #include <memory> | ||||
#include <vector> | #include <vector> | ||||
class TestThreadPool; | class TestThreadPool; | ||||
class CConfigDB; | class CConfigDB; | ||||
namespace ThreadPool | namespace ThreadPool | ||||
{ | { | ||||
class TaskManager; | |||||
class WorkerThread; | class WorkerThread; | ||||
enum class Priority | |||||
{ | |||||
NORMAL, | |||||
LOW | |||||
}; | |||||
/** | /** | ||||
* Execute work on the global queue. | * Execute work on the global queue. | ||||
* The low-priority queue has a differently-typed executor to highlight the different | * The low-priority queue has a differently-typed executor to highlight the different | ||||
* starvation guaranteeds. | * starvation guaranteeds. | ||||
*/ | */ | ||||
template<Priority Priority = Priority::NORMAL> | template<Priority Priority> | ||||
class GlobalExecutor final : public IExecutor | class GlobalExecutor final : public IExecutor | ||||
{ | { | ||||
friend class TaskManager; | friend class TaskManager; | ||||
protected: | protected: | ||||
GlobalExecutor(TaskManager& tm) : m_TaskManager(tm) {} | GlobalExecutor(TaskManager& tm) : m_TaskManager(tm) {} | ||||
virtual void ExecuteTask(std::function<void()>&& task) override; | virtual void ExecuteTask(std::function<void()>&& task) override; | ||||
virtual std::unique_ptr<IExecutor> Clone() const override { return std::make_unique<GlobalExecutor>(*this); }; | virtual std::unique_ptr<IExecutor> Clone() const override { return std::make_unique<GlobalExecutor>(*this); }; | ||||
▲ Show 20 Lines • Show All 65 Lines • ▼ Show 20 Lines | public: | ||||
*/ | */ | ||||
ThreadExecutor GetWorker(); | ThreadExecutor GetWorker(); | ||||
/** | /** | ||||
* Returns an executor that can be used to start (optionally different) work on (optionally all) threads. | * Returns an executor that can be used to start (optionally different) work on (optionally all) threads. | ||||
*/ | */ | ||||
EachThreadExecutor& GetAllWorkers(); | EachThreadExecutor& GetAllWorkers(); | ||||
/** | |||||
* Add a recurrent task running approximately every X ms. | |||||
* Recurrent tasks are run on the timer thread, and should run in microseconds or less. | |||||
* A typical use-case is to check if some actual work needs to be performed, and use the PoolExecutor | |||||
* passed as argument to do this on one of the regular workers. | |||||
* Note that the timer is allowed to drift over time. | |||||
* @param repeatms - Time, in milliseconds, between two calls. | |||||
* The actual value will depend on the threadpool timer. | |||||
* @param func - Function to run. | |||||
*/ | |||||
void AddRecurrentTask(u32 repeatms, std::function<RecurrentTaskStatus(GlobalExecutor<Priority::NORMAL>&)>&& func); | |||||
private: | private: | ||||
class Impl; | class Impl; | ||||
std::unique_ptr<Impl> m; | std::unique_ptr<Impl> m; | ||||
}; | }; | ||||
} // ThreadPool | } // ThreadPool | ||||
#endif // INCLUDED_THREADPOOL | #endif // INCLUDED_THREADPOOL |
Wildfire Games · Phabricator