Salome HOME
Merge branch 'V9_7_BR'
[modules/yacs.git] / src / workloadmanager / WorkloadManager.cxx
index 716bed0c76811217f5392004c3a8e4c88819c82a..98224019f42b0ea2521ee4a5376cfd9a0c6e4a6c 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2020  CEA/DEN, EDF R&D
+// Copyright (C) 2020-2021  CEA/DEN, EDF R&D
 //
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
@@ -28,7 +28,7 @@ namespace WorkloadManager
   , _data_mutex()
   , _startCondition()
   , _endCondition()
-  , _stop(false)
+  , _stop(true)
   , _otherThreads()
   , _algo(algo)
   {
@@ -46,6 +46,13 @@ namespace WorkloadManager
     _startCondition.notify_one();
   }
 
+  void WorkloadManager::freezeResources()
+  {
+    std::unique_lock<std::mutex> lock(_data_mutex);
+    _algo.freezeResources();
+    _startCondition.notify_one();
+  }
+
   void WorkloadManager::addTask(Task* t)
   {
     std::unique_lock<std::mutex> lock(_data_mutex);
@@ -57,13 +64,15 @@ namespace WorkloadManager
   {
     {
       std::unique_lock<std::mutex> lock(_data_mutex);
+      if(!_stop)
+        return; // already started
       _stop = false;
     }
-    _otherThreads.emplace_back(std::async([this]
+    _otherThreads.emplace_back(std::async(std::launch::async, [this]
       {
         runTasks();
       }));
-    _otherThreads.emplace_back(std::async([this]
+    _otherThreads.emplace_back(std::async(std::launch::async, [this]
       {
         endTasks();
       }));
@@ -74,6 +83,7 @@ namespace WorkloadManager
     {
       std::unique_lock<std::mutex> lock(_data_mutex);
       _stop = true;
+      _algo.freezeResources();
     }
     _startCondition.notify_one();
     _endCondition.notify_one();
@@ -91,7 +101,7 @@ namespace WorkloadManager
       RunningInfo taskInfo;
       while(chooseTaskToRun(taskInfo))
       {
-        _runningTasks.emplace(taskInfo.id, std::async([this, taskInfo]
+        _runningTasks.emplace(taskInfo.id, std::async(std::launch::async, [this, taskInfo]
           {
             runOneTask(taskInfo);
           }));