TLS backend OpenSSL: Use function-static variable for symbol loading

Replace a combination of a mutex, atomic variable, and another 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.

Task-number: QTBUG-103559
Change-Id: If9af8584e648ddb9ec518498ce035105e52413a2
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
Reviewed-by: Timur Pocheptsov <timur.pocheptsov@qt.io>
This commit is contained in:
Ievgenii Meshcheriakov 2022-08-30 16:29:55 +02:00
parent aed7b5f370
commit 1c217ba6f7

View File

@ -28,7 +28,6 @@
#elif QT_CONFIG(library)
# include <QtCore/qlibrary.h>
#endif
#include <QtCore/qmutex.h>
#include <QtCore/qdatetime.h>
#if defined(Q_OS_UNIX)
#include <QtCore/qdir.h>
@ -832,21 +831,9 @@ static LoadedOpenSsl loadOpenSsl()
}
#endif
Q_CONSTINIT static QBasicMutex symbolResolveMutex;
Q_CONSTINIT static QBasicAtomicInt symbolsResolved = Q_BASIC_ATOMIC_INITIALIZER(false);
Q_CONSTINIT static bool triedToResolveSymbols = false;
bool q_resolveOpenSslSymbols()
{
if (symbolsResolved.loadAcquire())
return true;
QMutexLocker locker(&symbolResolveMutex);
if (symbolsResolved.loadRelaxed())
return true;
if (triedToResolveSymbols)
return false;
triedToResolveSymbols = true;
static bool symbolsResolved = []() {
LoadedOpenSsl libs = loadOpenSsl();
if (!libs.ssl || !libs.crypto) {
qCWarning(lcTlsBackend, "Failed to load libssl/libcrypto.");
@ -1235,9 +1222,10 @@ bool q_resolveOpenSslSymbols()
RESOLVEFUNC(PKCS12_parse)
RESOLVEFUNC(d2i_PKCS12_bio)
RESOLVEFUNC(PKCS12_free)
symbolsResolved.storeRelease(true);
return true;
}();
return symbolsResolved;
}
#endif // QT_CONFIG(library)