QJniHelpers: replace some Q_GLOBAL_STATIC with QBasic* statics

For both QMutex and QAtomicInt, we have QBasic{Mutex,AtomicInt}, which
are PODs, and therefore constant-initialized. Q_GLOBAL_STATIC, otoh,
forces dynamic initialization, so don't use it.

Raw pointers don't need Q_GLOBAL_STATIC, either.

Patch up users. Instead of adding &'s to QMutexLocker arguments, port
directly to qt_scoped_lock, which takes by reference.

Change-Id: I1a13db2a5e88a52d4338a174a80522a3f5e58fa2
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Reviewed-by: Giuseppe D'Angelo <giuseppe.dangelo@kdab.com>
This commit is contained in:
Marc Mutz 2022-12-28 15:15:02 +01:00
parent fe7a0c19a6
commit 4225116260

View File

@ -10,6 +10,7 @@
#include "qsemaphore.h" #include "qsemaphore.h"
#include "qreadwritelock.h" #include "qreadwritelock.h"
#include <QtCore/private/qcoreapplication_p.h> #include <QtCore/private/qcoreapplication_p.h>
#include <QtCore/private/qlocking_p.h>
#include <android/log.h> #include <android/log.h>
#include <deque> #include <deque>
@ -34,10 +35,10 @@ static jobject g_jActivity = nullptr;
static jobject g_jService = nullptr; static jobject g_jService = nullptr;
static jobject g_jClassLoader = nullptr; static jobject g_jClassLoader = nullptr;
Q_GLOBAL_STATIC(QtAndroidPrivate::OnBindListener *, g_onBindListener, nullptr); Q_CONSTINIT static QtAndroidPrivate::OnBindListener *g_onBindListener;
Q_GLOBAL_STATIC(QMutex, g_onBindListenerMutex); Q_CONSTINIT static QBasicMutex g_onBindListenerMutex;
Q_GLOBAL_STATIC(QSemaphore, g_waitForServiceSetupSemaphore); Q_GLOBAL_STATIC(QSemaphore, g_waitForServiceSetupSemaphore);
Q_GLOBAL_STATIC(QAtomicInt, g_serviceSetupLockers); Q_CONSTINIT static QBasicAtomicInt g_serviceSetupLockers = Q_BASIC_ATOMIC_INITIALIZER(0);
Q_GLOBAL_STATIC(QReadWriteLock, g_updateMutex); Q_GLOBAL_STATIC(QReadWriteLock, g_updateMutex);
@ -361,36 +362,36 @@ void QtAndroidPrivate::waitForServiceSetup()
int QtAndroidPrivate::acuqireServiceSetup(int flags) int QtAndroidPrivate::acuqireServiceSetup(int flags)
{ {
g_serviceSetupLockers->ref(); g_serviceSetupLockers.ref();
return flags; return flags;
} }
void QtAndroidPrivate::setOnBindListener(QtAndroidPrivate::OnBindListener *listener) void QtAndroidPrivate::setOnBindListener(QtAndroidPrivate::OnBindListener *listener)
{ {
QMutexLocker lock(g_onBindListenerMutex()); const auto lock = qt_scoped_lock(g_onBindListenerMutex);
*g_onBindListener = listener; g_onBindListener = listener;
if (!g_serviceSetupLockers->deref()) if (!g_serviceSetupLockers.deref())
g_waitForServiceSetupSemaphore->release(); g_waitForServiceSetupSemaphore->release();
} }
jobject QtAndroidPrivate::callOnBindListener(jobject intent) jobject QtAndroidPrivate::callOnBindListener(jobject intent)
{ {
QMutexLocker lock(g_onBindListenerMutex()); const auto lock = qt_scoped_lock(g_onBindListenerMutex);
if (*g_onBindListener) if (g_onBindListener)
return (*g_onBindListener)->onBind(intent); return g_onBindListener->onBind(intent);
return nullptr; return nullptr;
} }
Q_GLOBAL_STATIC(QAtomicInt, g_androidDeadlockProtector); Q_CONSTINIT static QBasicAtomicInt g_androidDeadlockProtector = Q_BASIC_ATOMIC_INITIALIZER(0);
bool QtAndroidPrivate::acquireAndroidDeadlockProtector() bool QtAndroidPrivate::acquireAndroidDeadlockProtector()
{ {
return g_androidDeadlockProtector->testAndSetAcquire(0, 1); return g_androidDeadlockProtector.testAndSetAcquire(0, 1);
} }
void QtAndroidPrivate::releaseAndroidDeadlockProtector() void QtAndroidPrivate::releaseAndroidDeadlockProtector()
{ {
g_androidDeadlockProtector->storeRelease(0); g_androidDeadlockProtector.storeRelease(0);
} }
QT_END_NAMESPACE QT_END_NAMESPACE