Salome HOME
Fixed another bug with threads in BatchManager_Local: no more segfault when destroyin...
[tools/libbatch.git] / src / Local / Batch_BatchManager_Local.hxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 /*
23  * BatchManager_Local.hxx :
24  *
25  * Auteur : Ivan DUTKA-MALEN - EDF R&D
26  * Mail   : mailto:ivan.dutka-malen@der.edf.fr
27  * Date   : Thu Nov  6 10:17:22 2003
28  * Projet : Salome 2
29  *
30  * Refactored by Renaud Barate (EDF R&D) in September 2009 to use
31  * CommunicationProtocol classes and merge Local_SH, Local_RSH and Local_SSH batch
32  * managers.
33  *
34  */
35
36 #ifndef _BATCHMANAGER_LOCAL_H_
37 #define _BATCHMANAGER_LOCAL_H_
38
39 #include "Batch_Defines.hxx"
40 #include "Batch_CommunicationProtocol.hxx"
41
42 #ifdef WIN32
43 #include <Windows.h>
44 #endif
45
46 #include <list>
47 #include <map>
48 #include <queue>
49 #include <pthread.h>
50 #include "Batch_Job.hxx"
51 #include "Batch_JobId.hxx"
52 #include "Batch_JobInfo.hxx"
53 #include "Batch_JobInfo_Local.hxx"
54 #include "Batch_Job_Local.hxx"
55 #include "Batch_BatchManager.hxx"
56
57 namespace Batch {
58
59   class FactBatchManager;
60
61   /*!
62    *  This class defines a local pseudo batch manager that can launch jobs locally or on a remote
63    *  machine with SSH or RSH. This class is NOT thread-safe.
64    */
65   class BATCH_EXPORT BatchManager_Local : virtual public BatchManager
66   {
67   private:
68     typedef int Id;
69 #ifdef WIN32
70     typedef HANDLE Process;
71 #else
72     typedef pid_t Process;
73 #endif
74     friend class ThreadAdapter;
75     class ThreadAdapter{
76     public:
77       ThreadAdapter(BatchManager_Local & bm, const Job_Local & job, Id id);
78       static void * run(void * arg);
79       BatchManager_Local & getBatchManager() const { return _bm; };
80       Id getId() const { return _id; };
81
82     protected:
83       BatchManager_Local & _bm;
84       const Job_Local _job;
85       Id _id;
86
87     private:
88       void pere(Process child);
89 #ifndef WIN32
90       void fils();
91 #else
92       Process launchWin32ChildProcess();
93 #endif
94
95     };
96
97     enum Commande {
98       NOP = 0,
99       HOLD,
100       RELEASE,
101       TERM,
102       KILL,
103       ALTER
104     };
105
106     struct Child {
107       pthread_t thread_id;
108       std::queue<Commande, std::deque<Commande> > command_queue;
109       pid_t pid;
110       int exit_code;
111       bool hasFailed;
112       Parametre param;
113       Environnement env;
114     };
115
116
117
118   public:
119     // Constructeur et destructeur
120     BatchManager_Local(const FactBatchManager * parent,
121                        const char * host="localhost",
122                        CommunicationProtocolType protocolType = SSH); // connexion a la machine host
123     virtual ~BatchManager_Local();
124
125     // Recupere le nom du serveur par defaut
126     // static string BatchManager_Local::getDefaultServer();
127
128     // Get the underlying communication protocol
129     const CommunicationProtocol & getProtocol() const;
130
131     // Methodes pour le controle des jobs
132     virtual const JobId submitJob(const Job & job); // soumet un job au gestionnaire
133     virtual void deleteJob(const JobId & jobid); // retire un job du gestionnaire
134     virtual void holdJob(const JobId & jobid); // suspend un job en file d'attente
135     virtual void releaseJob(const JobId & jobid); // relache un job suspendu
136     virtual void alterJob(const JobId & jobid, const Parametre & param, const Environnement & env); // modifie un job en file d'attente
137     virtual void alterJob(const JobId & jobid, const Parametre & param); // modifie un job en file d'attente
138     virtual void alterJob(const JobId & jobid, const Environnement & env); // modifie un job en file d'attente
139     virtual JobInfo queryJob(const JobId & jobid); // renvoie l'etat du job
140     virtual bool isRunning(const JobId & jobid); // teste si un job est present en machine
141
142     virtual void setParametre(const JobId & jobid, const Parametre & param) { return alterJob(jobid, param); } // modifie un job en file d'attente
143     virtual void setEnvironnement(const JobId & jobid, const Environnement & env) { return alterJob(jobid, env); } // modifie un job en file d'attente
144
145
146   protected:
147     int _connect; // Local connect id
148     pthread_mutex_t _threads_mutex;
149     std::map<Id, Child > _threads;
150
151     const CommunicationProtocol & _protocol;
152
153     // Methode qui renvoie la commande a executer
154     std::vector<std::string> exec_command(const Parametre & param) const;
155
156   private:
157     virtual void cancel(pthread_t thread_id);
158     static  void kill_child_on_exit(void * p_pid);
159     static  void delete_on_exit(void * arg);
160     static void setFailedOnCancel(void * arg);
161     pthread_cond_t _threadSyncCondition;
162     Id _idCounter;
163
164 #ifdef SWIG
165   public:
166     // Recupere le l'identifiant d'un job deja soumis au BatchManager
167     //virtual const JobId getJobIdByReference(const string & ref) { return BatchManager::getJobIdByReference(ref); }
168     virtual const JobId getJobIdByReference(const char * ref) { return BatchManager::getJobIdByReference(ref); }
169 #endif
170
171   };
172
173 }
174
175 #endif