From c758b97a71a49903e3a5a5e8996e25ead238b4e4 Mon Sep 17 00:00:00 2001 From: caremoli Date: Wed, 9 Sep 2009 11:20:21 +0000 Subject: [PATCH] CCAR: replace the old way to take the python global interpreter lock by calls to PyGILState_Ensure/PyGILState_Release in C++ container so that GIL management be coherent from GUI embedded interpreter to embedded python container. --- src/Container/Container_i.cxx | 37 ++++++++++++++--------------------- 1 file changed, 15 insertions(+), 22 deletions(-) diff --git a/src/Container/Container_i.cxx b/src/Container/Container_i.cxx index 6db3ab07a..eaa03a5ad 100644 --- a/src/Container/Container_i.cxx +++ b/src/Container/Container_i.cxx @@ -83,6 +83,8 @@ map Engines_Container_i::_library_map; map Engines_Container_i::_toRemove_map; omni_mutex Engines_Container_i::_numInstanceMutex ; +static PyObject* _pyCont; + int checkifexecutable(const std::string&); int findpathof(std::string&, const std::string&); @@ -194,14 +196,7 @@ Engines_Container_i::Engines_Container_i (CORBA::ORB_ptr orb, if (!_isSupervContainer) { -#ifdef WIN32 - - PyEval_AcquireLock(); - PyThreadState *myTstate = PyThreadState_New(KERNEL_PYTHON::_interp); - PyThreadState *myoldTstate = PyThreadState_Swap(myTstate); -#else - Py_ACQUIRE_NEW_THREAD; -#endif + PyGILState_STATE gstate = PyGILState_Ensure(); #ifdef WIN32 // mpv: this is temporary solution: there is a unregular crash if not @@ -213,7 +208,11 @@ Engines_Container_i::Engines_Container_i (CORBA::ORB_ptr orb, #endif PyRun_SimpleString("import SALOME_Container\n"); PyRun_SimpleString((char*)myCommand.c_str()); - Py_RELEASE_NEW_THREAD; + PyObject *mainmod = PyImport_AddModule("__main__"); + PyObject *globals = PyModule_GetDict(mainmod); + _pyCont = PyDict_GetItemString(globals, "pyCont"); + + PyGILState_Release(gstate); } fileTransfer_i* aFileTransfer = new fileTransfer_i(); @@ -555,23 +554,20 @@ Engines_Container_i::load_component_Library(const char* componentName) } else { - Py_ACQUIRE_NEW_THREAD; - PyObject *mainmod = PyImport_AddModule("__main__"); - PyObject *globals = PyModule_GetDict(mainmod); - PyObject *pyCont = PyDict_GetItemString(globals, "pyCont"); - PyObject *result = PyObject_CallMethod(pyCont, + PyGILState_STATE gstate = PyGILState_Ensure(); + PyObject *result = PyObject_CallMethod(_pyCont, (char*)"import_component", (char*)"s",componentName); retpy=PyString_AsString(result); Py_XDECREF(result); SCRUTE(retpy); - Py_RELEASE_NEW_THREAD; + PyGILState_Release(gstate); if (retpy=="") // import possible: Python component { _numInstanceMutex.lock() ; // lock to be alone (stl container write) - _library_map[aCompName] = (void *)pyCont; // any non O value OK + _library_map[aCompName] = (void *)_pyCont; // any non O value OK _numInstanceMutex.unlock() ; MESSAGE("import Python: "<