1 // Copyright (C) 2020-2021 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"
25 namespace WorkloadManager
27 WorkloadManager::WorkloadManager(WorkloadAlgorithm& algo)
40 WorkloadManager::~WorkloadManager()
45 void WorkloadManager::addResource(const Resource& r)
47 std::unique_lock<std::mutex> lock(_data_mutex);
49 _startCondition.notify_one();
52 void WorkloadManager::freezeResources()
54 std::unique_lock<std::mutex> lock(_data_mutex);
55 _algo.freezeResources();
56 _startCondition.notify_one();
59 void WorkloadManager::addTask(Task* t)
61 std::unique_lock<std::mutex> lock(_data_mutex);
63 _startCondition.notify_one();
66 void WorkloadManager::start()
69 std::unique_lock<std::mutex> lock(_data_mutex);
71 return; // already started
74 _otherThreads.emplace_back(std::async(std::launch::async, [this]
78 _otherThreads.emplace_back(std::async(std::launch::async, [this]
84 void WorkloadManager::stop()
87 std::unique_lock<std::mutex> lock(_data_mutex);
89 _algo.freezeResources();
91 _startCondition.notify_one();
92 _endCondition.notify_one();
93 for(std::future<void>& th : _otherThreads)
97 void WorkloadManager::runTasks()
99 bool threadStop = false;
103 std::unique_lock<std::mutex> lock(_data_mutex);
104 _startCondition.wait(lock, [this] {return !_algo.empty() || _stop;});
105 RunningInfo taskInfo;
106 while(chooseTaskToRun(taskInfo))
108 _runningTasks.emplace(taskInfo.id, std::async(std::launch::async, [this, taskInfo]
110 runOneTask(taskInfo);
113 threadStop = _stop && _algo.empty();
115 // workaroud to release the lock and give a chance to other tasks to finish
116 std::this_thread::sleep_for(std::chrono::milliseconds(1));
120 void WorkloadManager::runOneTask(const RunningInfo& taskInfo)
122 taskInfo.info.task->run(taskInfo.info.worker);
125 std::unique_lock<std::mutex> lock(_data_mutex);
126 _finishedTasks.push(taskInfo);
127 _endCondition.notify_one();
131 void WorkloadManager::endTasks()
133 bool threadStop = false;
136 std::unique_lock<std::mutex> lock(_data_mutex);
137 _endCondition.wait(lock, [this]
139 return !_finishedTasks.empty() ||
140 (_stop && _runningTasks.empty() && _algo.empty());
142 while(!_finishedTasks.empty())
144 RunningInfo taskInfo = _finishedTasks.front();
145 _finishedTasks.pop();
146 _runningTasks[taskInfo.id].wait();
147 _runningTasks.erase(taskInfo.id);
148 _algo.liberate(taskInfo.info);
150 threadStop = _stop && _runningTasks.empty() && _algo.empty();
151 _startCondition.notify_one();
155 bool WorkloadManager::chooseTaskToRun(RunningInfo& taskInfo)
157 // We are already under the lock
158 taskInfo.id = _nextIndex;
159 taskInfo.info = _algo.chooseTask();
160 if(taskInfo.info.taskFound)
162 return taskInfo.info.taskFound;