9#include <private/qthread_p.h>
10#include <private/qcoreapplication_p.h>
11#include <private/qfreelist_p.h>
13#include <QtCore/q26numeric.h>
17using namespace std::chrono_literals;
44 Size5 = QtTimerIdFreeListConstants::MaxIndex - Offset5
57Q_GLOBAL_STATIC(QtTimerIdFreeList, timerIdFreeList)
59template <
typename T>
static T fromDuration(std::chrono::nanoseconds interval)
61 using namespace std::chrono;
62 qint64 value = ceil<milliseconds>(interval).count();
63 return q26::saturate_cast<T>(value);
66#if QT_VERSION < QT_VERSION_CHECK(7
, 0
, 0
)
67static inline QAbstractEventDispatcherV2 *v2(QAbstractEventDispatcher *self)
69 if (QAbstractEventDispatcherPrivate::get(self)->isV2)
70 return static_cast<QAbstractEventDispatcherV2 *>(self);
74static inline const QAbstractEventDispatcherV2 *v2(
const QAbstractEventDispatcher *self)
76 if (QAbstractEventDispatcherPrivate::get(self)->isV2)
77 return static_cast<
const QAbstractEventDispatcherV2 *>(self);
82QAbstractEventDispatcherPrivate::QAbstractEventDispatcherPrivate()
87 if (!timerIdFreeList.isDestroyed())
88 (
void)timerIdFreeList();
91QAbstractEventDispatcherPrivate::~QAbstractEventDispatcherPrivate()
94int QAbstractEventDispatcherPrivate::allocateTimerId()
101 if (QtTimerIdFreeList *fl = timerIdFreeList())
106void QAbstractEventDispatcherPrivate::releaseTimerId(
int timerId)
110 if (QtTimerIdFreeList *fl = timerIdFreeList())
111 fl->release(timerId);
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
152
153
154
155
156
157
158
159
162
163
164QAbstractEventDispatcher::QAbstractEventDispatcher(QObject *parent)
165 : QObject(*
new QAbstractEventDispatcherPrivate, parent) {}
168
169
170QAbstractEventDispatcher::QAbstractEventDispatcher(QAbstractEventDispatcherPrivate &dd,
172 : QObject(dd, parent) {}
175
176
177QAbstractEventDispatcher::~QAbstractEventDispatcher()
180 QThreadData *data = QThreadData::currentThreadData();
181 if (data && data->eventDispatcher.loadRelaxed() ==
this)
182 data->eventDispatcher.storeRelaxed(
nullptr);
186
187
188
189
190
191
192
193
194QAbstractEventDispatcher *QAbstractEventDispatcher::instance(QThread *thread)
197 QThreadData *data = thread ? QThreadData::get2(thread) : QThreadData::current();
198 return data->eventDispatcher.loadRelaxed();
202
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
234
235
236
237
238
239
240
243
244
245
246
247
248
251
252
253
254
255
256
259
260
261
262
263
264int QAbstractEventDispatcher::registerTimer(qint64 interval, Qt::TimerType timerType, QObject *object)
266 return int(registerTimer(interval * 1ms, timerType, object));
270
271
272
273
274
275
276Qt::TimerId QAbstractEventDispatcher::registerTimer(Duration interval, Qt::TimerType timerType,
279 auto id = Qt::TimerId(QAbstractEventDispatcherPrivate::allocateTimerId());
280#if QT_VERSION < QT_VERSION_CHECK(7
, 0
, 0
)
281 if (QAbstractEventDispatcherV2 *self = v2(
this))
282 self->registerTimer(id, interval, timerType, object);
284 registerTimer(qToUnderlying(id), fromDuration<qint64>(interval), timerType, object);
286 registerTimer(id, interval, timerType, object);
291#if QT_VERSION < QT_VERSION_CHECK(7
, 0
, 0
)
293
294
295
296
297
300
301
302
303
304
305
306
309
310
311
312
313
314
315
318
319
320
321
322
323
324
325
328
329
330
331
332
333
334
335
338
339
340
341
342
343
344
345
348
349
350
351
352
353
354
355
358
359
360
361
362
363
364
365
369
370
371
372
373
374
375
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
396
397
398
399
400
405
406
407void QAbstractEventDispatcher::startingUp()
411
412
413void QAbstractEventDispatcher::closingDown()
417
418
419
420
421
422
423
424
425
426
427
429
430
431
432
434
435
436
437
439
440
441
442
444
445
446
447
448
449
452
453
454
455
456
457
458
459
460
461
463
464
465
466
467
468
470
471
472
473
475
476
477
478
479
480
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507void QAbstractEventDispatcher::installNativeEventFilter(QAbstractNativeEventFilter *filterObj)
509 Q_D(QAbstractEventDispatcher);
512 d->eventFilters.removeAll(
nullptr);
513 d->eventFilters.removeAll(filterObj);
514 d->eventFilters.prepend(filterObj);
518
519
520
521
522
523
524
525
526
527
528
529
530void QAbstractEventDispatcher::removeNativeEventFilter(QAbstractNativeEventFilter *filter)
532 Q_D(QAbstractEventDispatcher);
533 if (
const auto idx = d->eventFilters.indexOf(filter); idx >= 0)
534 d->eventFilters[idx] =
nullptr;
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556bool QAbstractEventDispatcher::filterNativeEvent(
const QByteArray &eventType,
void *message, qintptr *result)
558 Q_D(QAbstractEventDispatcher);
559 if (!d->eventFilters.isEmpty()) {
562 QScopedScopeLevelCounter scopeLevelCounter(d->threadData.loadAcquire());
563 for (qsizetype i = 0; i < d->eventFilters.size(); ++i) {
564 QAbstractNativeEventFilter *filter = d->eventFilters.at(i);
567 if (filter->nativeEventFilter(eventType, message, result))
575
576
577
578
579
580
583
584
585
586
587
588
590#if QT_VERSION < QT_VERSION_CHECK(7
, 0
, 0
)
591void QAbstractEventDispatcher::registerTimer(Qt::TimerId timerId, Duration interval,
592 Qt::TimerType timerType, QObject *object)
594 if (QAbstractEventDispatcherV2 *self = v2(
this))
595 self->registerTimer(timerId, interval, timerType, object);
597 registerTimer(
int(timerId), fromDuration<qint64>(interval), timerType, object);
600bool QAbstractEventDispatcher::unregisterTimer(Qt::TimerId timerId)
602 if (QAbstractEventDispatcherV2 *self = v2(
this))
603 return self->unregisterTimer(timerId);
604 return unregisterTimer(
int(timerId));
607QList<QAbstractEventDispatcher::TimerInfoV2>
608QAbstractEventDispatcher::timersForObject(QObject *object)
const
610 if (
const QAbstractEventDispatcherV2 *self = v2(
this))
611 return self->timersForObject(object);
612 QList<TimerInfo> timers = registeredTimers(object);
613 QList<TimerInfoV2> result;
614 result.reserve(timers.size());
615 for (
const TimerInfo &t : timers)
616 result.emplaceBack(TimerInfoV2{ t.interval * 1ms, Qt::TimerId(t.timerId), t.timerType });
620QAbstractEventDispatcher::Duration
621QAbstractEventDispatcher::remainingTime(Qt::TimerId timerId)
const
623 if (
const QAbstractEventDispatcherV2 *self = v2(
this))
624 return self->remainingTime(timerId);
625 return const_cast<QAbstractEventDispatcher *>(
this)->remainingTime(
int(timerId)) * 1ms;
629
630
631
632
633
634
635
636
637
638
639
642
643
644QAbstractEventDispatcherV2::QAbstractEventDispatcherV2(QObject *parent)
645 : QAbstractEventDispatcherV2(*
new QAbstractEventDispatcherPrivate, parent)
650
651
652QAbstractEventDispatcherV2::QAbstractEventDispatcherV2(QAbstractEventDispatcherPrivate &dd,
654 : QAbstractEventDispatcher((dd.isV2 =
true, dd), parent)
659
660
661QAbstractEventDispatcherV2::~QAbstractEventDispatcherV2() =
default;
664
665
666
667void QAbstractEventDispatcherV2::registerTimer(
int timerId, qint64 interval,
668 Qt::TimerType timerType, QObject *object)
670 auto self =
static_cast<QAbstractEventDispatcherV2 *>(
this);
671 self->registerTimer(Qt::TimerId(timerId), interval * 1ms, timerType, object);
675
676
677
678bool QAbstractEventDispatcherV2::unregisterTimer(
int timerId)
680 auto self =
static_cast<QAbstractEventDispatcherV2 *>(
this);
681 return self->unregisterTimer(Qt::TimerId(timerId));
685
686
687
688auto QAbstractEventDispatcherV2::registeredTimers(QObject *object)
const -> QList<TimerInfo>
690 auto self =
static_cast<
const QAbstractEventDispatcherV2 *>(
this);
691 QList<TimerInfoV2> timers = self->timersForObject(object);
692 QList<TimerInfo> result;
693 result.reserve(timers.size());
694 for (
const TimerInfoV2 &t : timers)
695 result.emplaceBack(qToUnderlying(t.timerId), fromDuration<
int>(t.interval), t.timerType);
700
701
702
703int QAbstractEventDispatcherV2::remainingTime(
int timerId)
705 auto self =
static_cast<QAbstractEventDispatcherV2 *>(
this);
706 return fromDuration<
int>(self->remainingTime(Qt::TimerId(timerId)));
710
711
712
713bool QAbstractEventDispatcherV2::processEventsWithDeadline(QEventLoop::ProcessEventsFlags flags, QDeadlineTimer deadline)
716 return processEvents(flags);
722#include "moc_qabstracteventdispatcher.cpp"
Combined button and popup list for selecting options.
QFreeList< void, QtTimerIdFreeListConstants > QtTimerIdFreeList
static const int Sizes[BlockCount]