QTlsBackendOpenSSL: Use a function-static variable in ensureLibraryLoaded()

Replace a combination of a mutex and a state variable by
a function-local variable initialized by lambda.

C++17 standard guarantees that the lambda is called only once and
that any other callers will waiting for initialization to complete.

The mutex that was replaced is also used in ensureCiphersAndCertsLoaded()
but that seems to be a false sharing.

Task-number: QTBUG-103559
Change-Id: Idb269a24b53cf3812ca9630ab4fc87f99ab16d55
Reviewed-by: Timur Pocheptsov <timur.pocheptsov@qt.io>
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
This commit is contained in:
Ievgenii Meshcheriakov 2022-08-31 09:56:42 +02:00
parent 1c217ba6f7
commit 216fe24ca1
2 changed files with 6 additions and 10 deletions

View File

@ -59,7 +59,6 @@ static void q_loadCiphersForConnection(SSL *connection, QList<QSslCipher> &ciphe
}
}
bool QTlsBackendOpenSSL::s_libraryLoaded = false;
bool QTlsBackendOpenSSL::s_loadedCiphersAndCerts = false;
int QTlsBackendOpenSSL::s_indexForSSLExtraData = -1;
@ -92,12 +91,10 @@ void QTlsBackendOpenSSL::clearErrorQueue()
bool QTlsBackendOpenSSL::ensureLibraryLoaded()
{
if (!q_resolveOpenSslSymbols())
return false;
static bool libraryLoaded = []() {
if (!q_resolveOpenSslSymbols())
return false;
const QMutexLocker locker(qt_opensslInitMutex());
if (!s_libraryLoaded) {
// Initialize OpenSSL.
if (q_OPENSSL_init_ssl(0, nullptr) != 1)
return false;
@ -119,10 +116,10 @@ bool QTlsBackendOpenSSL::ensureLibraryLoaded()
return false;
}
s_libraryLoaded = true;
}
return true;
}();
return true;
return libraryLoaded;
}
QString QTlsBackendOpenSSL::backendName() const

View File

@ -43,7 +43,6 @@ public:
static bool ensureLibraryLoaded();
// Index used in SSL_get_ex_data to get the matching TlsCryptographerOpenSSL:
static bool s_libraryLoaded;
static bool s_loadedCiphersAndCerts;
static int s_indexForSSLExtraData;