1 // Copyright (C) 2007-2016 CEA/DEN, EDF R&D, OPEN CASCADE
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
19 // Author : Anthony GEAY (EDF R&D)
21 #include "SALOMESDS_KeyWaiter.hxx"
22 #include "SALOMESDS_DataScopeServer.hxx"
23 #include "SALOMESDS_PickelizedPyObjServer.hxx"
27 using namespace SALOMESDS;
29 KeyWaiter::KeyWaiter(PickelizedPyObjServer *var, const SALOME::ByteVec& keyVal):_var(var),_ze_key(0),_ze_value(0)
31 if(sem_init(&_sem,0,0)!=0)// put value to 0 to lock by default
32 throw Exception("KeyWaiter constructor : Error on initialization of semaphore !");
34 throw Exception("KeyWaiter constructor : Invalid glob var is NULL !");
35 if(!dynamic_cast<DataScopeServerTransaction *>(var->getFather()))
36 throw Exception("KeyWaiter constructor : Invalid glob var ! Invalid DataScope hosting it ! DataScopeServerTransaction excpected !");
38 PickelizedPyObjServer::FromByteSeqToCpp(keyVal,st);
39 _ze_key=PickelizedPyObjServer::GetPyObjFromPickled(st,getDSS());
40 PyObject *selfMeth(PyObject_GetAttrString(_var->getPyObj(),"__contains__"));//new ref
41 PyObject *args(PyTuple_New(1));
42 PyTuple_SetItem(args,0,_ze_key); Py_XINCREF(_ze_key); // _ze_key is stolen by PyTuple_SetItem
43 PyObject *retPy(PyObject_CallObject(selfMeth,args));
47 if(retPy!=Py_False && retPy!=Py_True)
48 throw Exception("KeyWaiter constructor : unexpected return of dict.__contains__ !");
51 PyObject *retPy2(PyDict_GetItem(_var->getPyObj(),_ze_key));
53 throw Exception("KeyWaiter constructor : dict.getitem fails !");
56 go();//notify that value already arrives -> unlock
60 getDSS()->addWaitKey(this);// key not present let the DataScope managing it !
65 KeyWaiter::~KeyWaiter()
69 Py_XDECREF(_ze_value);
72 PortableServer::POA_var KeyWaiter::getPOA() const
74 return getDSS()->getPOA4KeyWaiter();
78 * WARNING : here it is the single method that can be invoked in non SINGLE_THREAD POA.
79 * So take care to do avoid collapses (especially in python).
81 SALOME::ByteVec *KeyWaiter::waitFor()
85 throw Exception("KeyWaiter::waitFor : internal error 1 !");
86 SALOME::ByteVec *ret(0);
87 {// this section is to guarantee that no concurrent threads are doing python stuff at the same time
88 // Here a pickelization is needed so to guarantee to be alone doing python action the idea is to invoke using monothread POA.
89 DataScopeServerTransaction *dss(getDSS());
90 PortableServer::POA_var poa(dss->getPOA());
91 CORBA::Object_var dssPtr(poa->servant_to_reference(dss));
92 SALOME::DataScopeServerTransaction_var dssPtr2(SALOME::DataScopeServerTransaction::_narrow(dssPtr));
93 if(CORBA::is_nil(dssPtr2))
94 throw Exception("KeyWaiter::waitFor : internal error 2 !");
95 CORBA::Object_var thisPtr(getPOA()->servant_to_reference(this));
96 SALOME::KeyWaiter_var thisPtr2(SALOME::KeyWaiter::_narrow(thisPtr));
97 if(CORBA::is_nil(thisPtr2))
98 throw Exception("KeyWaiter::waitFor : internal error 3 !");
99 ret=dssPtr2->waitForMonoThrRev(thisPtr2);//<- this invokation through SINGLE_THREAD POA here will guarantee thread safety
106 * this method is supposed to be performed in alone.
108 SALOME::ByteVec *KeyWaiter::waitForMonoThr()
111 throw Exception("KeyWaiter::waitForMonoThr : no value ! invalid call of this method !");
112 Py_XINCREF(_ze_value);
113 std::string st(PickelizedPyObjServer::Pickelize(_ze_value,_var->getFather()));
114 return PickelizedPyObjServer::FromCppToByteSeq(st);
118 * WARNING call this method before calling go !
120 void KeyWaiter::valueJustCome(PyObject *val)
125 Py_XDECREF(_ze_value);
127 Py_XINCREF(_ze_value);
132 if(sem_post(&_sem)!=0)
134 std::ostringstream oss; oss << "KeyWaiter::go : error on post of semaphore ! ";
135 throw Exception(oss.str());