15#include "private/qcoreapplication_p.h"
27 int priority = ev.priority;
49 : _ref(initialRefCount), loopLevel(0), scopeLevel(0),
51 quitNow(
false), canWait(true), isAdopted(
false), requiresCoreApplication(true)
88 --pe.
receiver->d_func()->postedEvents;
89 pe.
event->m_posted =
false;
130 d_func()->running =
true;
133 d_func()->m_statusOrPendingObjects.setStatusAndClearList(
148 qFatal(
"QAdoptedThread::run(): Internal error, this implementation should never be called.");
153 : threadData(threadData)
157 <<
"scope level to" << threadData->
scopeLevel;
164 <<
"scope level to" << threadData->
scopeLevel;
174 isInFinish(
false), interruptionRequested(
false),
175 exited(
false), returnCode(-1),
176 stackSize(0), priority(
QThread::InheritPriority),
data(
d)
183 stackSize = 128 * 1024;
184#elif defined(Q_OS_RTEMS)
185 Q_CONSTINIT
static bool envStackSizeOk =
false;
188 stackSize = envStackSize;
191#if defined (Q_OS_WIN)
195 terminationEnabled =
true;
196 terminatePending =
false;
208 delete m_statusOrPendingObjects.list();
426 return data->thread.loadAcquire();
458 d->data->thread.storeRelaxed(
this);
469 d->data->thread.storeRelaxed(
this);
500 if (
d->running && !
d->finished && !
d->data->isAdopted)
501 qFatal(
"QThread: Destroyed while thread is still running");
503 d->data->thread.storeRelease(
nullptr);
517 return d->finished ||
d->isInFinish;
530 return d->running && !
d->isInFinish;
558 "cannot change stack size while the thread is running");
584 if (
auto pendingObjects =
list()) {
585 for (
auto obj: *pendingObjects)
587 delete pendingObjects;
590 data.store(encodeBindingStatus(status), std::memory_order_release);
612 d->m_statusOrPendingObjects.setStatusAndClearList(status);
613 d->data->quitNow =
false;
616 return d->returnCode;
621 int returnCode = eventLoop.
exec();
641 List *objectList =
list();
643 objectList =
new List();
644 objectList->reserve(8);
645 data.store(encodeList(objectList), std::memory_order_relaxed);
647 objectList->push_back(
object);
657 List *objectList =
list();
660 auto it = std::remove(objectList->begin(), objectList->end(),
object);
661 objectList->erase(
it, objectList->
end());
666 if (
auto status = m_statusOrPendingObjects.bindingStatus())
669 return m_statusOrPendingObjects.addObjectUnlessAlreadyStatus(
obj);
674 if (m_statusOrPendingObjects.bindingStatus())
677 m_statusOrPendingObjects.removeObject(
obj);
708 d->returnCode = returnCode;
709 d->data->quitNow =
true;
710 for (
int i = 0;
i <
d->data->eventLoops.size(); ++
i) {
712 eventLoop->
exit(returnCode);
767 qWarning(
"QThread::setPriority: Argument cannot be InheritPriority");
773 qWarning(
"QThread::setPriority: Cannot set priority, thread is not running");
776 d->setPriority(priority);
793 return Priority(
d->priority & 0xffff);
939 return d->data->eventLoops.size();
957 return data->threadId.loadRelaxed();
978 d->data->thread.storeRelaxed(
this);
1016 d->data->quitNow =
true;
1017 for (
int i = 0;
i <
d->data->eventLoops.size(); ++
i) {
1019 eventLoop->
exit(returnCode);
1034Qt::HANDLE QThread::currentThreadIdImpl() noexcept
1089 if (!
data && createIfNecessary) {
1094 data->isAdopted =
true;
1117 d->data->thread.storeRelaxed(
this);
1126 data->thread.storeRelease(
nullptr);
1151 return d->data->eventDispatcher.loadRelaxed();
1169 if (
d->data->hasEventDispatcher()) {
1170 qWarning(
"QThread::setEventDispatcher: An event dispatcher has already been created for this thread");
1176 qWarning(
"QThread::setEventDispatcher: Could not move event dispatcher to target thread");
1188#if QT_CONFIG(thread)
1220 qWarning(
"QThread::requestInterruption has no effect on the main thread");
1224 if (!
d->running ||
d->finished ||
d->isInFinish)
1226 d->interruptionRequested.store(
true, std::memory_order_relaxed);
1259 if (!
d->interruptionRequested.load(std::memory_order_relaxed))
1263 return d->running && !
d->finished && !
d->isInFinish;
1288class QThreadCreateThread :
public QThread
1291 explicit QThreadCreateThread(std::future<void> &&
future)
1296 ~QThreadCreateThread()
1298 requestInterruption();
1309 std::future<void> m_future;
1312QThread *QThread::createThreadImpl(std::future<void> &&
future)
1314 return new QThreadCreateThread(std::move(
future));
1325QDaemonThread::QDaemonThread(
QObject *parent)
1335QDaemonThread::~QDaemonThread()
1343#include "moc_qthread.cpp"
QAdoptedThread(QThreadData *data=nullptr)
T loadRelaxed() const noexcept
Type loadAcquire() const noexcept
void storeRelease(Type newValue) noexcept
static QBasicAtomicPointer< void > theMainThreadId
static QBasicAtomicPointer< QThread > theMainThread
int exec(ProcessEventsFlags flags=AllEvents)
Enters the main event loop and waits until exit() is called.
void exit(int returnCode=0)
Tells the event loop to exit with a return code.
qsizetype size() const noexcept
bool isEmpty() const noexcept
const QPostEvent & constLast() const noexcept
iterator insert(qsizetype i, parameter_type t)
const_reference at(qsizetype i) const noexcept
void append(parameter_type t)
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
virtual bool event(QEvent *event)
This virtual function receives events to an object and should return true if the event e was recogniz...
QThread * thread() const
Returns the thread in which the object lives.
bool moveToThread(QThread *thread QT6_DECL_NEW_OVERLOAD_TAIL)
Changes the thread affinity for this object and its children and returns true on success.
void addEvent(const QPostEvent &ev)
qsizetype insertionOffset
QScopedScopeLevelCounter(QThreadData *threadData)
~QScopedScopeLevelCounter()
static Q_AUTOTEST_EXPORT QThreadData * current(bool createIfNecessary=true)
QAtomicPointer< QAbstractEventDispatcher > eventDispatcher
QThreadData(int initialRefCount=1)
static void clearCurrentThreadData()
QAtomicPointer< void > threadId
QPostEventList postEventList
QAtomicPointer< QThread > thread
QAbstractEventDispatcher * createEventDispatcher()
static QAbstractEventDispatcher * createEventDispatcher(QThreadData *data)
QThreadPrivate(QThreadData *d=nullptr)
void removeObjectWithPendingBindingStatusChange(QObject *)
QBindingStatus * addObjectWithPendingBindingStatusChange(QObject *)
static bool isMainThread()
bool isInterruptionRequested() const
void start(Priority=InheritPriority)
QAbstractEventDispatcher * eventDispatcher() const
bool isCurrentThread() const
static int idealThreadCount() noexcept
bool event(QEvent *event) override
This virtual function receives events to an object and should return true if the event e was recogniz...
static Qt::HANDLE currentThreadId() noexcept Q_DECL_PURE_FUNCTION
static QThread * currentThread()
static void setTerminationEnabled(bool enabled=true)
static void yieldCurrentThread()
bool wait(QDeadlineTimer deadline=QDeadlineTimer(QDeadlineTimer::Forever))
QThread(QObject *parent=nullptr)
Priority priority() const
void finished(QPrivateSignal)
void setPriority(Priority priority)
void requestInterruption()
void setStackSize(uint stackSize)
void setEventDispatcher(QAbstractEventDispatcher *eventDispatcher)
void started(QPrivateSignal)
List * list() const noexcept
void removeObject(QObject *object)
void setStatusAndClearList(QBindingStatus *status) noexcept
QBindingStatus * addObjectUnlessAlreadyStatus(QObject *object)
QBindingStatus * bindingStatus() const noexcept
std::vector< QObject * > List
QSet< QString >::iterator it
Combined button and popup list for selecting options.
QTCONCURRENT_RUN_NODISCARD auto run(QThreadPool *pool, Function &&f, Args &&...args)
Q_AUTOTEST_EXPORT QBindingStatus * getBindingStatus(QBindingStatusAccessToken)
DBusConnection const char DBusError DBusBusType DBusError return DBusConnection DBusHandleMessageFunction void DBusFreeFunction return DBusConnection return DBusConnection return const char DBusError return DBusConnection DBusMessage dbus_uint32_t return DBusConnection dbus_bool_t DBusConnection DBusAddWatchFunction DBusRemoveWatchFunction DBusWatchToggledFunction void DBusFreeFunction return DBusConnection DBusDispatchStatusFunction void DBusFreeFunction DBusTimeout return DBusTimeout return DBusWatch return DBusWatch unsigned int return DBusError const DBusError return const DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessage return DBusMessageIter int const void return DBusMessageIter DBusMessageIter return DBusMessageIter void DBusMessageIter void int return DBusMessage DBusMessageIter return DBusMessageIter return DBusMessageIter DBusMessageIter const char const char const char const char return DBusMessage return DBusMessage const char return DBusMessage dbus_bool_t return DBusMessage dbus_uint32_t return DBusMessage void
static QDBusError::ErrorType get(const char *name)
static Q_CONSTINIT QBasicAtomicInt running
#define qCDebug(category,...)
GLuint64 GLenum void * handle
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
#define Q_ASSERT_X(cond, x, msg)
Q_CORE_EXPORT int qEnvironmentVariableIntValue(const char *varName, bool *ok=nullptr) noexcept
QFuture< void > future
[5]
QDeadlineTimer deadline(30s)