From: prascle Date: Fri, 20 May 2005 14:31:45 +0000 (+0000) Subject: PR: compatibility with Supervision, use new Python thread states for safety X-Git-Tag: BR_V220_LifeCycle_20050520~1 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=8d37bfef00c03e647eaf35594c0ca38e7eeed2a6;p=modules%2Fkernel.git PR: compatibility with Supervision, use new Python thread states for safety --- diff --git a/src/Container/Component_i.cxx b/src/Container/Component_i.cxx index fb2dc2b68..fd11e240b 100644 --- a/src/Container/Component_i.cxx +++ b/src/Container/Component_i.cxx @@ -56,7 +56,8 @@ bool Engines_Component_i::_isMultiInstance = false; Engines_Component_i::Engines_Component_i() { - ASSERT(0); + //ASSERT(0); + INFOS("Default Constructor..."); } //============================================================================= diff --git a/src/Container/Container_i.cxx b/src/Container/Container_i.cxx index 76d135641..59671af74 100644 --- a/src/Container/Container_i.cxx +++ b/src/Container/Container_i.cxx @@ -56,25 +56,6 @@ char ** _ArgV ; extern "C" {void ActSigIntHandler() ; } extern "C" {void SigIntHandler(int, siginfo_t *, void *) ; } - -// static PyMethodDef MethodPyVoidMethod[] = {{ NULL, NULL }}; -// PyThreadState *gtstate; - -// void init_python(int argc, char **argv) -// { -// if (gtstate) -// return; -// Py_SetProgramName(argv[0]); -// Py_Initialize(); // Initialize the interpreter -// PySys_SetArgv(argc, argv); -// PyEval_InitThreads(); // Create (and acquire) the interpreter lock -// Py_InitModule( "InitPyRunMethod" , MethodPyVoidMethod ) ; -// //PyOS_setsig(SIGSEGV,&Handler); -// //PyOS_setsig(SIGINT,&Handler); -// gtstate = PyEval_SaveThread(); // Release the global thread state -// SCRUTE(gtstate); -// } - const char *Engines_Container_i::_defaultContainerName="FactoryServer"; map Engines_Container_i::_cntInstances_map; map Engines_Container_i::_library_map; @@ -136,12 +117,15 @@ Engines_Container_i::Engines_Container_i (CORBA::ORB_ptr orb, MESSAGE(" argv" << i << " " << _argv[ i ]) ; i++ ; } - if ( argc != 4 ) + + if ( argc < 2 ) { - MESSAGE("SALOME_Container usage : SALOME_Container ServerName " << - "-ORBInitRef NameService=corbaname::hostname:tcpipPortNumber") ; - // exit(0) ; + INFOS("SALOME_Container usage : SALOME_Container ServerName"); + ASSERT(0) ; } + SCRUTE(argv[1]); + _isSupervContainer = false; + if (strcmp(argv[1],"SuperVisionContainer") == 0) _isSupervContainer = true; _containerName = BuildContainerNameForNS(containerName,hostname.c_str()); @@ -173,11 +157,13 @@ Engines_Container_i::Engines_Container_i (CORBA::ORB_ptr orb, myCommand += "')\n"; SCRUTE(myCommand); - SCRUTE(KERNEL_PYTHON::_gtstate); - PyEval_RestoreThread(KERNEL_PYTHON::_gtstate); - PyRun_SimpleString("import SALOME_Container\n"); - PyRun_SimpleString((char*)myCommand.c_str()); - PyEval_ReleaseThread(KERNEL_PYTHON::_gtstate); + if (!_isSupervContainer) + { + Py_ACQUIRE_NEW_THREAD; + PyRun_SimpleString("import SALOME_Container\n"); + PyRun_SimpleString((char*)myCommand.c_str()); + Py_RELEASE_NEW_THREAD; + } } } @@ -308,14 +294,18 @@ Engines_Container_i::load_component_Library(const char* componentName) // --- try import Python component INFOS("try import Python component "<string_to_object(iors.c_str()); iobject = Engines::Component::_narrow( obj ) ; diff --git a/src/Container/Container_init_python.cxx b/src/Container/Container_init_python.cxx index 1aac1d05c..7a4e58527 100644 --- a/src/Container/Container_init_python.cxx +++ b/src/Container/Container_init_python.cxx @@ -33,6 +33,7 @@ using namespace std; PyThreadState *KERNEL_PYTHON::_gtstate = 0; PyObject *KERNEL_PYTHON::salome_shared_modules_module = NULL; +PyInterpreterState *KERNEL_PYTHON::_interp = NULL; void KERNEL_PYTHON::init_python(int argc, char **argv) { @@ -48,6 +49,7 @@ void KERNEL_PYTHON::init_python(int argc, char **argv) Py_SetProgramName(argv[0]); Py_Initialize(); // Initialize the interpreter PySys_SetArgv(argc, argv); + KERNEL_PYTHON::_interp = PyThreadState_Get()->interp; PyEval_InitThreads(); // Create (and acquire) the interpreter lock ASSERT(!KERNEL_PYTHON::_gtstate); KERNEL_PYTHON::_gtstate = PyEval_SaveThread(); // Release global thread state diff --git a/src/Container/Container_init_python.hxx b/src/Container/Container_init_python.hxx index c28e99cd5..acaef6307 100644 --- a/src/Container/Container_init_python.hxx +++ b/src/Container/Container_init_python.hxx @@ -32,10 +32,32 @@ #include // must be before Python.h ! #include + +// next two MACRO must be used together only once inside a block +// ------------------------------------------------------------- +// protect a sequence of Python calls: +// - Python lock must be acquired for these calls +// - new Python thread state allows multi thread use of the sequence: +// - Python may release the lock within the sequence, so multiple +// thread execution of the sequence may occur. +// - For that case, each sequence call must use a specific Python +// thread state. +// - There is no need of C Lock protection of the sequence. + +#define Py_ACQUIRE_NEW_THREAD \ + PyEval_AcquireLock(); \ + PyThreadState *myTstate = PyThreadState_New(KERNEL_PYTHON::_interp); \ + PyThreadState *myoldTstate = PyThreadState_Swap(myTstate); + +#define Py_RELEASE_NEW_THREAD \ + PyEval_ReleaseThread(myTstate); \ + PyThreadState_Delete(myTstate); + struct KERNEL_PYTHON { static PyThreadState *_gtstate; static PyObject *salome_shared_modules_module; + static PyInterpreterState *_interp; static void init_python(int argc, char **argv); diff --git a/src/Container/SALOME_Container.cxx b/src/Container/SALOME_Container.cxx index 174bc4a00..f6bb9b0fe 100644 --- a/src/Container/SALOME_Container.cxx +++ b/src/Container/SALOME_Container.cxx @@ -59,16 +59,28 @@ int main(int argc, char* argv[]) MPI_Init(&argc,&argv); #endif // Initialise the ORB. - //ORB_INIT &init = *SINGLETON_::Instance() ; CORBA::ORB_var orb = CORBA::ORB_init( argc , argv ) ; SALOMETraceCollector *myThreadTrace = SALOMETraceCollector::instance(orb); INFOS_COMPILATION; BEGIN_OF(argv[0]); - int _argc = 1; - char* _argv[] = {""}; - KERNEL_PYTHON::init_python(_argc,_argv); - + ASSERT(argc > 1); + SCRUTE(argv[1]); + bool isSupervContainer = false; + if (strcmp(argv[1],"SuperVisionContainer") == 0) isSupervContainer = true; + + if (!isSupervContainer) + { + int _argc = 1; + char* _argv[] = {""}; + KERNEL_PYTHON::init_python(argc,argv); + } + else + { + Py_Initialize() ; + PySys_SetArgv( argc , argv ) ; + } + try{ // Obtain a reference to the root POA. // obtain the root poa manager diff --git a/src/Container/SALOME_Container_i.hxx b/src/Container/SALOME_Container_i.hxx index a3840cfb6..06e1f1aa8 100644 --- a/src/Container/SALOME_Container_i.hxx +++ b/src/Container/SALOME_Container_i.hxx @@ -112,6 +112,8 @@ protected: static std::map _toRemove_map;// library names to remove static omni_mutex _numInstanceMutex ; // lib and instance protection + bool _isSupervContainer; + SALOME_NamingService *_NS ; std::string _library_path; std::string _containerName;