static void connManager_cleanup()
{
// this is not atomic or thread-safe!
- delete connManager_ptr.load();
- connManager_ptr.store(0);
+ QNetworkConfigurationManagerPrivate *cmp = connManager_ptr.fetchAndStoreAcquire(0);
+ if (cmp)
+ cmp->cleanup();
}
void QNetworkConfigurationManagerPrivate::addPostRoutine()
if (QCoreApplicationPrivate::mainThread() == QThread::currentThread()) {
// right thread or no main thread yet
ptr->addPostRoutine();
- ptr->updateConfigurations();
+ ptr->initialize();
} else {
// wrong thread, we need to make the main thread do this
QObject *obj = new QObject;
QObject::connect(obj, SIGNAL(destroyed()), ptr, SLOT(addPostRoutine()), Qt::DirectConnection);
- ptr->updateConfigurations(); // this moves us to the main thread
+ ptr->initialize(); // this moves us to the right thread
obj->moveToThread(QCoreApplicationPrivate::mainThread());
obj->deleteLater();
}
qRegisterMetaType<QNetworkConfigurationPrivatePointer>("QNetworkConfigurationPrivatePointer");
}
+void QNetworkConfigurationManagerPrivate::initialize()
+{
+ //Two stage construction, because we only want to do this heavyweight work for the winner of the Q_GLOBAL_STATIC race.
+ bearerThread = new QThread();
+ bearerThread->moveToThread(QCoreApplicationPrivate::mainThread()); // because cleanup() is called in main thread context.
+ moveToThread(bearerThread);
+ bearerThread->start();
+ updateConfigurations();
+}
+
QNetworkConfigurationManagerPrivate::~QNetworkConfigurationManagerPrivate()
{
QMutexLocker locker(&mutex);
qDeleteAll(sessionEngines);
+ if (bearerThread)
+ bearerThread->quit();
+}
+
+void QNetworkConfigurationManagerPrivate::cleanup()
+{
+ QThread* thread = bearerThread;
+ deleteLater();
+ if (thread->wait(5000))
+ delete thread;
}
QNetworkConfiguration QNetworkConfigurationManagerPrivate::defaultConfiguration() const
if (qobject_cast<QBearerEngine *>(sender()))
return;
- if (thread() != QCoreApplicationPrivate::mainThread()) {
- if (thread() != QThread::currentThread())
- return;
-
- moveToThread(QCoreApplicationPrivate::mainThread());
- }
-
updating = false;
#ifndef QT_NO_LIBRARY
else
sessionEngines.append(engine);
- engine->moveToThread(QCoreApplicationPrivate::mainThread());
+ engine->moveToThread(bearerThread);
connect(engine, SIGNAL(updateCompleted()),
this, SLOT(updateConfigurations()));
if (firstUpdate) {
firstUpdate = false;
QList<QBearerEngine*> enginesToInitialize = sessionEngines; //shallow copy the list in case it is modified when we unlock mutex
- Qt::ConnectionType connectionType;
- if (QCoreApplicationPrivate::mainThread() == QThread::currentThread())
- connectionType = Qt::DirectConnection;
- else
- connectionType = Qt::BlockingQueuedConnection;
locker.unlock();
foreach (QBearerEngine* engine, enginesToInitialize) {
- QMetaObject::invokeMethod(engine, "initialize", connectionType);
+ QMetaObject::invokeMethod(engine, "initialize", Qt::BlockingQueuedConnection);
}
}
}