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 
26 namespace Threading
27 {
28 enum 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;
42 public:
43  TaskManager();
44  ~TaskManager();
45  TaskManager(const TaskManager&) = delete;
46  TaskManager(TaskManager&&) = delete;
47  TaskManager& operator=(const TaskManager&) = delete;
48  TaskManager& operator=(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 
74 private:
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
The task manager creates all worker threads on initialisation, and manages the task queues...
Definition: TaskManager.h:39
PackagedTask< ResultType > Wrap(T &&func)
Make the future wait for the result of func.
Definition: Future.h:281
Light-weight threading utilities.
Definition: TaskManager.cpp:37
PImpl-ed implementation of the Task manager.
Definition: TaskManager.cpp:122
Corresponds to std::future.
Definition: Future.h:145
#define T(string_literal)
Definition: secure_crt.cpp:77
const std::unique_ptr< Impl > m
Definition: TaskManager.h:79
TaskPriority
Definition: TaskManager.h:28
Future< std::invoke_result_t< T > > PushTask(T &&func, TaskPriority priority=TaskPriority::NORMAL)
Push a task to be executed.
Definition: TaskManager.h:67
Worker thread: process the taskManager queues until killed.
Definition: TaskManager.cpp:97