1 // Copyright (C) 2006-2014 CEA/DEN, EDF R&D
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
22 // rnv: avoid compilation warning on Linux : "_POSIX_C_SOURCE" and "_XOPEN_SOURCE" are redefined
23 #ifdef _POSIX_C_SOURCE
24 #undef _POSIX_C_SOURCE
32 #include "PyOptimizerAlg.hxx"
35 using namespace YACS::BASES;
36 using namespace YACS::ENGINE;
39 PyGILState_STATE gstate;
40 GILState(){gstate=PyGILState_Ensure();}
41 ~GILState(){PyGILState_Release(gstate);}
45 PyOptimizerAlgBase::PyOptimizerAlgBase(Pool *pool)
46 : OptimizerAlgBase(pool)
50 PyOptimizerAlgBase::~PyOptimizerAlgBase()
54 void PyOptimizerAlgBase::startProxy()
57 OptimizerAlgBase::startProxy();
60 void PyOptimizerAlgBase::takeDecisionProxy()
63 OptimizerAlgBase::takeDecisionProxy();
66 void PyOptimizerAlgBase::finishProxy()
69 OptimizerAlgBase::finishProxy();
72 void PyOptimizerAlgBase::initializeProxy(const Any *input) throw (YACS::Exception)
75 OptimizerAlgBase::initializeProxy(input);
78 TypeCode * PyOptimizerAlgBase::getTCForInProxy() const
81 return OptimizerAlgBase::getTCForInProxy();
84 TypeCode * PyOptimizerAlgBase::getTCForOutProxy() const
87 return OptimizerAlgBase::getTCForOutProxy();
90 TypeCode * PyOptimizerAlgBase::getTCForAlgoInitProxy() const
93 return OptimizerAlgBase::getTCForAlgoInitProxy();
96 TypeCode * PyOptimizerAlgBase::getTCForAlgoResultProxy() const
99 return OptimizerAlgBase::getTCForAlgoResultProxy();
102 Any * PyOptimizerAlgBase::getAlgoResultProxy()
105 return OptimizerAlgBase::getAlgoResultProxy();
109 PyOptimizerAlgASync::PyOptimizerAlgASync(Pool *pool)
110 : PyOptimizerAlgBase(pool)
114 PyOptimizerAlgASync::~PyOptimizerAlgASync()
116 if (getThreadStatus() == AlternateThread::UNEXISTING) return;
118 Py_BEGIN_ALLOW_THREADS;
120 // Force the thread termination. We must allow Python threads for that because the
121 // thread cleanup includes some Python cleanup.
122 terminateSlaveThread();
123 } catch (const exception & e) {
124 cerr << "Exception happened in PyOptimizerAlgASync destructor: " << e.what() << endl;
126 cerr << "Unknown exception happened in PyOptimizerAlgASync destructor." << endl;
128 Py_END_ALLOW_THREADS;
131 void PyOptimizerAlgASync::startProxy()
136 void PyOptimizerAlgASync::takeDecisionProxy()
141 void PyOptimizerAlgASync::finishProxy()
143 terminateSlaveThread();
144 PyOptimizerAlgBase::finishProxy();
147 void PyOptimizerAlgASync::start()
149 AlternateThread::start();
152 void PyOptimizerAlgASync::takeDecision()
154 signalSlaveAndWait();
157 void PyOptimizerAlgASync::signalMasterAndWait()
159 Py_BEGIN_ALLOW_THREADS;
160 AlternateThread::signalMasterAndWait();
161 Py_END_ALLOW_THREADS;
164 void PyOptimizerAlgASync::run()
167 startToTakeDecision();