10#include "private/qcoreapplication_p.h"
17
18
24 constLast().priority >= priority ||
25 insertionOffset >= size()) {
33 QPostEventList::iterator at = std::upper_bound(begin() + insertionOffset, end(), ev);
40
41
54 Q_ASSERT(_ref.loadRelaxed() == 0);
63 if (threadId.loadAcquire() == QCoreApplicationPrivate::theMainThreadId.loadAcquire()) {
64 QCoreApplicationPrivate::theMainThread.storeRelease(
nullptr);
65 QCoreApplicationPrivate::theMainThreadId.storeRelaxed(
nullptr);
76 QThread *t = thread.loadAcquire();
77 thread.storeRelease(
nullptr);
80 for (
int i = 0; i < postEventList.size(); ++i) {
83 --pe.receiver->d_func()->postedEvents;
84 pe.event->m_posted =
false;
96 Q_ASSERT(_ref.loadRelaxed() != 0);
111 eventDispatcher.storeRelease(ed);
116
117
120 : QThread(*
new QThreadPrivate(data))
125 d_func()->threadState = QThreadPrivate::Running;
127 d_func()->m_statusOrPendingObjects.setStatusAndClearList(
128 QtPrivate::getBindingStatus({}));
139void QAdoptedThread::run()
142 qFatal(
"QAdoptedThread::run(): Internal error, this implementation should never be called.");
147 : threadData(threadData)
150 qCDebug(lcDeleteLater) <<
"Increased" << threadData->thread
151 <<
"scope level to" << threadData->scopeLevel;
157 qCDebug(lcDeleteLater) <<
"Decreased" << threadData->thread
158 <<
"scope level to" << threadData->scopeLevel;
163
164
166QThreadPrivate::QThreadPrivate(QThreadData *d)
167 : QObjectPrivate(), data(d)
174 stackSize = 128 * 1024;
175#elif defined(Q_OS_RTEMS)
176 Q_CONSTINIT
static bool envStackSizeOk =
false;
177 static const int envStackSize = qEnvironmentVariableIntValue(
"QT_DEFAULT_THREAD_STACK_SIZE", &envStackSizeOk);
179 stackSize = envStackSize;
182#if defined (Q_OS_WIN)
185 terminationEnabled =
true;
186 terminatePending =
false;
190 data =
new QThreadData;
193QThreadPrivate::~QThreadPrivate()
198 delete m_statusOrPendingObjects.list();
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
303
304
305
306
307
308
309
310
311
312
313
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
335
336
337
338
339
340
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
361
362
363
364
365
366
367
368
369
370
373
374
375
376
377
378
379
380
381
382
383
384
385
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
412
413
414
415QThread *QThread::currentThread()
417 QThreadData *data = QThreadData::current();
418 Q_ASSERT(data !=
nullptr);
419 return data->thread.loadAcquire();
423
424
425
426
427
428
429
430
431
432
433
434bool QThread::isMainThread()
noexcept
436 return currentThreadId() == QCoreApplicationPrivate::theMainThreadId.loadRelaxed();
440
441
442
443
444
445
446QThread::QThread(QObject *parent)
447 : QObject(*(
new QThreadPrivate), parent)
451 d->data->thread.storeRelaxed(
this);
455
456
457QThread::QThread(QThreadPrivate &dd, QObject *parent)
458 : QObject(dd, parent)
462 d->data->thread.storeRelaxed(
this);
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
487 QMutexLocker locker(&d->mutex);
488 if (d->threadState == QThreadPrivate::Finishing)
489 d->wait(locker, QDeadlineTimer::Forever);
490 if (d->threadState == QThreadPrivate::Running && !d->data->isAdopted)
491 qFatal(
"QThread: Destroyed while thread '%ls' is still running", qUtf16Printable(objectName()));
493 d->data->thread.storeRelease(
nullptr);
498
499
500
501
502
503bool QThread::isFinished()
const
506 QMutexLocker locker(&d->mutex);
507 return d->threadState >= QThreadPrivate::Finishing;
511
512
513
514
515
516bool QThread::isRunning()
const
519 QMutexLocker locker(&d->mutex);
520 return d->threadState == QThreadPrivate::Running;
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543void QThread::setStackSize(uint stackSize)
546 Q_ASSERT_X(!isRunning(),
"QThread::setStackSize",
547 "cannot change stack size while the thread is running");
548 QMutexLocker locker(&d->mutex);
549 d->stackSize = stackSize;
553
554
555
556
557
558uint QThread::stackSize()
const
561 QMutexLocker locker(&d->mutex);
566
567
568
569
570
571
572
573
574
575
576
577
578void QThread::setServiceLevel(QualityOfService serviceLevel)
581 QMutexLocker locker(&d->mutex);
582 if (d->threadState != QThreadPrivate::Running) {
583 d->serviceLevel = serviceLevel;
585 Q_ASSERT_X(isCurrentThread(),
"QThread::setServiceLevel",
586 "cannot change quality of service level of a separate, running, thread");
587 d->setQualityOfServiceLevel(serviceLevel);
592
593
594
595
596
597
598QThread::QualityOfService QThread::serviceLevel()
const
601 QMutexLocker locker(&d->mutex);
602 return d->serviceLevel;
606
607
608
609
610
611void QtPrivate::BindingStatusOrList::setStatusAndClearList(QBindingStatus *status)
noexcept
614 if (
auto pendingObjects = list()) {
615 for (
auto obj: *pendingObjects)
616 QObjectPrivate::get(obj)->reinitBindingStorageAfterThreadMove();
617 delete pendingObjects;
620 data.store(encodeBindingStatus(status), std::memory_order_release);
624
625
626
627
628
629
630
631
632
633
634
635
639 const auto status = QtPrivate::getBindingStatus(QtPrivate::QBindingStatusAccessToken{});
641 QMutexLocker locker(&d->mutex);
642 d->m_statusOrPendingObjects.setStatusAndClearList(status);
643 d->data->quitNow =
false;
646 return d->returnCode;
650 QEventLoop eventLoop;
651 int returnCode = eventLoop.exec();
661
662
663
664
665
666
667QBindingStatus *QtPrivate::BindingStatusOrList::addObjectUnlessAlreadyStatus(QObject *object)
669 if (
auto status = bindingStatus())
671 List *objectList = list();
673 objectList =
new List();
674 objectList->reserve(8);
675 data.store(encodeList(objectList), std::memory_order_relaxed);
677 objectList->push_back(object);
682
683
684
685void QtPrivate::BindingStatusOrList::removeObject(QObject *object)
687 List *objectList = list();
690 auto it = std::remove(objectList->begin(), objectList->end(), object);
691 objectList->erase(it, objectList->end());
694QBindingStatus *QThreadPrivate::addObjectWithPendingBindingStatusChange(QObject *obj)
696 if (
auto status = m_statusOrPendingObjects.bindingStatus())
698 QMutexLocker lock(&mutex);
699 return m_statusOrPendingObjects.addObjectUnlessAlreadyStatus(obj);
702void QThreadPrivate::removeObjectWithPendingBindingStatusChange(QObject *obj)
704 if (m_statusOrPendingObjects.bindingStatus())
706 QMutexLocker lock(&mutex);
707 m_statusOrPendingObjects.removeObject(obj);
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733void QThread::exit(
int returnCode)
736 QMutexLocker locker(&d->mutex);
738 d->returnCode = returnCode;
739 d->data->quitNow =
true;
740 for (
int i = 0; i < d->data->eventLoops.size(); ++i) {
741 QEventLoop *eventLoop = d->data->eventLoops.at(i);
742 eventLoop->exit(returnCode);
747
748
749
750
751
752
753
754
755
760
761
762
763
764
765
766
767
768
769
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794void QThread::setPriority(Priority priority)
796 if (priority == QThread::InheritPriority) {
797 qWarning(
"QThread::setPriority: Argument cannot be InheritPriority");
801 QMutexLocker locker(&d->mutex);
802 if (d->threadState != QThreadPrivate::Running) {
803 qWarning(
"QThread::setPriority: Cannot set priority, thread is not running");
806 d->setPriority(priority);
810
811
812
813
814
815
816
817QThread::Priority QThread::priority()
const
820 QMutexLocker locker(&d->mutex);
823 return Priority(d->priority & 0xffff);
827
828
829
830
831
832
833
834
835
836
837
838
841
842
843
844
845
846
847
848
849
850
851
854
855
856
857
858
859
860
861
862
863
864
865
866
869
870
871
872
873
874
875
876
877
878
879
880
881
882
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
959
960
961
962
963
964
966int QThread::loopLevel()
const
969 return d->data->eventLoops.size();
973
974
975
976
977
978
979
980
981
982
983
984
985Qt::HANDLE QThreadPrivate::threadId()
const noexcept
987 return data->threadId.loadRelaxed();
991
992
993
994
995
996bool QThread::isCurrentThread()
const noexcept
999 return QThread::currentThreadId() == d->threadId();
1004QThread::QThread(QObject *parent)
1005 : QObject(*(
new QThreadPrivate), parent)
1008 d->data->thread.storeRelaxed(
this);
1026void QThread::start(Priority priority)
1033void QThread::terminate()
1043void QThread::exit(
int returnCode)
1046 d->data->quitNow =
true;
1047 for (
int i = 0; i < d->data->eventLoops.size(); ++i) {
1048 QEventLoop *eventLoop = d->data->eventLoops.at(i);
1049 eventLoop->exit(returnCode);
1053bool QThread::wait(QDeadlineTimer deadline)
1059bool QThread::event(QEvent *event)
1061 return QObject::event(event);
1064Qt::HANDLE QThread::currentThreadIdImpl()
noexcept
1066 return Qt::HANDLE(currentThread());
1069QThread *QThread::currentThread()
1071 return QThreadData::current()->thread.loadAcquire();
1074bool QThread::isCurrentThread()
const noexcept
1079int QThread::idealThreadCount()
noexcept
1084void QThread::yieldCurrentThread()
1089bool QThread::isFinished()
const
1094bool QThread::isRunning()
const
1100void QThread::requestInterruption()
1105bool QThread::isInterruptionRequested()
const
1110void QThread::setTerminationEnabled(
bool)
1115Q_CONSTINIT
static QThreadData *data =
nullptr;
1119 if (!data && createIfNecessary) {
1120 data =
new QThreadData;
1121 data->thread =
new QAdoptedThread(data);
1122 data->threadId.storeRelaxed(Qt::HANDLE(data->thread.loadAcquire()));
1124 data->isAdopted =
true;
1125 if (!QCoreApplicationPrivate::theMainThreadId.loadAcquire()) {
1126 auto *mainThread = data->thread.loadRelaxed();
1127 mainThread->setObjectName(
"Qt mainThread");
1128 QCoreApplicationPrivate::theMainThread.storeRelease(mainThread);
1129 QCoreApplicationPrivate::theMainThreadId.storeRelaxed(data->threadId.loadRelaxed());
1142
1143
1144QThread::QThread(QThreadPrivate &dd, QObject *parent)
1145 : QObject(dd, parent)
1149 d->data->thread.storeRelaxed(
this);
1158 data->thread.storeRelease(
nullptr);
1162void QThread::setStackSize(uint stackSize)
1164 Q_UNUSED(stackSize);
1167uint QThread::stackSize()
const
1175
1176
1177
1178
1179
1180QAbstractEventDispatcher *QThread::eventDispatcher()
const
1183 return d->data->eventDispatcher.loadRelaxed();
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198void QThread::setEventDispatcher(QAbstractEventDispatcher *eventDispatcher)
1201 if (d->data->hasEventDispatcher()) {
1202 qWarning(
"QThread::setEventDispatcher: An event dispatcher has already been created for this thread");
1204 eventDispatcher->moveToThread(
this);
1205 if (eventDispatcher->thread() ==
this)
1206 d->data->eventDispatcher = eventDispatcher;
1208 qWarning(
"QThread::setEventDispatcher: Could not move event dispatcher to target thread");
1213
1214
1215
1216
1217
1218
1220#if QT_CONFIG(thread)
1223
1224
1225bool QThread::event(QEvent *event)
1227 if (event->type() == QEvent::Quit) {
1231 return QObject::event(event);
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1248void QThread::requestInterruption()
1251 if (d->threadId() == QCoreApplicationPrivate::theMainThreadId.loadAcquire()) {
1252 qWarning(
"QThread::requestInterruption has no effect on the main thread");
1255 QMutexLocker locker(&d->mutex);
1256 if (d->threadState != QThreadPrivate::Running)
1258 d->interruptionRequested.store(
true, std::memory_order_relaxed);
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287bool QThread::isInterruptionRequested()
const
1291 if (!d->interruptionRequested.load(std::memory_order_relaxed))
1294 QMutexLocker locker(&d->mutex);
1295 return d->threadState == QThreadPrivate::Running;
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1320class QThreadCreateThread :
public QThread
1323 explicit QThreadCreateThread(std::future<
void> &&future)
1324 : m_future(std::move(future))
1328 ~QThreadCreateThread()
1330 requestInterruption();
1341 std::future<
void> m_future;
1344QThread *QThread::createThreadImpl(std::future<
void> &&future)
1346 return new QThreadCreateThread(std::move(future));
1350
1351
1352
1353
1354
1355
1356
1357QDaemonThread::QDaemonThread(QObject *parent)
1361 connect(
this, &QThread::started,
1363 [](){ QThreadData::current()->requiresCoreApplication =
false; },
1364 Qt::DirectConnection);
1367QDaemonThread::~QDaemonThread()
1375#include "moc_qthread.cpp"
void addEvent(const QPostEvent &ev)
QScopedScopeLevelCounter(QThreadData *threadData)
~QScopedScopeLevelCounter()
QThreadData(int initialRefCount=1)
bool requiresCoreApplication
static void clearCurrentThreadData()
QAbstractEventDispatcher * createEventDispatcher()
static QAbstractEventDispatcher * createEventDispatcher(QThreadData *data)
QThreadPrivate(QThreadData *d=nullptr)
Combined button and popup list for selecting options.