Salome HOME
Merge branch 'V7_dev'
[modules/kernel.git] / src / SALOMESDS / SALOMESDS_KeyWaiter.cxx
1 // Copyright (C) 2007-2016  CEA/DEN, EDF R&D, 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 excpected !");
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 SALOME::ByteVec *KeyWaiter::waitFor()
82 {
83   sem_wait(&_sem);
84   if(!_ze_value)
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
100   }
101   enforcedRelease();
102   return ret;
103 }
104
105 /*!
106  * this method is supposed to be performed in alone.
107  */
108 SALOME::ByteVec *KeyWaiter::waitForMonoThr()
109 {
110   if(!_ze_value)
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);
115 }
116
117 /*!
118  * WARNING call this method before calling go !
119  */
120 void KeyWaiter::valueJustCome(PyObject *val)
121 {
122   if(_ze_value==val)
123     return ;
124   if(_ze_value)
125     Py_XDECREF(_ze_value);
126   _ze_value=val;
127   Py_XINCREF(_ze_value);
128 }
129
130 void KeyWaiter::go()
131 {
132   if(sem_post(&_sem)!=0)
133     {
134       std::ostringstream oss; oss << "KeyWaiter::go : error on post of semaphore ! ";
135       throw Exception(oss.str());
136     }
137 }