Salome HOME
d0ac55609844338209b263123c1122551828b85f
[modules/yacs.git] / src / workloadmanager / WorkloadManager.cxx
1 // Copyright (C) 2020  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 #include "WorkloadManager.hxx"
20 #include "Task.hxx"
21
22 namespace WorkloadManager
23 {
24   WorkloadManager::WorkloadManager(WorkloadAlgorithm& algo)
25   : _runningTasks()
26   , _finishedTasks()
27   , _nextIndex(0)
28   , _data_mutex()
29   , _startCondition()
30   , _endCondition()
31   , _stop(false)
32   , _otherThreads()
33   , _algo(algo)
34   {
35   }
36
37   WorkloadManager::~WorkloadManager()
38   {
39     stop();
40   }
41
42   void WorkloadManager::addResource(const Resource& r)
43   {
44     std::unique_lock<std::mutex> lock(_data_mutex);
45     _algo.addResource(r);
46     _startCondition.notify_one();
47   }
48
49   void WorkloadManager::addTask(Task* t)
50   {
51     std::unique_lock<std::mutex> lock(_data_mutex);
52     _algo.addTask(t);
53     _startCondition.notify_one();
54   }
55
56   void WorkloadManager::start()
57   {
58     {
59       std::unique_lock<std::mutex> lock(_data_mutex);
60       _stop = false;
61     }
62     _otherThreads.emplace_back(std::async(std::launch::async, [this]
63       {
64         runTasks();
65       }));
66     _otherThreads.emplace_back(std::async(std::launch::async, [this]
67       {
68         endTasks();
69       }));
70   }
71
72   void WorkloadManager::stop()
73   {
74     {
75       std::unique_lock<std::mutex> lock(_data_mutex);
76       _stop = true;
77     }
78     _startCondition.notify_one();
79     _endCondition.notify_one();
80    for(std::future<void>& th : _otherThreads)
81      th.wait();
82   }
83
84   void WorkloadManager::runTasks()
85   {
86     bool threadStop = false;
87     while(!threadStop)
88     {
89       std::unique_lock<std::mutex> lock(_data_mutex);
90       _startCondition.wait(lock, [this] {return !_algo.empty() || _stop;});
91       RunningInfo taskInfo;
92       while(chooseTaskToRun(taskInfo))
93       {
94         _runningTasks.emplace(taskInfo.id, std::async(std::launch::async, [this, taskInfo]
95           {
96             runOneTask(taskInfo);
97           }));
98       }
99       threadStop = _stop && _algo.empty();
100     }
101   }
102
103   void WorkloadManager::runOneTask(const RunningInfo& taskInfo)
104   {
105     taskInfo.info.task->run(taskInfo.info.worker);
106
107     {
108       std::unique_lock<std::mutex> lock(_data_mutex);
109       _finishedTasks.push(taskInfo);
110       _endCondition.notify_one();
111     }
112   }
113
114   void WorkloadManager::endTasks()
115   {
116     bool threadStop = false;
117     while(!threadStop)
118     {
119       std::unique_lock<std::mutex> lock(_data_mutex);
120       _endCondition.wait(lock, [this]
121                             {
122                               return !_finishedTasks.empty() ||
123                               (_stop && _runningTasks.empty() && _algo.empty());
124                             });
125       while(!_finishedTasks.empty())
126       {
127         RunningInfo taskInfo = _finishedTasks.front();
128         _finishedTasks.pop();
129         _runningTasks[taskInfo.id].wait();
130         _runningTasks.erase(taskInfo.id);
131         _algo.liberate(taskInfo.info);
132       }
133       threadStop = _stop && _runningTasks.empty() && _algo.empty();
134       _startCondition.notify_one();
135     }
136   }
137
138   bool WorkloadManager::chooseTaskToRun(RunningInfo& taskInfo)
139   {
140     // We are already under the lock
141     taskInfo.id = _nextIndex;
142     taskInfo.info = _algo.chooseTask();
143     if(taskInfo.info.taskFound)
144       _nextIndex ++;
145     return taskInfo.info.taskFound;
146   }
147
148 }