From 513b8629ac55467ab7f604cd1733b542f7e9a40a Mon Sep 17 00:00:00 2001 From: Ovidiu Mircescu Date: Thu, 30 Sep 2021 14:37:01 +0200 Subject: [PATCH] DSM in multithread mode. --- src/SALOMESDS/SALOMESDS_DataServerManager.cxx | 74 +++++++++++++++++-- src/SALOMESDS/SALOMESDS_DataServerManager.hxx | 12 +++ src/SALOMESDS/SALOMESDS_RequestSwitcher.hxx | 4 +- src/SALOMESDS/TestSalomeSDS.py | 10 +++ 4 files changed, 90 insertions(+), 10 deletions(-) diff --git a/src/SALOMESDS/SALOMESDS_DataServerManager.cxx b/src/SALOMESDS/SALOMESDS_DataServerManager.cxx index 80ba9319e..57da81963 100644 --- a/src/SALOMESDS/SALOMESDS_DataServerManager.cxx +++ b/src/SALOMESDS/SALOMESDS_DataServerManager.cxx @@ -37,14 +37,25 @@ const char DataServerManager::DFT_SCOPE_NAME_IN_NS[]="Default"; SALOME::StringVec *RequestSwitcherDSM::listScopes() { - return _dsm->listScopes(); + return _dsm->listScopes_unsafe(); } SALOME::DataScopeServerTransaction_ptr RequestSwitcherDSM::giveADataScopeTransactionCalled(const char *scopeName, CORBA::Boolean& isCreated) { - return _dsm->giveADataScopeTransactionCalled(scopeName,isCreated); + return _dsm->giveADataScopeTransactionCalled_unsafe(scopeName,isCreated); } +void RequestSwitcherDSM::holdRequests() +{ + _dsm->holdRequests(); +} + +void RequestSwitcherDSM::activeRequests() +{ + _dsm->activeRequests(); +} + + DataServerManager::DataServerManager(const SALOME_CPythonHelper *pyHelper, CORBA::ORB_ptr orb, PortableServer::POA_ptr poa, SALOME_NamingService_Abstract *ns):_orb(CORBA::ORB::_duplicate(orb)) { _ns = ns==nullptr?new SALOME_NamingService(_orb):ns; @@ -54,7 +65,8 @@ DataServerManager::DataServerManager(const SALOME_CPythonHelper *pyHelper, CORBA policies.length(1); PortableServer::ThreadPolicy_var threadPol(poa->create_thread_policy(PortableServer::SINGLE_THREAD_MODEL)); policies[0]=PortableServer::ThreadPolicy::_duplicate(threadPol); - _poa=poa->create_POA("SingleThPOA4SDS",pman,policies); + PortableServer::POA_var safePoa = poa->create_POA("SingleThPOA4SDS",pman,policies); + _poa = poa; threadPol->destroy(); // activate this to be ready to be usable from NS. PortableServer::ObjectId_var id(_poa->activate_object(this)); @@ -75,7 +87,7 @@ DataServerManager::~DataServerManager() delete _ns; } -SALOME::StringVec *DataServerManager::listScopes() +SALOME::StringVec * DataServerManager::listScopes_unsafe() { std::vector scopes(listOfScopesCpp()); SALOME::StringVec *ret(new SALOME::StringVec); @@ -86,14 +98,21 @@ SALOME::StringVec *DataServerManager::listScopes() return ret; } +SALOME::StringVec *DataServerManager::listScopes() +{ + const std::lock_guard lock(_mutex); + return listScopes_unsafe(); +} + SALOME::StringVec *DataServerManager::listAliveAndKickingScopes() { + const std::lock_guard lock(_mutex); std::vector scopes(listOfScopesCpp()); std::size_t sz(scopes.size()); std::vector retCpp; retCpp.reserve(sz); for(std::size_t i=0;i lock(_mutex); + SALOME::DataScopeServerBase_var ret(retriveDataScope_unsafe(DFT_SCOPE_NAME_IN_NS)); if(CORBA::is_nil(ret)) return SALOME::DataScopeServer::_narrow(ret); SALOME::DataScopeServer_ptr ret2(SALOME::DataScopeServer::_narrow(ret)); @@ -117,6 +137,12 @@ SALOME::DataScopeServer_ptr DataServerManager::getDefaultScope() } CORBA::Boolean DataServerManager::isAliveAndKicking(const char *scopeName) +{ + const std::lock_guard lock(_mutex); + return isAliveAndKicking_unsafe(scopeName); +} + +CORBA::Boolean DataServerManager::isAliveAndKicking_unsafe(const char *scopeName) { SALOME::DataScopeServerBase_var scopePtr(getScopePtrGivenName(scopeName)); return IsAliveAndKicking(scopePtr); @@ -226,25 +252,43 @@ public: SALOME::DataScopeServer_ptr DataServerManager::createDataScope(const char *scopeName) { + const std::lock_guard lock(_mutex); return CreateDataScope(_orb,scopeName,listOfScopesCpp(),*_ns); } SALOME::DataScopeServer_ptr DataServerManager::giveADataScopeCalled(const char *scopeName, CORBA::Boolean& isCreated) { + const std::lock_guard lock(_mutex); return GiveADataScopeCalled(_orb,scopeName,listOfScopesCpp(),*_ns,isCreated); } SALOME::DataScopeServerTransaction_ptr DataServerManager::createDataScopeTransaction(const char *scopeName) { + const std::lock_guard lock(_mutex); return CreateDataScope(_orb,scopeName,listOfScopesCpp(),*_ns); } -SALOME::DataScopeServerTransaction_ptr DataServerManager::giveADataScopeTransactionCalled(const char *scopeName, CORBA::Boolean& isCreated) +SALOME::DataScopeServerTransaction_ptr +DataServerManager::giveADataScopeTransactionCalled_unsafe( + const char *scopeName, CORBA::Boolean& isCreated) { return GiveADataScopeCalled(_orb,scopeName,listOfScopesCpp(),*_ns,isCreated); } + +SALOME::DataScopeServerTransaction_ptr DataServerManager::giveADataScopeTransactionCalled(const char *scopeName, CORBA::Boolean& isCreated) +{ + const std::lock_guard lock(_mutex); + return giveADataScopeTransactionCalled_unsafe(scopeName, isCreated); +} + SALOME::DataScopeServerBase_ptr DataServerManager::retriveDataScope(const char *scopeName) +{ + const std::lock_guard lock(_mutex); + return retriveDataScope_unsafe(scopeName); +} + +SALOME::DataScopeServerBase_ptr DataServerManager::retriveDataScope_unsafe(const char *scopeName) { SALOME::DataScopeServerBase_var ret(getScopePtrGivenName(scopeName)); return SALOME::DataScopeServerBase::_duplicate(ret); @@ -252,6 +296,7 @@ SALOME::DataScopeServerBase_ptr DataServerManager::retriveDataScope(const char * void DataServerManager::removeDataScope(const char *scopeName) { + const std::lock_guard lock(_mutex); SALOME::DataScopeServerBase_var scs(getScopePtrGivenName(scopeName)); SALOME::DataScopeKiller_ptr killer; if(scs->shutdownIfNotHostedByDSM(killer)) @@ -261,10 +306,11 @@ void DataServerManager::removeDataScope(const char *scopeName) void DataServerManager::cleanScopesInNS() { + const std::lock_guard lock(_mutex); std::vector scopes(listOfScopesCpp()); for(std::vector::const_iterator it=scopes.begin();it!=scopes.end();it++) { - if(!isAliveAndKicking((*it).c_str())) + if(!isAliveAndKicking_unsafe((*it).c_str())) { std::string fullScopeName(SALOMESDS::DataServerManager::CreateAbsNameInNSFromScopeName(*it)); _ns->Destroy_Name(fullScopeName.c_str()); @@ -274,6 +320,7 @@ void DataServerManager::cleanScopesInNS() void DataServerManager::shutdownScopes() { + const std::lock_guard lock(_mutex); std::vector scopeNames(listOfScopesCpp()); for(std::vector::const_iterator it=scopeNames.begin();it!=scopeNames.end();it++) { @@ -330,6 +377,7 @@ SALOME::DataScopeServerBase_var DataServerManager::getScopePtrGivenName(const st SALOME::RequestSwitcherDSM_ptr DataServerManager::getRequestSwitcher() { + const std::lock_guard lock(_mutex); if(_rs.isNull()) { _rs=new RequestSwitcherDSM(_orb,this); @@ -337,3 +385,13 @@ SALOME::RequestSwitcherDSM_ptr DataServerManager::getRequestSwitcher() CORBA::Object_var obj(_rs->activate()); return SALOME::RequestSwitcherDSM::_narrow(obj); } + +void DataServerManager::holdRequests() +{ + _mutex.lock(); +} + +void DataServerManager::activeRequests() +{ + _mutex.unlock(); +} diff --git a/src/SALOMESDS/SALOMESDS_DataServerManager.hxx b/src/SALOMESDS/SALOMESDS_DataServerManager.hxx index b7594e944..41c631fc2 100644 --- a/src/SALOMESDS/SALOMESDS_DataServerManager.hxx +++ b/src/SALOMESDS/SALOMESDS_DataServerManager.hxx @@ -31,6 +31,7 @@ #include "SALOMESDS_Defines.hxx" #include +#include class SALOME_NamingService_Abstract; @@ -44,6 +45,8 @@ namespace SALOMESDS RequestSwitcherDSM(CORBA::ORB_ptr orb, DataServerManager *dsm):RequestSwitcherBase(orb),_dsm(dsm) { } SALOME::StringVec *listScopes(); SALOME::DataScopeServerTransaction_ptr giveADataScopeTransactionCalled(const char *scopeName, CORBA::Boolean& isCreated); + void holdRequests()override; + void activeRequests()override; private: DataServerManager *_dsm; }; @@ -71,17 +74,25 @@ namespace SALOMESDS void cleanScopesInNS(); void shutdownScopes(); SALOME::RequestSwitcherDSM_ptr getRequestSwitcher(); + void holdRequests(); + void activeRequests(); + public: CORBA::ORB_var getORB() { return _orb; } static std::string CreateAbsNameInNSFromScopeName(const std::string& scopeName); static CORBA::Boolean IsAliveAndKicking(SALOME::DataScopeServerBase_ptr scopePtr); static SALOME::DataScopeServerBase_var GetScopePtrGivenName(const std::string& scopeName, const std::vector& scopes, SALOME_NamingService_Abstract *ns); + SALOME::StringVec *listScopes_unsafe(); + SALOME::DataScopeServerTransaction_ptr giveADataScopeTransactionCalled_unsafe( + const char *scopeName, CORBA::Boolean& isCreated); public: static const char NAME_IN_NS[]; static const char DFT_SCOPE_NAME_IN_NS[]; private: std::vector listOfScopesCpp(); SALOME::DataScopeServerBase_var getScopePtrGivenName(const std::string& scopeName); + CORBA::Boolean isAliveAndKicking_unsafe(const char *scopeName); + SALOME::DataScopeServerBase_ptr retriveDataScope_unsafe(const char *scopeName); private: //! naming service object is owned SALOME_NamingService_Abstract *_ns = nullptr; @@ -89,6 +100,7 @@ namespace SALOMESDS //! single thread poa PortableServer::POA_var _poa; AutoServantPtr _rs; + std::mutex _mutex; }; } diff --git a/src/SALOMESDS/SALOMESDS_RequestSwitcher.hxx b/src/SALOMESDS/SALOMESDS_RequestSwitcher.hxx index 83e724fc4..8875e4cb3 100644 --- a/src/SALOMESDS/SALOMESDS_RequestSwitcher.hxx +++ b/src/SALOMESDS/SALOMESDS_RequestSwitcher.hxx @@ -36,8 +36,8 @@ namespace SALOMESDS { public: RequestSwitcherBase(CORBA::ORB_ptr orb); - void holdRequests(); - void activeRequests(); + virtual void holdRequests(); + virtual void activeRequests(); PortableServer::POA_var getPOA() const { return _poa_for_request_control; } private: PortableServer::POA_var _poa_for_request_control; diff --git a/src/SALOMESDS/TestSalomeSDS.py b/src/SALOMESDS/TestSalomeSDS.py index 526b6d804..2dc132dc1 100644 --- a/src/SALOMESDS/TestSalomeSDS.py +++ b/src/SALOMESDS/TestSalomeSDS.py @@ -494,6 +494,16 @@ class SalomeSDSTest(unittest.TestCase): dsm.removeDataScope(scopeName) pass + def testShutdownScopes(self): + """ Test shutdownScopes. + """ + salome.salome_init() + dss, isCreated = salome.dsm.giveADataScopeTransactionCalled('scope0') + t0 = dss.createRdExtVarTransac('a', obj2Str({})) + dss.atomicApply([t0]) + salome.dsm.shutdownScopes() # no exception expected + pass + pass if __name__=="__main__": -- 2.39.2