21 QSet<CWorkerBase *> CWorkerBase::s_allWorkers;
26 m_handle = GetCurrentThread();
39 auto handle = m_handle.load();
42 const auto status = WaitForSingleObject(handle, 0);
48 case WAIT_FAILED: qWarning() <<
"Thread" << name <<
"unspecified error";
break;
49 case WAIT_OBJECT_0: qWarning() <<
"Thread" << name <<
"unsafely terminated by program shutdown";
break;
50 case WAIT_TIMEOUT:
break;
58 const unsigned long timeoutMs = 5 * 1000;
59 const bool ok =
wait(timeoutMs);
61 const QString as = QStringLiteral(
"Wait timeout after %1ms for '%2'").
arg(timeoutMs).
arg(name);
69 const std::function<
QVariant()> &task)
71 auto *worker =
new CWorker(task);
72 emit worker->aboutToStart();
84 worker->setObjectName(name);
86 worker->moveToThread(
thread);
88 Q_ASSERT_X(s, Q_FUNC_INFO,
"cannot invoke");
94 void CWorker::ps_runTask()
101 Q_ASSERT_X(workerThread->
thread()->
isRunning(), Q_FUNC_INFO,
"Owner thread's event loop already ended");
116 workerThread->
quit();
117 const bool ok = workerThread->
wait(5000);
118 const QString as = QStringLiteral(
"Worker thread '%2' refuses to stop after worker finished")
139 std::promise<void> promise;
140 then([&] { promise.set_value(); });
141 promise.get_future().wait();
170 Q_ASSERT_X(!name.
isEmpty(), Q_FUNC_INFO,
"Empty name");
192 m_owner->objectName().
isEmpty() ? m_owner->metaObject()->className() : m_owner->objectName();
193 thread->setObjectName(ownerName +
": " + m_name);
205 this->setEnabled(
false);
220 this->setEnabled(
false);
244 const QString msg = QStringLiteral(
"Waiting for quitAndWait of '%1' for %2ms").
arg(name).
arg(waitTime);
252 void CContinuousWorker::finish()
257 Q_ASSERT_X(m_owner->
thread()->
isRunning(), Q_FUNC_INFO,
"Owner thread's event loop already ended");
271 workerThread->
quit();
272 const bool ok = workerThread->
wait(5000);
273 const QString as = QStringLiteral(
"Worker thread '%2' refuses to stop after worker finished")
virtual void beforeQuit() noexcept
Called before quit is called.
const QObject * owner() const
Owner of the worker.
virtual void cleanup()
Called when the thread is finished.
void quit() noexcept final
Stops the thread the next time around its event loop. The thread and the worker will then be deleted.
virtual void initialize()
Called when the thread is started.
virtual unsigned long waitTimeoutMs() const
Wait time for quitAndWait, 0 means not waiting.
void quitAndWait() noexcept final
Calls quit() and blocks until the thread is finished.
void start(QThread::Priority priority=QThread::InheritPriority)
Starts a thread and moves the worker into it.
CContinuousWorker(QObject *owner, const QString &name)
Constructor.
const QString & getName()
Name of the worker.
static const QString & worker()
Background task.
Class for emitting a log message.
Derived & info(const char16_t(&format)[N])
Set the severity to info, providing a format string.
Just a subclass of QThread whose destructor waits for the thread to finish.
~CRegularThread()
Destructor.
static bool isInThisThread(const QObject *toBeTested)
Is the current thread the object's thread?
CWorkerBase()
Constructor.
void aboutToStart()
Emitted when the task is about to start.
void then(T *context, F functor)
Connects to a functor or method which will be called when the task is finished.
void setStarted()
Mark the task as started.
void abandon() noexcept
Notify the task that its result is no longer needed, so it can finish early.
static const QStringList & getLogCategories()
Log categories.
void abandonAndWait() noexcept
Convenience to call abandon() followed by waitForFinished().
bool isAbandoned() const
For the task to check whether it can finish early.
bool hasStarted() const
True if the worker has started.
~CWorkerBase()
Destructor.
void setFinished()
Mark the task as finished.
void waitForFinished() noexcept
Blocks until the task is finished.
Class for doing some arbitrary parcel of work in its own thread.
Free functions in swift::misc.
qint64 currentMSecsSinceEpoch()
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
bool moveToThread(QThread *targetThread)
void setObjectName(QAnyStringView name)
QThread * thread() const const
QString arg(Args &&... args) const const
bool isEmpty() const const
QByteArray toLatin1() const const
QThread * currentThread()
bool isInterruptionRequested() const const
bool isRunning() const const
void requestInterruption()
void start(QThread::Priority priority)
bool wait(QDeadlineTimer deadline)
#define SWIFT_AUDIT_X(COND, WHERE, WHAT)
A weaker kind of verify.
#define SWIFT_VERIFY_X(COND, WHERE, WHAT)
A weaker kind of assert.