Salome HOME
Updated copyright comment
[modules/yacs.git] / src / runtime / PyOptimizerAlg.cxx
1 // Copyright (C) 2006-2024  CEA, EDF
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include <iostream>
21
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
25 #endif
26
27 #ifdef _XOPEN_SOURCE
28 #undef _XOPEN_SOURCE
29 #endif
30 #include <Python.h>
31
32 #include "PyOptimizerAlg.hxx"
33
34 using namespace std;
35 using namespace YACS::BASES;
36 using namespace YACS::ENGINE;
37
38 struct GILState{
39   PyGILState_STATE gstate;
40   GILState(){gstate=PyGILState_Ensure();}
41   ~GILState(){PyGILState_Release(gstate);}
42 };
43
44
45 PyOptimizerAlgBase::PyOptimizerAlgBase(Pool *pool)
46   : OptimizerAlgBase(pool)
47 {
48 }
49
50 PyOptimizerAlgBase::~PyOptimizerAlgBase()
51 {
52 }
53
54 void PyOptimizerAlgBase::startProxy()
55 {
56   GILState gs;
57   OptimizerAlgBase::startProxy();
58 }
59
60 void PyOptimizerAlgBase::takeDecisionProxy()
61 {
62   GILState gs;
63   OptimizerAlgBase::takeDecisionProxy();
64 }
65
66 void PyOptimizerAlgBase::finishProxy()
67 {
68   GILState gs;
69   OptimizerAlgBase::finishProxy();
70 }
71
72 void PyOptimizerAlgBase::initializeProxy(const Any *input)
73 {
74   GILState gs;
75   OptimizerAlgBase::initializeProxy(input);
76 }
77
78 TypeCode * PyOptimizerAlgBase::getTCForInProxy() const
79 {
80   GILState gs;
81   return OptimizerAlgBase::getTCForInProxy();
82 }
83
84 TypeCode * PyOptimizerAlgBase::getTCForOutProxy() const
85 {
86   GILState gs;
87   return OptimizerAlgBase::getTCForOutProxy();
88 }
89
90 TypeCode * PyOptimizerAlgBase::getTCForAlgoInitProxy() const
91 {
92   GILState gs;
93   return OptimizerAlgBase::getTCForAlgoInitProxy();
94 }
95
96 TypeCode * PyOptimizerAlgBase::getTCForAlgoResultProxy() const
97 {
98   GILState gs;
99   return OptimizerAlgBase::getTCForAlgoResultProxy();
100 }
101
102 Any * PyOptimizerAlgBase::getAlgoResultProxy()
103 {
104   GILState gs;
105   return OptimizerAlgBase::getAlgoResultProxy();
106 }
107
108
109 PyOptimizerAlgASync::PyOptimizerAlgASync(Pool *pool)
110   : PyOptimizerAlgBase(pool)
111 {
112 }
113
114 PyOptimizerAlgASync::~PyOptimizerAlgASync()
115 {
116   if (getThreadStatus() == AlternateThread::UNEXISTING) return;
117
118   Py_BEGIN_ALLOW_THREADS;
119   try {
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;
125   } catch (...) {
126     cerr << "Unknown exception happened in PyOptimizerAlgASync destructor." << endl;
127   }
128   Py_END_ALLOW_THREADS;
129 }
130
131 void PyOptimizerAlgASync::startProxy()
132 {
133   start();
134 }
135
136 void PyOptimizerAlgASync::takeDecisionProxy()
137 {
138   takeDecision();
139 }
140
141 void PyOptimizerAlgASync::finishProxy()
142 {
143   terminateSlaveThread();
144   PyOptimizerAlgBase::finishProxy();
145 }
146
147 void PyOptimizerAlgASync::start()
148 {
149   AlternateThread::start();
150 }
151
152 void PyOptimizerAlgASync::takeDecision()
153 {
154   signalSlaveAndWait();
155 }
156
157 void PyOptimizerAlgASync::signalMasterAndWait()
158 {
159   Py_BEGIN_ALLOW_THREADS;
160   AlternateThread::signalMasterAndWait();
161   Py_END_ALLOW_THREADS;
162 }
163
164 void PyOptimizerAlgASync::run()
165 {
166   GILState gs;
167   startToTakeDecision();
168 }