TaskRunner.h
1 /**************************************************************************************
2 Copyright 2015 Applied Research Associates, Inc.
3 Licensed under the Apache License, Version 2.0 (the "License"); you may not use
4 this file except in compliance with the License. You may obtain a copy of the License
5 at:
6 http://www.apache.org/licenses/LICENSE-2.0
7 Unless required by applicable law or agreed to in writing, software distributed under
8 the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
9 CONDITIONS OF ANY KIND, either express or implied. See the License for the
10 specific language governing permissions and limitations under the License.
11 **************************************************************************************/
12 
13 #pragma once
14 
15 #include <biogears/cdm/utils/TaskRunner/TaskQueue.h>
16 
17 #include <biogears/cdm/CommonDataModel.h>
18 
19 #include <memory>
20 #include <mutex>
21 #include <queue>
22 
23 namespace biogears {
24 //--------------------------------------------------------------------------------------------------
27 //--------------------------------------------------------------------------------------------------
28 class BIOGEARS_API TaskRunner : public TaskQueue {
29 public:
30  TaskRunner();
31  TaskRunner(unsigned int threadCount);
32  virtual ~TaskRunner() = default;
33 
34  TaskRunner(const TaskRunner& other) = delete;
35  TaskRunner& operator=(const TaskRunner& other) = delete;
36 
37  void AddTask(std::unique_ptr<Task> task);
38  void Run();
39 
40  virtual std::unique_ptr<Task> GetNextTask() override;
41 
42  void EchoProgress(bool echo) { m_echoPercentComplete = echo; }
43  virtual void ReportTaskComplete() override;
44 
45 private:
46  void PrintProgress();
47 
48  mutable std::recursive_mutex m_taskQueueMutex;
49  std::queue<std::unique_ptr<Task>> m_tasks;
50 
51  unsigned int m_threadCount = 1;
52 
53  bool m_echoPercentComplete = false;
54  unsigned int m_totalTaskCount = 0;
55  unsigned int m_tasksComplete = 0;
56 };
57 }
Interface through which TaskProcessors retrieve Tasks from the TaskRunner.
Definition: TaskQueue.h:23
void EchoProgress(bool echo)
Definition: TaskRunner.h:42
std::recursive_mutex m_taskQueueMutex
Mutex used to synchronize concurrent access to the task queue.
Definition: TaskRunner.h:48
std::queue< std::unique_ptr< Task > > m_tasks
Queue of tasks to be run.
Definition: TaskRunner.h:49
This class handles concurrent execution of tasks.
Definition: TaskRunner.h:28
Definition: SEElectricalCircuit.h:18