From b00bd29d8ab1e58192fbf356b77b6c735ada9e5e Mon Sep 17 00:00:00 2001 From: Johnny Willemsen Date: Thu, 27 Jun 2024 16:59:45 +0200 Subject: [PATCH] Experiment with std::once_call * TAO/TAO_IDL/be/be_visitor_operation/ami_cs.cpp: * TAO/TAO_IDL/be/be_visitor_operation/operation.cpp: * TAO/tao/Object.cpp: * TAO/tao/Object.h: * TAO/tao/Object.inl: --- .../be/be_visitor_operation/ami_cs.cpp | 6 +- .../be/be_visitor_operation/operation.cpp | 8 +-- TAO/tao/Object.cpp | 60 ++++++++----------- TAO/tao/Object.h | 20 ++----- TAO/tao/Object.inl | 7 --- 5 files changed, 32 insertions(+), 69 deletions(-) diff --git a/TAO/TAO_IDL/be/be_visitor_operation/ami_cs.cpp b/TAO/TAO_IDL/be/be_visitor_operation/ami_cs.cpp index 7c59fada12b55..498935da55b37 100644 --- a/TAO/TAO_IDL/be/be_visitor_operation/ami_cs.cpp +++ b/TAO/TAO_IDL/be/be_visitor_operation/ami_cs.cpp @@ -119,11 +119,7 @@ be_visitor_operation_ami_cs::visit_operation (be_operation *node) else { *os << be_nl - << "if (!this->is_evaluated ())" << be_idt_nl - << "{" << be_idt_nl - << "::CORBA::Object::tao_object_initialize (this);" - << be_uidt_nl - << "}" << be_uidt_nl << be_nl; + << "std::call_once(this->object_initialized_flag_, CORBA::Object::tao_object_initialize, this);" << be_nl_2; } // Includes the reply handler, but we have to add 1 for the retval anyway. diff --git a/TAO/TAO_IDL/be/be_visitor_operation/operation.cpp b/TAO/TAO_IDL/be/be_visitor_operation/operation.cpp index 4be922272827a..9fa303140de90 100644 --- a/TAO/TAO_IDL/be/be_visitor_operation/operation.cpp +++ b/TAO/TAO_IDL/be/be_visitor_operation/operation.cpp @@ -187,12 +187,8 @@ be_visitor_operation::gen_stub_operation_body ( if (!node->is_abstract ()) { // If the object is lazily evaluated the proxy broker might well - // be null. Initialize it now. - *os << "if (!this->is_evaluated ())" << be_idt_nl - << "{" << be_idt_nl - << "::CORBA::Object::tao_object_initialize (this);" - << be_uidt_nl - << "}" << be_uidt_nl << be_nl; + // be null. Initialize it now. + *os << "std::call_once(this->object_initialized_flag_, CORBA::Object::tao_object_initialize, this);" << be_nl_2; } // Declare return type helper class. diff --git a/TAO/tao/Object.cpp b/TAO/tao/Object.cpp index fb2dce593cb75..a6f75bcdc5a41 100644 --- a/TAO/tao/Object.cpp +++ b/TAO/tao/Object.cpp @@ -42,7 +42,6 @@ CORBA::Object::Object (TAO_Stub * protocol_proxy, TAO_ORB_Core *orb_core) : refcount_ (1) , is_local_ (false) - , is_evaluated_ (true) , ior_ (nullptr) , orb_core_ (orb_core) , protocol_proxy_ (protocol_proxy) @@ -67,7 +66,6 @@ CORBA::Object::Object (IOP::IOR *ior, TAO_ORB_Core *orb_core) : refcount_ (1) , is_local_ (false) - , is_evaluated_ (false) , ior_ (ior) , orb_core_ (orb_core) , protocol_proxy_ (nullptr) @@ -77,20 +75,7 @@ CORBA::Object::Object (IOP::IOR *ior, // Too lazy to do this check in every method properly! This is useful // only for lazily evaluated IOR's #define TAO_OBJECT_IOR_EVALUATE \ -if (!this->is_evaluated_) \ - { \ - ACE_GUARD (TAO_SYNCH_MUTEX , mon, this->object_init_lock_); \ - if (!this->is_evaluated_) \ - CORBA::Object::tao_object_initialize (this); \ - } - -#define TAO_OBJECT_IOR_EVALUATE_RETURN \ -if (!this->is_evaluated_) \ - { \ - ACE_GUARD_RETURN (TAO_SYNCH_MUTEX , mon, this->object_init_lock_, 0); \ - if (!this->is_evaluated_) \ - CORBA::Object::tao_object_initialize (this); \ - } + std::call_once(this->object_initialized_flag_, CORBA::Object::tao_object_initialize, this); void CORBA::Object::_add_ref () @@ -190,7 +175,7 @@ CORBA::Object::_servant () const CORBA::Boolean CORBA::Object::_is_a (const char *type_id) { - TAO_OBJECT_IOR_EVALUATE_RETURN; + TAO_OBJECT_IOR_EVALUATE; // NOTE: if _stub->type_id is nonzero and we have local knowledge of // it, we can answer this question without a costly remote call. @@ -258,14 +243,14 @@ CORBA::Object::_stubobj () const TAO_Stub * CORBA::Object::_stubobj () { - TAO_OBJECT_IOR_EVALUATE_RETURN; + TAO_OBJECT_IOR_EVALUATE; return this->protocol_proxy_; } CORBA::ULong CORBA::Object::_hash (CORBA::ULong maximum) { - TAO_OBJECT_IOR_EVALUATE_RETURN; + TAO_OBJECT_IOR_EVALUATE; if (this->protocol_proxy_ != nullptr) return this->protocol_proxy_->hash (maximum); @@ -297,7 +282,7 @@ CORBA::Object::_is_equivalent (CORBA::Object_ptr other_obj) return true; } - TAO_OBJECT_IOR_EVALUATE_RETURN; + TAO_OBJECT_IOR_EVALUATE; if (this->protocol_proxy_ != nullptr) return this->protocol_proxy_->is_equivalent (other_obj); @@ -310,7 +295,7 @@ CORBA::Object::_is_equivalent (CORBA::Object_ptr other_obj) TAO::ObjectKey * CORBA::Object::_key () { - TAO_OBJECT_IOR_EVALUATE_RETURN; + TAO_OBJECT_IOR_EVALUATE; if (this->_stubobj () && this->_stubobj ()->profile_in_use ()) return this->_stubobj ()->profile_in_use ()->_key (); @@ -444,7 +429,7 @@ CORBA::Object::_create_request (CORBA::Context_ptr ctx, CORBA::Request_ptr CORBA::Object::_request (const char *operation) { - TAO_OBJECT_IOR_EVALUATE_RETURN; + TAO_OBJECT_IOR_EVALUATE; if (this->protocol_proxy_) { TAO_Dynamic_Adapter *dynamic_adapter = @@ -475,7 +460,7 @@ CORBA::Object::_request (const char *operation) CORBA::Boolean CORBA::Object::_non_existent () { - TAO_OBJECT_IOR_EVALUATE_RETURN; + TAO_OBJECT_IOR_EVALUATE; CORBA::Boolean retval = false; @@ -496,14 +481,14 @@ CORBA::Object::_non_existent () CORBA::InterfaceDef_ptr CORBA::Object::_get_interface () { - TAO_OBJECT_IOR_EVALUATE_RETURN; + TAO_OBJECT_IOR_EVALUATE; return this->proxy_broker ()->_get_interface (this); } CORBA::Object_ptr CORBA::Object::_get_component () { - TAO_OBJECT_IOR_EVALUATE_RETURN; + TAO_OBJECT_IOR_EVALUATE; return this->proxy_broker ()->_get_component (this); } #endif @@ -511,7 +496,7 @@ CORBA::Object::_get_component () char* CORBA::Object::_repository_id () { - TAO_OBJECT_IOR_EVALUATE_RETURN; + TAO_OBJECT_IOR_EVALUATE; return this->proxy_broker ()->_repository_id (this); } @@ -528,7 +513,7 @@ CORBA::Object::_repository_id () CORBA::Policy_ptr CORBA::Object::_get_policy (CORBA::PolicyType type) { - TAO_OBJECT_IOR_EVALUATE_RETURN; + TAO_OBJECT_IOR_EVALUATE; if (this->protocol_proxy_) return this->protocol_proxy_->get_policy (type); @@ -545,7 +530,7 @@ CORBA::Object::_get_policy (CORBA::PolicyType type) CORBA::Policy_ptr CORBA::Object::_get_cached_policy (TAO_Cached_Policy_Type type) { - TAO_OBJECT_IOR_EVALUATE_RETURN; + TAO_OBJECT_IOR_EVALUATE; if (this->protocol_proxy_) return this->protocol_proxy_->get_cached_policy (type); @@ -564,7 +549,7 @@ CORBA::Object::_set_policy_overrides ( const CORBA::PolicyList & policies, CORBA::SetOverrideType set_add) { - TAO_OBJECT_IOR_EVALUATE_RETURN; + TAO_OBJECT_IOR_EVALUATE; if (!this->protocol_proxy_) { @@ -606,7 +591,7 @@ CORBA::Object::_set_policy_overrides ( CORBA::PolicyList * CORBA::Object::_get_policy_overrides (const CORBA::PolicyTypeSeq & types) { - TAO_OBJECT_IOR_EVALUATE_RETURN; + TAO_OBJECT_IOR_EVALUATE; if (this->protocol_proxy_) return this->protocol_proxy_->get_policy_overrides (types); else @@ -623,7 +608,7 @@ CORBA::Boolean CORBA::Object::_validate_connection ( CORBA::PolicyList_out inconsistent_policies) { - TAO_OBJECT_IOR_EVALUATE_RETURN; + TAO_OBJECT_IOR_EVALUATE; inconsistent_policies = nullptr; CORBA::Boolean retval = true; @@ -665,7 +650,7 @@ CORBA::Object::_get_orb () } else { - TAO_OBJECT_IOR_EVALUATE_RETURN; + TAO_OBJECT_IOR_EVALUATE; if (this->protocol_proxy_) return CORBA::ORB::_duplicate (this->protocol_proxy_->orb_core ()->orb ()); else @@ -729,8 +714,13 @@ operator<< (TAO_OutputCDR& cdr, const CORBA::Object* x) /*static*/ void CORBA::Object::tao_object_initialize (CORBA::Object *obj) { - CORBA::ULong const profile_count = - obj->ior_->profiles.length (); + // For a local object or when no ior has been set nothing has to be done + if (obj->_is_local () || !obj->ior_) + { + return; + } + + CORBA::ULong const profile_count = obj->ior_->profiles.length (); // Assumption is that after calling this method, folks should test // for protocol_proxy_ or whatever to make sure that things have @@ -825,8 +815,6 @@ CORBA::Object::tao_object_initialize (CORBA::Object *obj) obj->protocol_proxy_ = objdata; - obj->is_evaluated_ = true; - // Release the contents of the ior to keep memory consumption down. obj->ior_ = nullptr; diff --git a/TAO/tao/Object.h b/TAO/tao/Object.h index be3a1b18ef32f..56fab2a822a09 100644 --- a/TAO/tao/Object.h +++ b/TAO/tao/Object.h @@ -31,6 +31,7 @@ #include "tao/Arg_Traits_T.h" #include "tao/Any_Insert_Policy_T.h" #include +#include TAO_BEGIN_VERSIONED_NAMESPACE_DECL @@ -293,9 +294,6 @@ namespace CORBA #endif /* GEN_OSTREAM_OPS */ - /// Accessor to the flag.. - CORBA::Boolean is_evaluated () const; - /// Accessor for the ORB_Core.. TAO_ORB_Core *orb_core () const; @@ -335,6 +333,10 @@ namespace CORBA # define TAO_OBJECT_USES_STD_ATOMIC_REFCOUNT std::atomic refcount_; + /// once_flag to make sure that this object is only initialized once + std::once_flag object_initialized_flag_; +# define TAO_OBJECT_USES_STD_ONCE_FLAG_OBJECT_INITIALIZED + private: Object (const Object &) = delete; Object &operator = (const Object &) = delete; @@ -343,9 +345,6 @@ namespace CORBA /// Specify whether this is a local object or not. CORBA::Boolean is_local_; - /// Flag to indicate whether the IOP::IOR has been evaluated fully. - CORBA::Boolean is_evaluated_; - /// If the IOR hasnt been evaluated fully, then the contents of /// the IOR that we received should be in here! IOP::IOR_var ior_; @@ -372,15 +371,6 @@ namespace CORBA * Objects */ TAO_Stub * protocol_proxy_; - - /// Protect reference count manipulation from race conditions. - /** - * This lock is only instantiated for unconstrained objects. The - * reason for this is that locality-constrained objects that do - * not require reference counting (the default) may be - * instantiated in the critical path. - */ - TAO_SYNCH_MUTEX object_init_lock_; }; } // End CORBA namespace. diff --git a/TAO/tao/Object.inl b/TAO/tao/Object.inl index 0059623312d7a..34878518a6db1 100644 --- a/TAO/tao/Object.inl +++ b/TAO/tao/Object.inl @@ -22,7 +22,6 @@ ACE_INLINE CORBA::Object::Object (int) : refcount_ (1), is_local_ (true), - is_evaluated_ (true), ior_ (), orb_core_ (nullptr), protocol_proxy_ (nullptr) @@ -56,12 +55,6 @@ CORBA::Object::_narrow (CORBA::Object_ptr obj) return CORBA::Object::_duplicate (obj); } -ACE_INLINE CORBA::Boolean -CORBA::Object::is_evaluated () const -{ - return this->is_evaluated_; -} - ACE_INLINE TAO_ORB_Core * CORBA::Object::orb_core () const {