Salome HOME
Porting to Python 2.6 - add coding page specification for Python scripts
[modules/kernel.git] / src / Container / Container_init_python.hxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //  SALOME Container : implementation of container and engine for Kernel
23 //  File   : Container_init_python.hxx
24 //  Author : Paul RASCLE, EDF
25 //  Module : KERNEL
26 //  $Header$
27 //
28 #ifndef _CONTAINER_INIT_PYTHON_HXX_
29 #define _CONTAINER_INIT_PYTHON_HXX_
30
31 #include "SALOME_Container.hxx"
32 #include <SALOMEconfig.h>
33
34 #include <pthread.h>  // must be before Python.h !
35 #include <Python.h>
36
37
38 // next two MACRO must be used together only once inside a block
39 // -------------------------------------------------------------
40 // protect a sequence of Python calls:
41 // - Python lock must be acquired for these calls
42 // - new Python thread state allows multi thread use of the sequence:
43 //    - Python may release the lock within the sequence, so multiple
44 //      thread execution of the sequence may occur.
45 //    - For that case, each sequence call must use a specific Python
46 //      thread state.
47 //    - There is no need of C Lock protection of the sequence.
48
49
50 #define Py_ACQUIRE_NEW_THREAD \
51   PyEval_AcquireLock(); \
52   PyThreadState *myTstate = PyThreadState_New(KERNEL_PYTHON::_interp); \
53   PyThreadState_Swap(myTstate);
54
55 #define Py_RELEASE_NEW_THREAD \
56   PyEval_ReleaseThread(myTstate); \
57   PyThreadState_Delete(myTstate);
58
59 struct CONTAINER_EXPORT KERNEL_PYTHON
60 {
61 #ifdef WIN32
62   static PyThreadState *get_gtstate() { return KERNEL_PYTHON::_gtstate; }
63   static PyObject *getsalome_shared_modules_module() { return KERNEL_PYTHON::salome_shared_modules_module; }
64   static PyInterpreterState *get_interp() { return KERNEL_PYTHON::_interp; }
65 #endif
66   static PyThreadState *_gtstate;
67   static PyObject *salome_shared_modules_module;
68   static PyInterpreterState *_interp;
69
70   static void init_python(int argc, char **argv);
71
72 };
73
74 #endif