Salome HOME
e971d1ea6293b467c933983b3327a23f629fd0db
[modules/yacs.git] / src / workloadmanager / WorkloadManager.hxx
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 #ifndef WORKLOADMANAGER_H
20 #define WORKLOADMANAGER_H
21 #include "YACSlibWorkloadmanagerExport.hxx"
22
23 #include <mutex>
24 #include <future>
25 #include <condition_variable> // notifications
26 #include <map>
27 #include <queue>
28 #include <list>
29 #include "Task.hxx"
30 #include "WorkloadAlgorithm.hxx"
31
32 namespace WorkloadManager
33 {
34   class YACSLIBWLM_EXPORT WorkloadManager
35   {
36   public:
37     WorkloadManager(WorkloadAlgorithm& algo);
38     WorkloadManager(const WorkloadManager&) = delete;
39     WorkloadManager()=delete;
40     ~WorkloadManager();
41     void addTask(Task* t);
42     void addResource(const Resource& r);
43     void start(); //! start execution
44     void stop(); //! stop execution
45
46   private:
47     typedef unsigned long TaskId;
48     struct RunningInfo
49     {
50       TaskId id;
51       WorkloadAlgorithm::LaunchInfo info;
52     };
53     std::map<TaskId, std::future<void> > _runningTasks;
54     std::queue<RunningInfo> _finishedTasks;
55     TaskId _nextIndex;
56     std::mutex _data_mutex;
57     std::condition_variable _startCondition; // start tasks thread notification
58     std::condition_variable _endCondition; // end tasks thread notification
59     bool _stop;
60     std::vector< std::future<void> > _otherThreads;
61     WorkloadAlgorithm& _algo;
62
63     void runTasks();
64     void endTasks();
65     void runOneTask(const RunningInfo& taskInfo);
66     // choose a task and block a resource
67     bool chooseTaskToRun(RunningInfo& taskInfo);
68   };
69 }
70 #endif // WORKLOADMANAGER_H