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"
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::freezeResources()
51 std::unique_lock<std::mutex> lock(_data_mutex);
52 _algo.freezeResources();
53 _startCondition.notify_one();
56 void WorkloadManager::addTask(Task* t)
58 std::unique_lock<std::mutex> lock(_data_mutex);
60 _startCondition.notify_one();
63 void WorkloadManager::start()
66 std::unique_lock<std::mutex> lock(_data_mutex);
68 return; // already started
71 _otherThreads.emplace_back(std::async(std::launch::async, [this]
75 _otherThreads.emplace_back(std::async(std::launch::async, [this]
81 void WorkloadManager::stop()
84 std::unique_lock<std::mutex> lock(_data_mutex);
86 _algo.freezeResources();
88 _startCondition.notify_one();
89 _endCondition.notify_one();
90 for(std::future<void>& th : _otherThreads)
94 void WorkloadManager::runTasks()
96 bool threadStop = false;
99 std::unique_lock<std::mutex> lock(_data_mutex);
100 _startCondition.wait(lock, [this] {return !_algo.empty() || _stop;});
101 RunningInfo taskInfo;
102 while(chooseTaskToRun(taskInfo))
104 _runningTasks.emplace(taskInfo.id, std::async(std::launch::async, [this, taskInfo]
106 runOneTask(taskInfo);
109 threadStop = _stop && _algo.empty();
113 void WorkloadManager::runOneTask(const RunningInfo& taskInfo)
115 taskInfo.info.task->run(taskInfo.info.worker);
118 std::unique_lock<std::mutex> lock(_data_mutex);
119 _finishedTasks.push(taskInfo);
120 _endCondition.notify_one();
124 void WorkloadManager::endTasks()
126 bool threadStop = false;
129 std::unique_lock<std::mutex> lock(_data_mutex);
130 _endCondition.wait(lock, [this]
132 return !_finishedTasks.empty() ||
133 (_stop && _runningTasks.empty() && _algo.empty());
135 while(!_finishedTasks.empty())
137 RunningInfo taskInfo = _finishedTasks.front();
138 _finishedTasks.pop();
139 _runningTasks[taskInfo.id].wait();
140 _runningTasks.erase(taskInfo.id);
141 _algo.liberate(taskInfo.info);
143 threadStop = _stop && _runningTasks.empty() && _algo.empty();
144 _startCondition.notify_one();
148 bool WorkloadManager::chooseTaskToRun(RunningInfo& taskInfo)
150 // We are already under the lock
151 taskInfo.id = _nextIndex;
152 taskInfo.info = _algo.chooseTask();
153 if(taskInfo.info.taskFound)
155 return taskInfo.info.taskFound;