From: abn Date: Thu, 28 Jul 2016 11:50:44 +0000 (+0200) Subject: [MEDCalc] avoiding use of PyLockWrapper in MED component ... X-Git-Tag: V8_1_0b1~17 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=af14afa2faa9842e8d4d174ffa8a6a15336c884b;p=modules%2Fmed.git [MEDCalc] avoiding use of PyLockWrapper in MED component ... --- diff --git a/src/MEDCalc/cmp/CMakeLists.txt b/src/MEDCalc/cmp/CMakeLists.txt index 489d221ce..22657a8ea 100644 --- a/src/MEDCalc/cmp/CMakeLists.txt +++ b/src/MEDCalc/cmp/CMakeLists.txt @@ -29,7 +29,6 @@ INCLUDE_DIRECTORIES( ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/.. ${PROJECT_BINARY_DIR}/idl - ${GUI_INCLUDE_DIRS} # For PyLockWrapper ) SET(COMMON_SOURCES @@ -69,7 +68,6 @@ SET(COMMON_LIBS ${OMNIORB_LIBRARIES} ${PYTHON_LIBRARIES} ${MEDCoupling_medcoupling} ${MEDCoupling_medloader} ${MEDCoupling_medcouplingremapper} - ${GUI_PyInterp} ) # This undefines the macros MIN and MAX which are specified in the windows headers diff --git a/src/MEDCalc/cmp/MEDPresentation.cxx b/src/MEDCalc/cmp/MEDPresentation.cxx index e4bd9c9ae..88c4a2fcf 100644 --- a/src/MEDCalc/cmp/MEDPresentation.cxx +++ b/src/MEDCalc/cmp/MEDPresentation.cxx @@ -18,6 +18,7 @@ // // Authors: A Bruneton (CEA), C Aguerre (EdF) +#include "MEDPyLockWrapper.hxx" #include "MEDFactoryClient.hxx" #include "MEDPresentation.hxx" #include "MEDPresentationException.hxx" @@ -25,7 +26,6 @@ #include #undef LOG #include -#include #include @@ -80,7 +80,7 @@ MEDPresentation::~MEDPresentation() { STDLOG("~MEDPresentation(): clear display"); { - PyLockWrapper lock; + MEDPyLockWrapper lock; std::ostringstream oss_o, oss_v, oss; oss_o << "__obj" << _objId; oss_v << "__view" << _renderViewPyId; @@ -116,7 +116,7 @@ MEDPresentation::pushAndExecPyLine(const std::string & lin) void MEDPresentation::execPyLine(const std::string & lin) { - PyLockWrapper lock; + MEDPyLockWrapper lock; // STDLOG("@@@@ MEDPresentation::execPyLine() about to exec >> " << lin); if(PyRun_SimpleString(lin.c_str())) { @@ -196,7 +196,7 @@ MEDPresentation::getIntProperty(const std::string& propName) const void MEDPresentation::internalGeneratePipeline() { - PyLockWrapper lock; + MEDPyLockWrapper lock; pushAndExecPyLine( "import pvsimple as pvs;"); } @@ -328,7 +328,7 @@ MEDPresentation::GeneratePythonId() void MEDPresentation::activateView() { - PyLockWrapper lock; + MEDPyLockWrapper lock; std::ostringstream oss; oss << "pvs.SetActiveView(__view" << _renderViewPyId << ");"; @@ -360,7 +360,7 @@ MEDPresentation::paravisDump() const void MEDPresentation::fillAvailableFieldComponents() { - PyLockWrapper lock; // GIL! + MEDPyLockWrapper lock; // GIL! std::ostringstream oss_o; oss_o << "__obj" << _objId; std::string obj(oss_o.str()); diff --git a/src/MEDCalc/cmp/MEDPresentation.hxx b/src/MEDCalc/cmp/MEDPresentation.hxx index 511abb442..cfb974a4b 100644 --- a/src/MEDCalc/cmp/MEDPresentation.hxx +++ b/src/MEDCalc/cmp/MEDPresentation.hxx @@ -21,8 +21,8 @@ #ifndef SRC_MEDCALC_CMP_MEDPRESENTATION_HXX_ #define SRC_MEDCALC_CMP_MEDPRESENTATION_HXX_ -#include "MEDCouplingRefCountObject.hxx" #include +#include "MEDCouplingRefCountObject.hxx" #include "MEDCALC.hxx" #include diff --git a/src/MEDCalc/cmp/MEDPresentation.txx b/src/MEDCalc/cmp/MEDPresentation.txx index f4100e77b..0e061fc20 100644 --- a/src/MEDCalc/cmp/MEDPresentation.txx +++ b/src/MEDCalc/cmp/MEDPresentation.txx @@ -20,9 +20,9 @@ #ifndef _MED_PRESENTATION_TXX_ #define _MED_PRESENTATION_TXX_ +#include "MEDPyLockWrapper.hxx" #include #include -#include template void @@ -89,7 +89,7 @@ MEDPresentation::updateComponent(const std::string& newCompo) // Update ParaView pipeline: { - PyLockWrapper lock; + MEDPyLockWrapper lock; std::ostringstream oss; std::string cmd = getComponentSelectionCommand(); @@ -117,7 +117,7 @@ MEDPresentation::updateColorMap(MEDCALC::MEDPresentationColorMap colorMap) // Update the pipeline: { - PyLockWrapper lock; + MEDPyLockWrapper lock; std::string cmd = getColorMapCommand(); pushAndExecPyLine(cmd); pushAndExecPyLine("pvs.Render();"); @@ -142,7 +142,7 @@ MEDPresentation::updateScalarBarRange(MEDCALC::MEDPresentationScalarBarRange sbR // Update the pipeline: { - PyLockWrapper lock; + MEDPyLockWrapper lock; std::string cmd = getRescaleCommand(); pushAndExecPyLine(cmd); pushAndExecPyLine("pvs.Render();"); diff --git a/src/MEDCalc/cmp/MEDPresentationScalarMap.cxx b/src/MEDCalc/cmp/MEDPresentationScalarMap.cxx index 9c0181b3e..573a34b39 100644 --- a/src/MEDCalc/cmp/MEDPresentationScalarMap.cxx +++ b/src/MEDCalc/cmp/MEDPresentationScalarMap.cxx @@ -17,10 +17,11 @@ // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com // +#include "MEDPyLockWrapper.hxx" + #include "MEDPresentationScalarMap.hxx" #include "MEDPresentationException.hxx" -#include #include #undef LOG // should be fixed in KERNEL - double definition #include @@ -40,7 +41,7 @@ MEDPresentationScalarMap::internalGeneratePipeline() { MEDPresentation::internalGeneratePipeline(); - PyLockWrapper lock; + MEDPyLockWrapper lock; std::ostringstream oss_o, oss_d,oss_l, oss, oss_v; oss_o << "__obj" << _objId; std::string obj(oss_o.str()); diff --git a/src/MEDCalc/cmp/MEDPyLockWrapper.hxx b/src/MEDCalc/cmp/MEDPyLockWrapper.hxx new file mode 100644 index 000000000..cf231f2d6 --- /dev/null +++ b/src/MEDCalc/cmp/MEDPyLockWrapper.hxx @@ -0,0 +1,76 @@ +// Copyright (C) 2011-2016 CEA/DEN, EDF R&D +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com +// + +#ifndef SRC_MEDCALC_CMP_MEDPYLOCKWRAPPER_HXX_ +#define SRC_MEDCALC_CMP_MEDPYLOCKWRAPPER_HXX_ + +#include + +#ifdef _DEBUG_ + #include +#endif + +/** + * \class MEDPyLockWrapper + * \brief Python GIL wrapper. Copy of GUI's PyLockWrapper. + * + * Utility class wrapping the Python GIL acquisition. This makes use of the high level + * API (PyGILState_Ensure and PyGILState_Release), and is hence compatible with only + * one running Python interpreter (no call to Py_NewInterpreter()). + * When the class is instanciated the lock is acquired. It is released at destruction time. + * Copy construction (and hence assignation) is forbidden. + */ +class MEDPyLockWrapper +{ +public: + /** + * \brief Constructor. Automatically acquires GIL. + */ + MEDPyLockWrapper() + { + _gil_state = PyGILState_Ensure(); + // Save current thread state for later comparison + _state = PyGILState_GetThisThreadState(); + } + + /** + * \brief Destructor. Automatically releases GIL. + */ + ~MEDPyLockWrapper() + { + PyThreadState* _currState = PyGILState_GetThisThreadState(); +#ifdef _DEBUG_ + if (_currState != _state) + { + std::cout << "!!!!!!!!! MEDPyLockWrapper inconsistency - now entering infinite loop for debugging\n"; + while(1); + } +#endif + PyGILState_Release(_gil_state); + } + +private: + PyGILState_STATE _gil_state; + PyThreadState* _state; + + // "Rule of 3" - Forbid usage of copy operator and copy-constructor + MEDPyLockWrapper(const MEDPyLockWrapper & another); + const MEDPyLockWrapper & operator=(const MEDPyLockWrapper & another); +}; +#endif /* SRC_MEDCALC_CMP_MEDPYLOCKWRAPPER_HXX_ */