11#include "private/qcoreapplication_p.h"
17using namespace Qt::StringLiterals;
20
21
27 constLast().priority >= priority ||
28 insertionOffset >= size()) {
36 QPostEventList::iterator at = std::upper_bound(begin() + insertionOffset, end(), ev);
43
44
49 Q_ASSERT(_ref.loadRelaxed() == 0);
52 if (threadId.loadAcquire() == QCoreApplicationPrivate::theMainThreadId.loadAcquire()) {
53 QCoreApplicationPrivate::theMainThread.storeRelease(
nullptr);
54 QCoreApplicationPrivate::theMainThreadId.storeRelaxed(
nullptr);
65 QThread *t = thread.loadAcquire();
66 thread.storeRelease(
nullptr);
78 constexpr int MaxPasses = 16;
80 QList<QPostEvent> pending;
81 for (
int pass = 0;; ++pass) {
83 const auto locker = qt_scoped_lock(postEventList.mutex);
84 if (postEventList.isEmpty())
86 pending.swap(
static_cast<QList<QPostEvent> &>(postEventList));
87 postEventList.startOffset = 0;
88 postEventList.insertionOffset = 0;
93 for (
const QPostEvent &pe : std::as_const(pending)) {
96 pe.receiver->d_func()->postedEvents.fetchAndSubRelaxed(1);
97 pe.event->m_posted =
false;
100 if (pass == MaxPasses) {
101 qWarning(
"QThreadData::clearEvents: giving up after %d passes, leaking %lld event(s)",
102 MaxPasses, qlonglong(pending.size()));
107 for (
const QPostEvent &pe : std::as_const(pending))
116 eventDispatcher.storeRelease(ed);
121
122
125 : QThread(*
new QThreadPrivate(data))
132 Qt::HANDLE id = QThread::currentThreadId();
133 data->threadId.storeRelaxed(id);
134 if (!QCoreApplicationPrivate::theMainThreadId.loadAcquire()) {
136 QCoreApplicationPrivate::theMainThread.storeRelease(
this);
137 QCoreApplicationPrivate::theMainThreadId.storeRelaxed(id);
140 d_func()->setObjectNameWithoutBindings(u"Qt mainThread"_s);
146 d_func()->threadState = QThreadPrivate::Running;
149 QMutexLocker lock(&d_func()->mutex);
150 d_func()->data->m_statusOrPendingObjects.setStatusAndClearList(
151 QtPrivate::getBindingStatus({}));
163void QAdoptedThread::run()
166 qFatal(
"QAdoptedThread::run(): Internal error, this implementation should never be called.");
172
173
175QThreadPrivate::QThreadPrivate(QThreadData *d)
176 : QObjectPrivate(), data(d)
183 stackSize = 128 * 1024;
184#elif defined(Q_OS_RTEMS)
185 Q_CONSTINIT
static bool envStackSizeOk =
false;
186 static const int envStackSize = qEnvironmentVariableIntValue(
"QT_DEFAULT_THREAD_STACK_SIZE", &envStackSizeOk);
188 stackSize = envStackSize;
192 data =
new QThreadData;
195QThreadPrivate::~QThreadPrivate()
200 delete data->m_statusOrPendingObjects.list();
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
301
302
303
306
307
308
309
310
311
312
313
314
315
316
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
338
339
340
341
342
343
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
364
365
366
367
368
369
370
371
372
373
376
377
378
379
380
381
382
383
384
385
386
387
388
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
415
416
417
418QThread *QThread::currentThread()
420 QThreadData *data = QThreadData::current();
421 Q_ASSERT(data !=
nullptr);
422 return data->thread.loadAcquire();
426
427
428
429
430
431
432
433
434
435
436
437bool QThread::isMainThread()
noexcept
439 return currentThreadId() == QCoreApplicationPrivate::theMainThreadId.loadRelaxed();
443
444
445
446
447
448
449QThread::QThread(QObject *parent)
450 : QObject(*(
new QThreadPrivate), parent)
454 d->data->thread.storeRelaxed(
this);
458
459
460QThread::QThread(QThreadPrivate &dd, QObject *parent)
461 : QObject(dd, parent)
465 d->data->thread.storeRelaxed(
this);
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
490 QMutexLocker locker(&d->mutex);
491 if (d->threadState == QThreadPrivate::Finishing)
492 d->wait(locker, QDeadlineTimer::Forever);
493 if (d->threadState == QThreadPrivate::Running && !d->data->isAdopted)
494 qFatal(
"QThread: Destroyed while thread '%ls' is still running", qUtf16Printable(objectName()));
496 d->data->thread.storeRelease(
nullptr);
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516bool QThread::isFinished()
const
519 QMutexLocker locker(&d->mutex);
520 return d->threadState >= QThreadPrivate::Finishing;
524
525
526
527
528
529
530
531
532
533
534bool QThread::isRunning()
const
537 QMutexLocker locker(&d->mutex);
538 return d->threadState == QThreadPrivate::Running;
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561void QThread::setStackSize(uint stackSize)
564 Q_ASSERT_X(!isRunning(),
"QThread::setStackSize",
565 "cannot change stack size while the thread is running");
566 QMutexLocker locker(&d->mutex);
567 d->stackSize = stackSize;
571
572
573
574
575
576uint QThread::stackSize()
const
579 QMutexLocker locker(&d->mutex);
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
605
606
607
608
609
610
611
612
613
614
615
616
617void QThread::setServiceLevel(QualityOfService serviceLevel)
620 QMutexLocker locker(&d->mutex);
621 if (d->threadState != QThreadPrivate::Running) {
622 d->serviceLevel = serviceLevel;
624 Q_ASSERT_X(isCurrentThread(),
"QThread::setServiceLevel",
625 "cannot change quality of service level of a separate, running, thread");
626 d->setQualityOfServiceLevel(serviceLevel);
631
632
633
634
635
636
637QThread::QualityOfService QThread::serviceLevel()
const
640 QMutexLocker locker(&d->mutex);
641 return d->serviceLevel;
647
648
649
650
651
654 if (
auto pendingObjects = list()) {
655 for (
auto obj: *pendingObjects)
656 QObjectPrivate::get(obj)->reinitBindingStorageAfterThreadMove();
657 delete pendingObjects;
660 data.store(encodeBindingStatus(status), std::memory_order_release);
666
667
668
669
670
671
672
673
674
675
676
677
681 const auto status = QtPrivate::getBindingStatus(QtPrivate::QBindingStatusAccessToken{});
683 QMutexLocker locker(&d->mutex);
684 d->data->m_statusOrPendingObjects.setStatusAndClearList(status);
685 d->data->quitNow =
false;
688 return d->returnCode;
692 QEventLoop eventLoop;
693 int returnCode = eventLoop.exec();
703
704
705
706
707
708
709QBindingStatus *QtPrivate::BindingStatusOrList::addObjectUnlessAlreadyStatus(QObject *object)
711 if (
auto status = bindingStatus())
713 List *objectList = list();
715 objectList =
new List();
716 objectList->reserve(8);
717 data.store(encodeList(objectList), std::memory_order_relaxed);
719 objectList->push_back(object);
724
725
726
727void QtPrivate::BindingStatusOrList::removeObject(QObject *object)
729 List *objectList = list();
732 auto it = std::remove(objectList->begin(), objectList->end(), object);
733 objectList->erase(it, objectList->end());
736QBindingStatus *QThreadPrivate::addObjectWithPendingBindingStatusChange(QObject *obj)
738 if (
auto status = data->m_statusOrPendingObjects.bindingStatus())
740 QMutexLocker lock(&mutex);
741 return data->m_statusOrPendingObjects.addObjectUnlessAlreadyStatus(obj);
744void QThreadPrivate::removeObjectWithPendingBindingStatusChange(QObject *obj)
746 if (data->m_statusOrPendingObjects.bindingStatus())
748 QMutexLocker lock(&mutex);
749 data->m_statusOrPendingObjects.removeObject(obj);
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775void QThread::exit(
int returnCode)
778 QMutexLocker locker(&d->mutex);
780 d->returnCode = returnCode;
781 d->data->quitNow =
true;
782 for (
int i = 0; i < d->data->eventLoops.size(); ++i) {
783 QEventLoop *eventLoop = d->data->eventLoops.at(i);
784 eventLoop->exit(returnCode);
789
790
791
792
793
794
795
796
797
802
803
804
805
806
807
808
809
810
811
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836void QThread::setPriority(Priority priority)
838 if (priority == QThread::InheritPriority) {
839 qWarning(
"QThread::setPriority: Argument cannot be InheritPriority");
843 QMutexLocker locker(&d->mutex);
844 if (d->threadState != QThreadPrivate::Running) {
845 qWarning(
"QThread::setPriority: Cannot set priority, thread is not running");
848 d->setPriority(priority);
852
853
854
855
856
857
858
859QThread::Priority QThread::priority()
const
862 QMutexLocker locker(&d->mutex);
865 return Priority(d->priority & 0xffff);
869
870
871
872
873
874
875
876
877
878
879
880
883
884
885
886
887
888
889
890
891
892
893
896
897
898
899
900
901
902
903
904
905
906
907
908
911
912
913
914
915
916
917
918
919
920
921
922
923
924
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984bool QThread::wait(QDeadlineTimer deadline)
987 QMutexLocker locker(&d->mutex);
989 if (d->threadState == QThreadPrivate::NotStarted || d->threadState == QThreadPrivate::Finished)
991 if (isCurrentThread()) {
992 qWarning(
"QThread::wait: Thread tried to wait on itself");
995 return d->wait(locker, deadline);
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1020
1021
1022
1023
1024
1025
1027int QThread::loopLevel()
const
1030 return d->data->eventLoops.size();
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046Qt::HANDLE QThreadPrivate::threadId()
const noexcept
1048 return data->threadId.loadRelaxed();
1052
1053
1054
1055
1056
1057bool QThread::isCurrentThread()
const noexcept
1060 return QThread::currentThreadId() == d->threadId();
1065QThread::QThread(QObject *parent)
1066 : QObject(*(
new QThreadPrivate), parent)
1069 d->data->thread.storeRelaxed(
this);
1077QThread *QThread::createThreadImpl(std::future<
void>&&)
1092void QThread::start(Priority priority)
1099void QThread::terminate()
1109void QThread::exit(
int returnCode)
1112 d->data->quitNow =
true;
1113 for (
int i = 0; i < d->data->eventLoops.size(); ++i) {
1114 QEventLoop *eventLoop = d->data->eventLoops.at(i);
1115 eventLoop->exit(returnCode);
1119bool QThread::wait(QDeadlineTimer deadline)
1125bool QThread::event(QEvent *event)
1127 return QObject::event(event);
1130Qt::HANDLE QThread::currentThreadIdImpl()
noexcept
1132 return Qt::HANDLE(currentThread());
1135QThread *QThread::currentThread()
1137 return QThreadData::current()->thread.loadAcquire();
1140bool QThread::isMainThread()
noexcept
1145bool QThread::isCurrentThread()
const noexcept
1150int QThread::idealThreadCount()
noexcept
1155void QThread::yieldCurrentThread()
1160bool QThread::isFinished()
const
1165bool QThread::isRunning()
const
1171void QThread::requestInterruption()
1176bool QThread::isInterruptionRequested()
const
1181void QThread::setTerminationEnabled(
bool)
1185void QThread::setServiceLevel(QualityOfService)
1190Q_CONSTINIT
static QThreadData *data =
nullptr;
1199 Q_ASSERT(!currentThreadData());
1200 data =
new QThreadData;
1201 data->thread =
new QAdoptedThread(data);
1212
1213
1214QThread::QThread(QThreadPrivate &dd, QObject *parent)
1215 : QObject(dd, parent)
1219 d->data->thread.storeRelaxed(
this);
1228 data->thread.storeRelease(
nullptr);
1232void QThread::setStackSize(uint stackSize)
1234 Q_UNUSED(stackSize);
1237uint QThread::stackSize()
const
1245
1246
1247
1248
1249
1250QAbstractEventDispatcher *QThread::eventDispatcher()
const
1253 return d->data->eventDispatcher.loadRelaxed();
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268void QThread::setEventDispatcher(QAbstractEventDispatcher *eventDispatcher)
1271 if (d->data->hasEventDispatcher()) {
1272 qWarning(
"QThread::setEventDispatcher: An event dispatcher has already been created for this thread");
1274 eventDispatcher->moveToThread(
this);
1275 if (eventDispatcher->thread() ==
this)
1276 d->data->eventDispatcher.storeRelaxed(eventDispatcher);
1278 qWarning(
"QThread::setEventDispatcher: Could not move event dispatcher to target thread");
1283
1284
1285
1286
1287
1288
1290#if QT_CONFIG(thread)
1293
1294
1295bool QThread::event(QEvent *event)
1297 if (event->type() == QEvent::Quit) {
1301 return QObject::event(event);
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1321void QThread::requestInterruption()
1324 if (d->threadId() == QCoreApplicationPrivate::theMainThreadId.loadAcquire()) {
1325 qWarning(
"QThread::requestInterruption has no effect on the main thread");
1328 QMutexLocker locker(&d->mutex);
1329 if (d->threadState != QThreadPrivate::Running)
1331 d->interruptionRequested.store(
true, std::memory_order_relaxed);
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360bool QThread::isInterruptionRequested()
const
1364 if (!d->interruptionRequested.load(std::memory_order_relaxed))
1367 QMutexLocker locker(&d->mutex);
1368 return d->threadState == QThreadPrivate::Running;
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1393class QThreadCreateThread :
public QThread
1396 explicit QThreadCreateThread(std::future<
void> &&future)
1397 : m_future(std::move(future))
1401 ~QThreadCreateThread()
1403 requestInterruption();
1414 std::future<
void> m_future;
1417QThread *QThread::createThreadImpl(std::future<
void> &&future)
1419 return new QThreadCreateThread(std::move(future));
1423
1424
1425
1426
1427
1428
1429
1430QDaemonThread::QDaemonThread(QObject *parent)
1434 connect(
this, &QThread::started,
1436 [](){ QThreadData::current()->requiresCoreApplication =
false; },
1437 Qt::DirectConnection);
1440QDaemonThread::~QDaemonThread()
1448#include "moc_qthread.cpp"
void addEvent(const QPostEvent &ev)
static void clearCurrentThreadData()
QAbstractEventDispatcher * createEventDispatcher()
static QAbstractEventDispatcher * createEventDispatcher(QThreadData *data)
QThreadPrivate(QThreadData *d=nullptr)
Combined button and popup list for selecting options.