Skip to content

Commit

Permalink
eliminate main thread store
Browse files Browse the repository at this point in the history
fixes RCC runtime crash on NetBSD

Signed-off-by: Ivailo Monev <[email protected]>
  • Loading branch information
fluxer committed Nov 23, 2020
1 parent 2635c8a commit 8a7f7fe
Show file tree
Hide file tree
Showing 7 changed files with 3 additions and 55 deletions.
13 changes: 0 additions & 13 deletions src/core/kernel/qcoreapplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,13 +169,6 @@ struct QCoreApplicationData {
~QCoreApplicationData() {
#ifndef QT_NO_LIBRARY
delete app_libpaths;
#endif
#ifndef QT_NO_QOBJECT
// cleanup the QAdoptedThread created for the main() thread
if (QCoreApplicationPrivate::theMainThread) {
QThreadData *data = QThreadData::get2(QCoreApplicationPrivate::theMainThread);
data->deref(); // deletes the data and the adopted thread
}
#endif
}

Expand All @@ -200,10 +193,6 @@ QCoreApplicationPrivate::QCoreApplicationPrivate(int &aargc, char **aargv)
argv = (char **)&empty; // ouch! careful with QCoreApplication::argv()!
}
QCoreApplicationPrivate::is_app_closing = false;

// note: this call to QThread::currentThread() may end up setting theMainThread!
if (Q_UNLIKELY(QThread::currentThread() != theMainThread))
qWarning("WARNING: QApplication was not created in the main() thread.");
}

QCoreApplicationPrivate::~QCoreApplicationPrivate()
Expand Down Expand Up @@ -231,8 +220,6 @@ void QCoreApplicationPrivate::createEventDispatcher()
eventDispatcher = new QEventDispatcherUNIX(q);
}

QThread *QCoreApplicationPrivate::theMainThread = Q_NULLPTR;

#if !defined (QT_NO_DEBUG)
void QCoreApplicationPrivate::checkReceiverThread(QObject *receiver)
{
Expand Down
3 changes: 0 additions & 3 deletions src/core/kernel/qcoreapplication_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,6 @@ class Q_CORE_EXPORT QCoreApplicationPrivate : public QObjectPrivate
virtual void createEventDispatcher();
static void removePostedEvent(QEvent *);

static QThread *theMainThread;
static inline QThread *mainThread()
{ Q_ASSERT(theMainThread != 0); return theMainThread; }
static bool checkInstance(const char *method);
static void sendPostedEvents(QObject *receiver, int event_type, QThreadData *data);

Expand Down
7 changes: 1 addition & 6 deletions src/core/plugin/qfactoryloader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ QFactoryLoader::QFactoryLoader(const char *iid,
Qt::CaseSensitivity cs)
: QObject(*new QFactoryLoaderPrivate)
{
moveToThread(QCoreApplicationPrivate::mainThread());
Q_D(QFactoryLoader);
d->iid = iid;
d->cs = cs;
Expand Down Expand Up @@ -223,11 +222,7 @@ QObject *QFactoryLoader::instance(const QString &key) const
QString lowered = d->cs ? key : key.toLower();
if (QLibraryPrivate* library = d->keyMap.value(lowered)) {
if (library->instance || library->loadPlugin()) {
if (QObject *obj = library->instance()) {
if (obj && !obj->parent())
obj->moveToThread(QCoreApplicationPrivate::mainThread());
return obj;
}
return library->instance();
}
}
return 0;
Expand Down
11 changes: 0 additions & 11 deletions src/core/thread/qthread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,6 @@ QThreadData::~QThreadData()
{
Q_ASSERT(_ref == 0);

// In the odd case that Qt is running on a secondary thread, the main
// thread instance will have been dereffed asunder because of the deref in
// QThreadData::current() and the deref in the pthread_destroy. To avoid
// crashing during QCoreApplicationData's global static cleanup we need to
// safeguard the main thread here.. This fix is a bit crude, but it solves
// the problem...
if (this->thread == QCoreApplicationPrivate::theMainThread) {
QCoreApplicationPrivate::theMainThread = Q_NULLPTR;
QThreadData::clearCurrentThreadData();
}

delete thread;
thread = Q_NULLPTR;

Expand Down
2 changes: 0 additions & 2 deletions src/core/thread/qthread_unix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,6 @@ QThreadData *QThreadData::current()

data->isAdopted = true;
data->threadId = (Qt::HANDLE)pthread_self();
if (!QCoreApplicationPrivate::theMainThread)
QCoreApplicationPrivate::theMainThread = data->thread;
}
return data;
}
Expand Down
16 changes: 2 additions & 14 deletions src/network/bearer/qnetworkconfigmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@
#include <QtCore/qstringlist.h>
#include <QtCore/qcoreapplication.h>
#include <QtCore/qmutex.h>
#include <QtCore/qthread.h>
#include <QtCore/qcoreapplication_p.h>

#ifndef QT_NO_BEARERMANAGEMENT

Expand Down Expand Up @@ -74,18 +72,8 @@ static QNetworkConfigurationManagerPrivate *connManager()
if (!(ptr = connManager_ptr.fetchAndAddAcquire(0))) {
ptr = new QNetworkConfigurationManagerPrivate;

if (QCoreApplicationPrivate::mainThread() == QThread::currentThread()) {
// right thread or no main thread yet
ptr->addPostRoutine();
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->initialize(); // this moves us to the right thread
obj->moveToThread(QCoreApplicationPrivate::mainThread());
obj->deleteLater();
}
ptr->addPostRoutine();
ptr->initialize();

connManager_ptr.fetchAndStoreRelease(ptr);
}
Expand Down
6 changes: 0 additions & 6 deletions src/network/bearer/qnetworkconfigmanager_p.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,10 @@

#include "qnetworkconfigmanager_p.h"
#include "qbearerplugin_p.h"

#include <QtCore/qfactoryloader_p.h>

#include <QtCore/qdebug.h>
#include <QtCore/qtimer.h>
#include <QtCore/qstringlist.h>
#include <QtCore/qcoreapplication_p.h>

#ifndef QT_NO_BEARERMANAGEMENT

Expand All @@ -59,7 +56,6 @@ QNetworkConfigurationManagerPrivate::QNetworkConfigurationManagerPrivate()

void QNetworkConfigurationManagerPrivate::initialize()
{
moveToThread(QCoreApplicationPrivate::mainThread()); // because cleanup() is called in main thread context.
updateConfigurations();
}

Expand Down Expand Up @@ -366,8 +362,6 @@ void QNetworkConfigurationManagerPrivate::updateConfigurations()
else
sessionEngines.append(engine);

engine->moveToThread(QCoreApplicationPrivate::mainThread());

connect(engine, SIGNAL(updateCompleted()),
this, SLOT(updateConfigurations()),
Qt::QueuedConnection);
Expand Down

0 comments on commit 8a7f7fe

Please sign in to comment.