71QEventLoop::QEventLoop(QObject *parent)
72 : QObject(*
new QEventLoopPrivate, parent)
75 QThreadData *threadData = d->threadData.loadRelaxed();
76 if (!QCoreApplication::instanceExists() && threadData->requiresCoreApplication) {
77 qWarning(
"QEventLoop: Cannot be used without QCoreApplication");
79 threadData->ensureEventDispatcher();
135int QEventLoop::exec(ProcessEventsFlags flags)
138 auto threadData = d->threadData.loadRelaxed();
141 QMutexLocker locker(&
static_cast<QThreadPrivate *>(QObjectPrivate::get(threadData->thread.loadAcquire()))->mutex);
142 if (threadData->quitNow)
146 qWarning(
"QEventLoop::exec: instance %p has already called exec()",
this);
150 struct LoopReference {
151 QEventLoopPrivate *d;
152 QMutexLocker<QMutex> &locker;
154 bool exceptionCaught;
155 LoopReference(QEventLoopPrivate *d, QMutexLocker<QMutex> &locker) : d(d), locker(locker), exceptionCaught(
true)
158 d->exit.storeRelease(
false);
160 auto threadData = d->threadData.loadRelaxed();
161 ++threadData->loopLevel;
162 threadData->eventLoops.push(d->q_func());
169 if (exceptionCaught) {
170 const char *type =
nullptr;
173 if (
const std::type_info *ti = abi::__cxa_current_exception_type())
176 qWarning(
"Qt has caught an exception thrown from an event handler. Throwing \n"
177 "exceptions from an event handler is not supported in Qt. \n"
178 "You must not let any exception whatsoever propagate through Qt code.%s%s",
179 type ?
"\nException was of type: " :
"", type ? type :
"");
182 auto threadData = d->threadData.loadRelaxed();
183 QEventLoop *eventLoop = threadData->eventLoops.pop();
184 Q_ASSERT_X(eventLoop == d->q_func(),
"QEventLoop::exec()",
"internal error");
187 --threadData->loopLevel;
190 LoopReference ref(d, locker);
193 QCoreApplication *app = QCoreApplication::instance();
194 if (app && app->thread() == thread())
195 QCoreApplication::removePostedEvents(app, QEvent::Quit);
197 while (!d->exit.loadAcquire())
198 processEvents(flags | WaitForMoreEvents | EventLoopExec);
200 ref.exceptionCaught =
false;
201 return d->returnCode.loadRelaxed();