LCOV - code coverage report
Current view: top level - source/ps - TaskManager.h (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 5 5 100.0 %
Date: 2023-01-19 00:18:29 Functions: 10 12 83.3 %

          Line data    Source code
       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             :  */
      39           1 : class TaskManager
      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>
      67      100009 :     Future<std::invoke_result_t<T>> PushTask(T&& func, TaskPriority priority = TaskPriority::NORMAL)
      68             :     {
      69      100009 :         Future<std::invoke_result_t<T>> ret;
      70      100009 :         DoPushTask(ret.Wrap(std::move(func)), priority);
      71      100009 :         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

Generated by: LCOV version 1.13