1 // Copyright (C) 2020 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #include "WorkloadManager.hxx"
22 namespace WorkloadManager
24 WorkloadManager::WorkloadManager(WorkloadAlgorithm& algo)
37 WorkloadManager::~WorkloadManager()
42 void WorkloadManager::addResource(const Resource& r)
44 std::unique_lock<std::mutex> lock(_data_mutex);
46 _startCondition.notify_one();
49 void WorkloadManager::addTask(Task* t)
51 std::unique_lock<std::mutex> lock(_data_mutex);
53 _startCondition.notify_one();
56 void WorkloadManager::start()
59 std::unique_lock<std::mutex> lock(_data_mutex);
62 _otherThreads.emplace_back(std::async(std::launch::async, [this]
66 _otherThreads.emplace_back(std::async(std::launch::async, [this]
72 void WorkloadManager::stop()
75 std::unique_lock<std::mutex> lock(_data_mutex);
78 _startCondition.notify_one();
79 _endCondition.notify_one();
80 for(std::future<void>& th : _otherThreads)
84 void WorkloadManager::runTasks()
86 bool threadStop = false;
89 std::unique_lock<std::mutex> lock(_data_mutex);
90 _startCondition.wait(lock, [this] {return !_algo.empty() || _stop;});
92 while(chooseTaskToRun(taskInfo))
94 _runningTasks.emplace(taskInfo.id, std::async(std::launch::async, [this, taskInfo]
99 threadStop = _stop && _algo.empty();
103 void WorkloadManager::runOneTask(const RunningInfo& taskInfo)
105 taskInfo.info.task->run(taskInfo.info.worker);
108 std::unique_lock<std::mutex> lock(_data_mutex);
109 _finishedTasks.push(taskInfo);
110 _endCondition.notify_one();
114 void WorkloadManager::endTasks()
116 bool threadStop = false;
119 std::unique_lock<std::mutex> lock(_data_mutex);
120 _endCondition.wait(lock, [this]
122 return !_finishedTasks.empty() ||
123 (_stop && _runningTasks.empty() && _algo.empty());
125 while(!_finishedTasks.empty())
127 RunningInfo taskInfo = _finishedTasks.front();
128 _finishedTasks.pop();
129 _runningTasks[taskInfo.id].wait();
130 _runningTasks.erase(taskInfo.id);
131 _algo.liberate(taskInfo.info);
133 threadStop = _stop && _runningTasks.empty() && _algo.empty();
134 _startCondition.notify_one();
138 bool WorkloadManager::chooseTaskToRun(RunningInfo& taskInfo)
140 // We are already under the lock
141 taskInfo.id = _nextIndex;
142 taskInfo.info = _algo.chooseTask();
143 if(taskInfo.info.taskFound)
145 return taskInfo.info.taskFound;