Salome HOME
Updated copyright comment
[modules/kernel.git] / src / SALOMESDS / SALOMESDS_KeyWaiter.cxx
1 // Copyright (C) 2007-2024  CEA, EDF, OPEN CASCADE
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 // Author : Anthony GEAY (EDF R&D)
20
21 #include "SALOMESDS_KeyWaiter.hxx"
22 #include "SALOMESDS_DataScopeServer.hxx"
23 #include "SALOMESDS_PickelizedPyObjServer.hxx"
24
25 #include <sstream>
26
27 using namespace SALOMESDS;
28
29 KeyWaiter::KeyWaiter(PickelizedPyObjServer *var, const SALOME::ByteVec& keyVal):_var(var),_ze_key(0),_ze_value(0)
30 {
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 !");
33   if(!var)
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 !");
37   std::string st;
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));
44   Py_XDECREF(args);
45   Py_XDECREF(selfMeth);
46   //
47   if(retPy!=Py_False && retPy!=Py_True)
48     throw Exception("KeyWaiter constructor : unexpected return of dict.__contains__ !");
49   if(retPy==Py_True)
50     {
51       PyObject *retPy2(PyDict_GetItem(_var->getPyObj(),_ze_key));
52       if(retPy2==NULL)
53         throw Exception("KeyWaiter constructor : dict.getitem fails !");
54       Py_XINCREF(retPy2);
55       _ze_value=retPy2;
56       go();//notify that value already arrives -> unlock
57     }
58   else
59     {
60       getDSS()->addWaitKey(this);// key not present let the DataScope managing it !
61     }
62   Py_XDECREF(retPy);
63 }
64
65 KeyWaiter::~KeyWaiter()
66 {
67   Py_XDECREF(_ze_key);
68   if(_ze_value)
69     Py_XDECREF(_ze_value);
70 }
71
72 PortableServer::POA_var KeyWaiter::getPOA() const
73 {
74   return getDSS()->getPOA4KeyWaiter();
75 }
76
77 /*!
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).
80  */
81 void KeyWaiter::waitFor()
82 {
83   sem_wait(&_sem);
84   if(!_ze_value)
85     throw Exception("KeyWaiter::waitFor : internal error 1 !");
86 }
87
88 /*!
89  * this method is supposed to be performed in alone.
90  */
91 SALOME::ByteVec *KeyWaiter::waitForMonoThr()
92 {
93   if(!_ze_value)
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);
98 }
99
100 SALOME::ByteVec *KeyWaiter::waitForAndKill()
101 {
102   if(!_ze_value)
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()));
106   //
107   if(PyDict_DelItem(_var->getPyObj(),_ze_key)!=0)
108     throw Exception("KeyWaiter::waitForAndKill : error during entry removal !");
109   //
110   return PickelizedPyObjServer::FromCppToByteSeq(st);
111 }
112
113 /*!
114  * WARNING call this method before calling go !
115  */
116 void KeyWaiter::valueJustCome(PyObject *val)
117 {
118   if(_ze_value==val)
119     return ;
120   if(_ze_value)
121     Py_XDECREF(_ze_value);
122   _ze_value=val;
123   Py_XINCREF(_ze_value);
124 }
125
126 void KeyWaiter::go()
127 {
128   if(sem_post(&_sem)!=0)
129     {
130       std::ostringstream oss; oss << "KeyWaiter::go : error on post of semaphore ! ";
131       throw Exception(oss.str());
132     }
133 }