1 // Copyright (C) 2007-2021 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 expected !");
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 void KeyWaiter::waitFor()
85 throw Exception("KeyWaiter::waitFor : internal error 1 !");
89 * this method is supposed to be performed in alone.
91 SALOME::ByteVec *KeyWaiter::waitForMonoThr()
94 throw Exception("KeyWaiter::waitForMonoThr : no value ! invalid call of this method !");
95 Py_XINCREF(_ze_value);
96 std::string st(PickelizedPyObjServer::Pickelize(_ze_value,_var->getFather()));
97 return PickelizedPyObjServer::FromCppToByteSeq(st);
100 SALOME::ByteVec *KeyWaiter::waitForAndKill()
103 throw Exception("KeyWaiter::waitForAndKill : no value ! invalid call of this method !");
104 Py_XINCREF(_ze_value);
105 std::string st(PickelizedPyObjServer::Pickelize(_ze_value,_var->getFather()));
107 if(PyDict_DelItem(_var->getPyObj(),_ze_key)!=0)
108 throw Exception("KeyWaiter::waitForAndKill : error during entry removal !");
110 return PickelizedPyObjServer::FromCppToByteSeq(st);
114 * WARNING call this method before calling go !
116 void KeyWaiter::valueJustCome(PyObject *val)
121 Py_XDECREF(_ze_value);
123 Py_XINCREF(_ze_value);
128 if(sem_post(&_sem)!=0)
130 std::ostringstream oss; oss << "KeyWaiter::go : error on post of semaphore ! ";
131 throw Exception(oss.str());