70QEventLoop::QEventLoop(QObject *parent)
71 : QObject(*
new QEventLoopPrivate, parent)
74 QThreadData *threadData = d->threadData.loadRelaxed();
75 if (!QCoreApplication::instanceExists() && threadData->requiresCoreApplication) {
76 qWarning(
"QEventLoop: Cannot be used without QCoreApplication");
78 threadData->ensureEventDispatcher();
134int QEventLoop::exec(ProcessEventsFlags flags)
137 auto threadData = d->threadData.loadRelaxed();
140 QMutexLocker locker(&
static_cast<QThreadPrivate *>(QObjectPrivate::get(threadData->thread.loadAcquire()))->mutex);
141 if (threadData->quitNow)
145 qWarning(
"QEventLoop::exec: instance %p has already called exec()",
this);
149 struct LoopReference {
150 QEventLoopPrivate *d;
151 QMutexLocker<QMutex> &locker;
153 bool exceptionCaught;
154 LoopReference(QEventLoopPrivate *d, QMutexLocker<QMutex> &locker) : d(d), locker(locker), exceptionCaught(
true)
157 d->exit.storeRelease(
false);
159 auto threadData = d->threadData.loadRelaxed();
160 ++threadData->loopLevel;
161 threadData->eventLoops.push(d->q_func());
168 if (exceptionCaught) {
169 const char *type =
nullptr;
172 if (
const std::type_info *ti = abi::__cxa_current_exception_type())
175 qWarning(
"Qt has caught an exception thrown from an event handler. Throwing \n"
176 "exceptions from an event handler is not supported in Qt. \n"
177 "You must not let any exception whatsoever propagate through Qt code.%s%s",
178 type ?
"\nException was of type: " :
"", type ? type :
"");
181 auto threadData = d->threadData.loadRelaxed();
182 QEventLoop *eventLoop = threadData->eventLoops.pop();
183 Q_ASSERT_X(eventLoop == d->q_func(),
"QEventLoop::exec()",
"internal error");
186 --threadData->loopLevel;
189 LoopReference ref(d, locker);
192 QCoreApplication *app = QCoreApplication::instance();
193 if (app && app->thread() == thread())
194 QCoreApplication::removePostedEvents(app, QEvent::Quit);
196 while (!d->exit.loadAcquire())
197 processEvents(flags | WaitForMoreEvents | EventLoopExec);
199 ref.exceptionCaught =
false;
200 return d->returnCode.loadRelaxed();