Pyrogenesis trunk
TaskManager.h
Go to the documentation of this file.
1/* Copyright (C) 2022 Wildfire Games.
2 * This file is part of 0 A.D.
3 *
4 * 0 A.D. is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * 0 A.D. is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#ifndef INCLUDED_THREADING_TASKMANAGER
19#define INCLUDED_THREADING_TASKMANAGER
20
21#include "ps/Future.h"
22
23#include <memory>
24#include <vector>
25
26namespace Threading
27{
28enum class TaskPriority
29{
30 NORMAL,
31 LOW
32};
33
34/**
35 * The task manager creates all worker threads on initialisation,
36 * and manages the task queues.
37 * See implementation for additional comments.
38 */
40{
41 friend class WorkerThread;
42public:
45 TaskManager(const TaskManager&) = delete;
49
50 static void Initialise();
51 static TaskManager& Instance();
52
53 /**
54 * Clears all tasks from the queue. This blocks on started tasks.
55 */
56 void ClearQueue();
57
58 /**
59 * @return the number of threaded workers.
60 */
61 size_t GetNumberOfWorkers() const;
62
63 /**
64 * Push a task to be executed.
65 */
66 template<typename T>
68 {
70 DoPushTask(ret.Wrap(std::move(func)), priority);
71 return ret;
72 }
73
74private:
75 TaskManager(size_t numberOfWorkers);
76
77 void DoPushTask(std::function<void()>&& task, TaskPriority priority);
78
79 class Impl;
80 const std::unique_ptr<Impl> m;
81};
82} // namespace Threading
83
84#endif // INCLUDED_THREADING_TASKMANAGER
Corresponds to std::future.
Definition: Future.h:151
PackagedTask< Callback > Wrap(Callback &&callback)
Make the future wait for the result of callback.
Definition: Future.h:297
The task manager creates all worker threads on initialisation, and manages the task queues.
Definition: TaskManager.h:40
TaskManager(const TaskManager &)=delete
static TaskManager & Instance()
Definition: TaskManager.cpp:245
TaskManager()
Definition: TaskManager.cpp:163
const std::unique_ptr< Impl > m
Definition: TaskManager.h:80
TaskManager & operator=(TaskManager &&)=delete
void ClearQueue()
Clears all tasks from the queue.
Definition: TaskManager.cpp:182
TaskManager(TaskManager &&)=delete
static void Initialise()
Definition: TaskManager.cpp:239
Future< std::invoke_result_t< T > > PushTask(T &&func, TaskPriority priority=TaskPriority::NORMAL)
Push a task to be executed.
Definition: TaskManager.h:67
TaskManager & operator=(const TaskManager &)=delete
size_t GetNumberOfWorkers() const
Definition: TaskManager.cpp:195
void DoPushTask(std::function< void()> &&task, TaskPriority priority)
Definition: TaskManager.cpp:200
Worker thread: process the taskManager queues until killed.
Definition: TaskManager.cpp:98
Light-weight threading utilities.
Definition: TaskManager.cpp:38
TaskPriority
Definition: TaskManager.h:29
#define T(string_literal)
Definition: secure_crt.cpp:77