diff --git a/cpp/src/Ice/ConnectionFactory.cpp b/cpp/src/Ice/ConnectionFactory.cpp index 2ea7ce38951..d9cb1775dcc 100644 --- a/cpp/src/Ice/ConnectionFactory.cpp +++ b/cpp/src/Ice/ConnectionFactory.cpp @@ -457,77 +457,35 @@ IceInternal::OutgoingConnectionFactory::getConnection( bool& compress) { { + assert(cb); unique_lock lock(_mutex); if (_destroyed) { throw Ice::CommunicatorDestroyedException(__FILE__, __LINE__); } + // Search for an existing connections matching one of the given endpoints. + Ice::ConnectionIPtr connection = findConnection(connectors, compress); + if (connection) + { + return connection; + } + // - // Try to get the connection. We may need to wait for other threads to - // finish if one of them is currently establishing a connection to one - // of our connectors. + // Determine whether another thread/request is currently attempting to connect to + // one of our endpoints; if so we wait until it's done. // - while (true) + if (addToPending(cb, connectors)) { - if (_destroyed) - { - throw Ice::CommunicatorDestroyedException(__FILE__, __LINE__); - } - - // - // Search for a matching connection. If we find one, we're done. - // - Ice::ConnectionIPtr connection = findConnection(connectors, compress); - if (connection) - { - return connection; - } - - // - // Determine whether another thread/request is currently attempting to connect to - // one of our endpoints; if so we wait until it's done. - // - if (addToPending(cb, connectors)) - { - // - // If a callback is not specified we wait until another thread notifies us about a - // change to the pending list. Otherwise, if a callback is provided we're done: - // when the pending list changes the callback will be notified and will try to - // get the connection again. - // - if (!cb) - { - _conditionVariable.wait(lock); - } - else - { - return nullptr; - } - } - else - { - // - // If no thread is currently establishing a connection to one of our connectors, - // we get out of this loop and start the connection establishment to one of the - // given connectors. - // - break; - } + // A connection to one of our endpoints is pending. The callback will be notified once the connection + // is established. Returning null indicates that the connection is still pending. + return nullptr; } } - // - // At this point, we're responsible for establishing the connection to one of - // the given connectors. If it's a non-blocking connect, calling nextConnector - // will start the connection establishment. Otherwise, we return null to get - // the caller to establish the connection. - // - if (cb) - { - cb->nextConnector(); - } - + // No connection is pending. Call nextConnector to initiate connection establishment. Return null to indicate + // that the connection is still pending. + cb->nextConnector(); return nullptr; } @@ -1578,7 +1536,7 @@ IceInternal::IncomingConnectionFactory::connectionStartCompleted(const Ice::Conn lock_guard lock(_mutex); // - // Initialy, connections are in the holding state. If the factory is active + // Initially, connections are in the holding state. If the factory is active // we activate the connection. // if (_state == StateActive) @@ -1590,15 +1548,7 @@ IceInternal::IncomingConnectionFactory::connectionStartCompleted(const Ice::Conn void IceInternal::IncomingConnectionFactory::connectionStartFailed(const Ice::ConnectionIPtr& /*connection*/, exception_ptr) { - lock_guard lock(_mutex); - if (_state >= StateClosed) - { - return; - } - - // // Do not warn about connection exceptions here. The connection is not yet validated. - // } // diff --git a/java/src/Ice/src/main/java/com/zeroc/Ice/IncomingConnectionFactory.java b/java/src/Ice/src/main/java/com/zeroc/Ice/IncomingConnectionFactory.java index e7e6d980548..796312682b7 100644 --- a/java/src/Ice/src/main/java/com/zeroc/Ice/IncomingConnectionFactory.java +++ b/java/src/Ice/src/main/java/com/zeroc/Ice/IncomingConnectionFactory.java @@ -329,12 +329,7 @@ public synchronized void connectionStartCompleted(ConnectionI connection) { @Override public synchronized void connectionStartFailed(ConnectionI connection, LocalException ex) { - if (_state >= StateClosed) { - return; - } - // // Do not warn about connection exceptions here. The connection is not yet validated. - // } public IncomingConnectionFactory(Instance instance, EndpointI endpoint, ObjectAdapter adapter) { diff --git a/java/src/Ice/src/main/java/com/zeroc/Ice/OutgoingConnectionFactory.java b/java/src/Ice/src/main/java/com/zeroc/Ice/OutgoingConnectionFactory.java index 539408507f1..a6353878cf5 100644 --- a/java/src/Ice/src/main/java/com/zeroc/Ice/OutgoingConnectionFactory.java +++ b/java/src/Ice/src/main/java/com/zeroc/Ice/OutgoingConnectionFactory.java @@ -367,47 +367,22 @@ private ConnectionI getConnection( throw new CommunicatorDestroyedException(); } - // - // Try to get the connection. We may need to wait for other threads to - // finish if one of them is currently establishing a connection to one - // of our connectors. - // - while (true) { - if (_destroyed) { - throw new CommunicatorDestroyedException(); - } - - // - // Search for a matching connection. If we find one, we're done. - // - ConnectionI connection = findConnection(connectors, compress); - if (connection != null) { - return connection; - } - - if (addToPending(cb, connectors)) { - return null; - } else { - // - // If no thread is currently establishing a connection to one of our connectors, - // we get out of this loop and start the connection establishment to one of the - // given connectors. - // - break; - } + // Search for an existing connections matching one of the given endpoints. + ConnectionI connection = findConnection(connectors, compress); + if (connection != null) { + return connection; } - } - // - // At this point, we're responsible for establishing the connection to one of - // the given connectors. If it's a non-blocking connect, calling nextConnector - // will start the connection establishment. Otherwise, we return null to get - // the caller to establish the connection. - // - if (cb != null) { - cb.nextConnector(); + if (addToPending(cb, connectors)) { + // A connection to one of our endpoints is pending. The callback will be notified once the connection + // is established. Returning null indicates that the connection is still pending. + return null; + } } + // No connection is pending. Call nextConnector to initiate connection establishment. Return null to indicate + // that the connection is still pending. + cb.nextConnector(); return null; }