1 // Copyright (C) 2011-2024 CEA, EDF
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #ifndef SRC_MEDCALC_CMP_MEDPYLOCKWRAPPER_HXX_
21 #define SRC_MEDCALC_CMP_MEDPYLOCKWRAPPER_HXX_
30 * \class MEDPyLockWrapper
31 * \brief Python GIL wrapper. Copy of GUI's PyLockWrapper.
33 * Utility class wrapping the Python GIL acquisition. This makes use of the high level
34 * API (PyGILState_Ensure and PyGILState_Release), and is hence compatible with only
35 * one running Python interpreter (no call to Py_NewInterpreter()).
36 * When the class is instantiated the lock is acquired. It is released at destruction time.
37 * Copy construction (and hence assignation) is forbidden.
39 class MEDPyLockWrapper
43 * \brief Constructor. Automatically acquires GIL.
47 _gil_state = PyGILState_Ensure();
48 // Save current thread state for later comparison
49 _state = PyGILState_GetThisThreadState();
53 * \brief Destructor. Automatically releases GIL.
58 if (PyGILState_GetThisThreadState() != _state)
60 std::cout << "!!!!!!!!! MEDPyLockWrapper inconsistency - now entering infinite loop for debugging\n";
64 PyGILState_Release(_gil_state);
68 PyGILState_STATE _gil_state;
69 PyThreadState* _state;
71 // "Rule of 3" - Forbid usage of copy operator and copy-constructor
72 MEDPyLockWrapper(const MEDPyLockWrapper & another);
73 const MEDPyLockWrapper & operator=(const MEDPyLockWrapper & another);
75 #endif /* SRC_MEDCALC_CMP_MEDPYLOCKWRAPPER_HXX_ */