Back to index

salome-kernel  6.5.0
Container_init_python.hxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 //  SALOME Container : implementation of container and engine for Kernel
00024 //  File   : Container_init_python.hxx
00025 //  Author : Paul RASCLE, EDF
00026 //  Module : KERNEL
00027 //  $Header: /home/server/cvs/KERNEL/KERNEL_SRC/src/Container/Container_init_python.hxx,v 1.5.32.3.2.1 2012-04-12 14:05:03 vsr Exp $
00028 //
00029 #ifndef _CONTAINER_INIT_PYTHON_HXX_
00030 #define _CONTAINER_INIT_PYTHON_HXX_
00031 
00032 #include "SALOME_Container.hxx"
00033 #include <SALOMEconfig.h>
00034 
00035 #include <pthread.h>  // must be before Python.h !
00036 
00037 #ifdef _XOPEN_SOURCE
00038 #undef _XOPEN_SOURCE
00039 #endif
00040 #include <Python.h>
00041 
00042 
00043 // next two MACRO must be used together only once inside a block
00044 // -------------------------------------------------------------
00045 // protect a sequence of Python calls:
00046 // - Python lock must be acquired for these calls
00047 // - new Python thread state allows multi thread use of the sequence:
00048 //    - Python may release the lock within the sequence, so multiple
00049 //      thread execution of the sequence may occur.
00050 //    - For that case, each sequence call must use a specific Python
00051 //      thread state.
00052 //    - There is no need of C Lock protection of the sequence.
00053 
00054 
00055 #define Py_ACQUIRE_NEW_THREAD \
00056   PyEval_AcquireLock(); \
00057   PyThreadState *myTstate = PyThreadState_New(KERNEL_PYTHON::_interp); \
00058   PyThreadState_Swap(myTstate);
00059 
00060 #define Py_RELEASE_NEW_THREAD \
00061   PyEval_ReleaseThread(myTstate); \
00062   PyThreadState_Delete(myTstate);
00063 
00064 struct CONTAINER_EXPORT KERNEL_PYTHON
00065 {
00066 #ifdef WIN32
00067   static PyThreadState *get_gtstate() { return KERNEL_PYTHON::_gtstate; }
00068   static PyObject *getsalome_shared_modules_module() { return KERNEL_PYTHON::salome_shared_modules_module; }
00069   static PyInterpreterState *get_interp() { return KERNEL_PYTHON::_interp; }
00070 #endif
00071   static PyThreadState *_gtstate;
00072   static PyObject *salome_shared_modules_module;
00073   static PyInterpreterState *_interp;
00074 
00075   static void init_python(int argc, char **argv);
00076 
00077 };
00078 
00079 #endif