Version: 8.3.0
PyOptimizerAlg.cxx
Go to the documentation of this file.
1 // Copyright (C) 2006-2016 CEA/DEN, EDF R&D
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 
51 {
52 }
53 
55 {
56  GILState gs;
58 }
59 
61 {
62  GILState gs;
64 }
65 
67 {
68  GILState gs;
70 }
71 
73 {
74  GILState gs;
76 }
77 
79 {
80  GILState gs;
82 }
83 
85 {
86  GILState gs;
88 }
89 
91 {
92  GILState gs;
94 }
95 
97 {
98  GILState gs;
100 }
101 
103 {
104  GILState gs;
106 }
107 
108 
110  : PyOptimizerAlgBase(pool)
111 {
112 }
113 
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 
132 {
133  start();
134 }
135 
137 {
138  takeDecision();
139 }
140 
142 {
143  terminateSlaveThread();
145 }
146 
148 {
149  AlternateThread::start();
150 }
151 
153 {
154  signalSlaveAndWait();
155 }
156 
158 {
159  Py_BEGIN_ALLOW_THREADS;
160  AlternateThread::signalMasterAndWait();
161  Py_END_ALLOW_THREADS;
162 }
163 
165 {
166  GILState gs;
168 }