QSqlDatabase: use a QReadWriteLock to access DriverDict

DriverDict might be accessed from different threads so we have to make
sure it's thread-safe. Use the already existing QReadWriteLock from
QConnectionDict for later merging of these two global instances. The two
instances do not interfere each other (DriverDict is a dictionary for
custom registered sql drivers, QConnectionDict contains all current
active database connections) so the dual-use of the mutex is fine.

Pick-to: 6.5 6.2 5.15
Change-Id: I84c77df666e72e826d0d3d291cecd5417bbd1baf
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
bb10
Christian Ehrlicher 2023-04-27 20:37:18 +02:00
parent bc85390dcc
commit 67b8dec0d0
1 changed files with 3 additions and 0 deletions

View File

@ -503,6 +503,7 @@ QStringList QSqlDatabase::drivers()
}
}
QReadLocker locker(&dbDict()->lock);
const DriverDict dict = QSqlDatabasePrivate::driverDict();
for (const auto &[k, _] : dict.asKeyValueRange()) {
if (!list.contains(k))
@ -527,6 +528,7 @@ QStringList QSqlDatabase::drivers()
*/
void QSqlDatabase::registerSqlDriver(const QString& name, QSqlDriverCreatorBase *creator)
{
QWriteLocker locker(&dbDict()->lock);
delete QSqlDatabasePrivate::driverDict().take(name);
if (creator)
QSqlDatabasePrivate::driverDict().insert(name, creator);
@ -642,6 +644,7 @@ void QSqlDatabasePrivate::init(const QString &type)
drvName = type;
if (!driver) {
QReadLocker locker(&dbDict()->lock);
DriverDict dict = QSqlDatabasePrivate::driverDict();
for (DriverDict::const_iterator it = dict.constBegin();
it != dict.constEnd() && !driver; ++it) {