qsslsocket_openssl_symbols.cpp: replace mutex pool use with QBasicMutex

The use of QMutexPool caused an #ifdef, because, lacking an object,
some OpenSSL function pointer was used as the address required as
input for the mutex pool. Sadly, the names of the functions differ
between OpenSSL versions, thus the need for an #ifdef.

By simply using a QBasicMutex (defined at namespace scope to evade
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=79561), the #ifdef can go
away.

Another level of #ifdefs goes away because, even for
non-QT_CONFIG(thread) builds, Q*Mutex etc are defined, just as
no-ops. So we can freely use them without QT_CONFIG(thread) guard,
unlike QMutexPool.

Finally, optimize for the (common?) case of already-loaded libraries
by making symbolsResolved an atomic variable, and checking that before
taking the mutex (double-checked locking, done right).

For reasons of said GCC bug, again, the QBasicAtomic is defined at
namespace scope. And then move the other boolean there for symmetry.

Change-Id: Ic5f44871fb200e5368b9af327e4d1e852fbc586c
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
bb10
Marc Mutz 2019-07-04 22:33:07 +02:00
parent c39910993e
commit f14db5dd3a
1 changed files with 10 additions and 14 deletions

View File

@ -63,9 +63,6 @@
# include <QtCore/qlibrary.h>
#endif
#include <QtCore/qmutex.h>
#if QT_CONFIG(thread)
#include <private/qmutexpool_p.h>
#endif
#include <QtCore/qdatetime.h>
#if defined(Q_OS_UNIX)
#include <QtCore/qdir.h>
@ -936,18 +933,16 @@ static QPair<QLibrary*, QLibrary*> loadOpenSsl()
}
#endif
static QBasicMutex symbolResolveMutex;
static QBasicAtomicInt symbolsResolved = Q_BASIC_ATOMIC_INITIALIZER(false);
static bool triedToResolveSymbols = false;
bool q_resolveOpenSslSymbols()
{
static bool symbolsResolved = false;
static bool triedToResolveSymbols = false;
#if QT_CONFIG(thread)
#if QT_CONFIG(opensslv11)
QMutexLocker locker(QMutexPool::globalInstanceGet((void *)&q_OPENSSL_init_ssl));
#else
QMutexLocker locker(QMutexPool::globalInstanceGet((void *)&q_SSL_library_init));
#endif
#endif
if (symbolsResolved)
if (symbolsResolved.loadAcquire())
return true;
QMutexLocker locker(&symbolResolveMutex);
if (symbolsResolved.loadRelaxed())
return true;
if (triedToResolveSymbols)
return false;
@ -1402,7 +1397,8 @@ bool q_resolveOpenSslSymbols()
RESOLVEFUNC(d2i_PKCS12_bio)
RESOLVEFUNC(PKCS12_free)
symbolsResolved = true;
symbolsResolved.storeRelease(true);
delete libs.first;
delete libs.second;
return true;