Salome HOME
Merge branch 'V8_5_BR'
[modules/yacs.git] / src / engine / Executor.hxx
1 // Copyright (C) 2006-2016  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
20 #ifndef __EXECUTOR_HXX__
21 #define __EXECUTOR_HXX__
22
23 #include "YACSlibEngineExport.hxx"
24 #include "Mutex.hxx"
25 #include "Thread.hxx"
26 #include "Semaphore.hxx"
27 #include "Exception.hxx"
28 #include "define.hxx"
29
30 #ifdef WIN32
31 #include <windows.h>
32 #pragma comment(lib,"winmm.lib")
33 #endif
34 #include <list>
35 #include <vector>
36 #include <set>
37 #include <string>
38 #include <fstream>
39 #include <ctime>
40
41 namespace YACS
42 {
43   namespace ENGINE
44   {
45     class Scheduler;
46     class ComposedNode;
47     class Task;
48     
49 /*! \brief Threaded Executor 
50  *
51  * \ingroup Executors
52  *
53  *
54  */
55     class YACSLIBENGINE_EXPORT Executor
56     {
57     protected:
58       Scheduler *_mainSched;
59       ComposedNode *_root;
60       int _nbOfConcurrentThreads;
61       YACS::BASES::Mutex _mutexForNbOfConcurrentThreads;
62       YACS::BASES::Condition _condForNewTasksToPerform;
63       YACS::BASES::Semaphore _semForMaxThreads;
64       YACS::BASES::Condition _condForStepByStep;
65       YACS::BASES::Condition _condForPilot;
66       YACS::BASES::Mutex _mutexForSchedulerUpdate;
67       YACS::BASES::Mutex _mutexForTrace;
68       bool _toContinue;
69       bool _isOKToEnd;
70       bool _stopOnErrorRequested;
71       bool _dumpOnErrorRequested;
72       bool _errorDetected;
73       bool _isRunningunderExternalControl;
74       bool _isWaitingEventsFromRunningTasks;
75       int _numberOfRunningTasks;
76       std::set<Task *> _runningTasks; 
77       int _numberOfEndedTasks;
78       int _semThreadCnt;
79       YACS::ExecutorState _executorState;
80       YACS::ExecutionMode _execMode;
81       std::list<std::string> _listOfBreakPoints;
82       std::list<std::string> _listOfTasksToLoad;
83       std::vector<Task *> _tasks; 
84       std::vector<Task *> _tasksSave; 
85       std::list< YACS::BASES::Thread * > _groupOfAllThreadsCreated;
86       std::ofstream _trace;
87       std::string _dumpErrorFile;
88       bool _keepGoingOnFail;
89       //! specifies if scope DynParaLoop is active or not. False by default.
90       bool _DPLScopeSensitive;
91 #ifdef WIN32
92           DWORD _start;
93 #else
94       timeval _start;
95 #endif
96     public:
97       Executor();
98       virtual ~Executor();
99       void RunA(Scheduler *graph,int debug=0, bool fromScratch=true);
100       void RunW(Scheduler *graph,int debug=0, bool fromScratch=true) { RunB(graph, debug, fromScratch); }
101       void RunB(Scheduler *graph,int debug=0, bool fromScratch=true);
102       void setKeepGoingProperty(bool newVal) { _keepGoingOnFail=newVal; }
103       bool getKeepGoingProperty() const { return _keepGoingOnFail; }
104       void setDPLScopeSensitive(bool newVal) { _DPLScopeSensitive=newVal; }
105       bool getDPLScopeSensitive() const { return _DPLScopeSensitive; }
106       YACS::ExecutionMode getCurrentExecMode();
107       YACS::ExecutorState getExecutorState();
108       void setExecMode(YACS::ExecutionMode mode);
109       void setListOfBreakPoints(std::list<std::string> listOfBreakPoints);
110       std::list<std::string> getTasksToLoad();
111       bool setStepsToExecute(std::list<std::string> listToExecute);
112       bool resumeCurrentBreakPoint();
113       bool isNotFinished();
114       void stopExecution();
115       bool saveState(const std::string& xmlFile);
116       bool loadState();
117       int getNbOfThreads();
118       int getNumberOfRunningTasks() const { return _numberOfRunningTasks; }
119       void displayDot(Scheduler *graph);
120       void setStopOnError(bool dumpRequested=false, std::string xmlFile="");
121       void unsetStopOnError();
122       void waitPause();
123       bool suspendASAP();
124       void resume(bool suspended);
125       static int _maxThreads;
126       static size_t _threadStackSize;
127       YACS::BASES::Mutex& getTheMutexForSchedulerUpdate() { return _mutexForSchedulerUpdate; }
128     protected:
129       bool checkBreakPoints();
130       void waitResume();
131       void loadTask(Task *task, const Executor *execInst);
132       void loadTasks(const std::vector<Task *>& tasks, const Executor *execInst);
133       void loadParallelTasks(const std::vector<Task *>& tasks, const Executor *execInst);
134       void launchTasks(const std::vector<Task*>& tasks);
135       void launchTask(Task *task);
136       void wakeUp();
137       void sleepWhileNoEventsFromAnyRunningTask();
138       void notifyEndOfThread(YACS::BASES::Thread *thread);
139       void traceExec(Task *task, const std::string& message, const std::string& placement);
140       void _displayDot(Scheduler *graph);
141       virtual void sendEvent(const std::string& event);
142       static void FilterTasksConsideringContainers(std::vector<Task *>& tsks);
143       static std::string ComputePlacement(Task *zeTask);
144     protected:
145       static void *functionForTaskLoad(void *);
146       static void *functionForTaskExecution(void *);
147     };
148   }
149 }
150
151 #endif