Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qcoreevent.cpp
Go to the documentation of this file.
1// Copyright (C) 2022 The Qt Company Ltd.
2// Copyright (C) 2016 Intel Corporation.
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
4
5#include "qcoreevent.h"
6#include "qcoreevent_p.h"
7#include "qcoreapplication.h"
9
10#include "qbasicatomic.h"
11
12#include <qtcore_tracepoints_p.h>
13
14#include <limits>
15
17
18Q_TRACE_POINT(qtcore, QEvent_ctor, QEvent *event, QEvent::Type type);
19Q_TRACE_POINT(qtcore, QEvent_dtor, QEvent *event, QEvent::Type type);
20
276 : t(type), m_reserved(0),
277 m_inputEvent(false), m_pointerEvent(false), m_singlePointEvent(false)
278{
279 Q_TRACE(QEvent_ctor, this, type);
280}
281
333
339{ return new QEvent(*this); }
340
422namespace {
423template <size_t N>
424struct QBasicAtomicBitField {
425 enum {
426 BitsPerInt = std::numeric_limits<uint>::digits,
427 NumInts = (N + BitsPerInt - 1) / BitsPerInt,
428 NumBits = N
429 };
430
431 // This atomic int points to the next (possibly) free ID saving
432 // the otherwise necessary scan through 'data':
433 QBasicAtomicInteger<uint> next;
434 QBasicAtomicInteger<uint> data[NumInts];
435
436 constexpr QBasicAtomicBitField() = default;
437
438 bool allocateSpecific(int which) noexcept
439 {
440 QBasicAtomicInteger<uint> &entry = data[which / BitsPerInt];
441 const uint old = entry.loadRelaxed();
442 const uint bit = 1U << (which % BitsPerInt);
443 if (old & bit)
444 return false; // already taken
445 return (entry.fetchAndOrRelaxed(bit) & bit) == 0;
446
447 // don't update 'next' here - it's unlikely that it will need
448 // to be updated, in the general case, and having 'next'
449 // trailing a bit is not a problem, as it is just a starting
450 // hint for allocateNext(), which, when wrong, will just
451 // result in a few more rounds through the allocateNext()
452 // loop.
453 }
454
455 int allocateNext() noexcept
456 {
457 // Unroll loop to iterate over ints, then bits? Would save
458 // potentially a lot of cmpxchgs, because we can scan the
459 // whole int before having to load it again.
460
461 // Then again, this should never execute many iterations, so
462 // leave like this for now:
463 for (uint i = next.loadRelaxed(); i < NumBits; ++i) {
464 if (allocateSpecific(i)) {
465 // remember next (possibly) free id:
466 const uint oldNext = next.loadRelaxed();
467 next.testAndSetRelaxed(oldNext, qMax(i + 1, oldNext));
468 return i;
469 }
470 }
471 return -1;
472 }
473};
474
475} // unnamed namespace
476
477typedef QBasicAtomicBitField<QEvent::MaxUser - QEvent::User + 1> UserEventTypeRegistry;
478
480
481static inline int registerEventTypeZeroBased(int id) noexcept
482{
483 // if the type hint hasn't been registered yet, take it:
484 if (id < UserEventTypeRegistry::NumBits && id >= 0 && userEventTypeRegistry.allocateSpecific(id))
485 return id;
486
487 // otherwise, ignore hint:
488 return userEventTypeRegistry.allocateNext();
489}
490
505{
507 return result < 0 ? -1 : QEvent::MaxUser - result ;
508}
509
536 : QEvent(Timer), id(timerId)
537{}
538
540
541
580
582
583
632
634
635
647QDeferredDeleteEvent::QDeferredDeleteEvent(int loopLevel, int scopeLevel)
648 : QEvent(QEvent::DeferredDelete), m_loopLevel(loopLevel), m_scopeLevel(scopeLevel)
649{ }
650
652
654
655#include "moc_qcoreevent.cpp"
\inmodule QtCore
Definition qbytearray.h:57
\inmodule QtCore
Definition qcoreevent.h:379
static void removePostedEvent(QEvent *)
Removes event from the queue of posted events, and emits a warning message if appropriate.
static QCoreApplication * instance() noexcept
Returns a pointer to the application's QCoreApplication (or QGuiApplication/QApplication) instance.
\inmodule QtCore
Definition qcoreevent.h:45
static int registerEventType(int hint=-1) noexcept
Type
This enum type defines the valid event types in Qt.
Definition qcoreevent.h:51
QEvent(Type type)
Constructs an event object of type type.
virtual ~QEvent()
Destroys the event.
virtual QEvent * clone() const
Creates and returns an identical copy of this event.
\inmodule QtCore
Definition qobject.h:103
\inmodule QtCore
Definition qcoreevent.h:366
QTimerEvent(int timerId)
Constructs a timer event object with the timer identifier set to timerId.
short next
Definition keywords.cpp:445
Combined button and popup list for selecting options.
static int registerEventTypeZeroBased(int id) noexcept
QBasicAtomicBitField< QEvent::MaxUser - QEvent::User+1 > UserEventTypeRegistry
static Q_CONSTINIT UserEventTypeRegistry userEventTypeRegistry
#define Q_IMPL_EVENT_COMMON(Class)
Definition qcoreevent.h:31
constexpr const T & qMax(const T &a, const T &b)
Definition qminmax.h:42
GLenum GLuint id
[7]
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLenum type
GLuint name
GLfloat n
struct _cl_event * event
const GLubyte * c
GLuint entry
GLdouble GLdouble t
Definition qopenglext.h:243
GLuint64EXT * result
[6]
static QT_BEGIN_NAMESPACE QVariant hint(QPlatformIntegration::StyleHint h)
#define Q_TRACE(x,...)
Definition qtrace_p.h:144
#define Q_TRACE_POINT(provider, tracepoint,...)
Definition qtrace_p.h:232
unsigned int uint
Definition qtypes.h:34
QLayoutItem * child
[0]
Definition moc.h:23