]> SALOME platform Git repositories - modules/kernel.git/blob - src/LifeCycleCORBA/Launchers.cxx
Salome HOME
PR: merge from branch BR_auto_V310 tag mergefrom_OCC_development_for_3_2_0a2_10mar06
[modules/kernel.git] / src / LifeCycleCORBA / Launchers.cxx
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 // 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either 
7 // version 2.1 of the License.
8 // 
9 // This library is distributed in the hope that it will be useful 
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12 // Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public  
15 // License along with this library; if not, write to the Free Software 
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 //
18 // See http://www.salome-platform.org/
19 //
20
21 #include <time.h>
22 #include <sys/time.h>
23
24 #include "utilities.h"
25
26 #include <Launchers.hxx>
27
28 using namespace std;
29
30 static int Launchers_IsLoaded = 0;
31 static PyObject * Launchers_module = 0;
32 static char * Launchers_name="Launchers";
33
34 PyThreadState *mainThreadState=0;
35
36 PyThreadState * getMainThreadState(){
37   PyInterpreterState *interp;
38   PyThreadState *p;
39   interp=PyInterpreterState_Head();
40   for (interp = PyInterpreterState_Head(); interp != NULL; ){
41     if(interp->next == NULL)break;
42     interp=interp->next;
43   }
44   for (p = interp->tstate_head; p != NULL; ){
45     if(p->next == NULL)break;
46     p=p->next;
47   }
48   return p;
49 }
50 PyThreadState *acquireMainThread(){
51     PyEval_AcquireLock();
52     return  PyThreadState_Swap(mainThreadState);
53 }
54 void releaseMainThread(PyThreadState *tstate){
55     PyThreadState_Swap(tstate);
56     PyEval_ReleaseLock();
57 }
58
59 void Launchers_assertInitialized() {
60   MESSAGE("===========================================================");
61   MESSAGE("Launchers_assertInitialized");
62   MESSAGE("===========================================================");
63    PyThreadState *_save; 
64    if( !Py_IsInitialized() ) {
65      MESSAGE("===========================================================");
66      MESSAGE("Py_Initialize()");
67      MESSAGE("===========================================================");
68          Py_Initialize();
69          PyEval_InitThreads();
70          PyEval_SaveThread();
71    }
72    if( !Launchers_IsLoaded ) {
73       mainThreadState=getMainThreadState();
74       _save=acquireMainThread();
75       Launchers_module=PyImport_ImportModule(Launchers_name);
76       if(!Launchers_module){
77         PyErr_Print();
78         return;
79       }
80       Py_INCREF(Launchers_module);
81       Launchers_IsLoaded = 1;
82       releaseMainThread(_save);
83    }
84 }
85
86 PyObject * getLauncher(char* arg0) {
87     PyObject *v;
88     PyThreadState *_save; 
89     Launchers_assertInitialized();
90     _save=acquireMainThread();
91     v=PyObject_CallMethod(Launchers_module,"getLauncher","s",arg0);
92     if(!v){
93       PyErr_Print();
94       return NULL;
95     }
96     releaseMainThread(_save);
97     Py_INCREF(v);
98     return v;
99 }
100
101 void Launcher_Slaunch(PyObject *self, char* arg1, char* arg2) {
102     PyObject *v;
103     PyThreadState *_save; 
104     Launchers_assertInitialized();
105     _save=acquireMainThread();
106     v=PyObject_CallMethod(self,"Slaunch","ss",arg1,arg2);
107     if(!v){
108       PyErr_Print();
109     }
110     releaseMainThread(_save);
111 }
112