mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-20 12:35:43 +08:00
refs #476 Facility to ask worker to abandon its task and finish early.
This commit is contained in:
@@ -51,6 +51,23 @@ namespace BlackMisc
|
|||||||
promise.get_future().wait();
|
promise.get_future().wait();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void CWorkerBase::abandon() Q_DECL_NOEXCEPT
|
||||||
|
{
|
||||||
|
thread()->requestInterruption();
|
||||||
|
quit();
|
||||||
|
}
|
||||||
|
|
||||||
|
void CWorkerBase::abandonAndWait() Q_DECL_NOEXCEPT
|
||||||
|
{
|
||||||
|
thread()->requestInterruption();
|
||||||
|
quitAndWait();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool CWorkerBase::isAbandoned() const
|
||||||
|
{
|
||||||
|
return thread()->isInterruptionRequested();
|
||||||
|
}
|
||||||
|
|
||||||
void CContinuousWorker::start(QThread::Priority priority)
|
void CContinuousWorker::start(QThread::Priority priority)
|
||||||
{
|
{
|
||||||
if (m_name.isEmpty()) { m_name = metaObject()->className(); }
|
if (m_name.isEmpty()) { m_name = metaObject()->className(); }
|
||||||
|
|||||||
@@ -155,14 +155,25 @@ namespace BlackMisc
|
|||||||
}
|
}
|
||||||
|
|
||||||
//! Blocks until the task is finished.
|
//! Blocks until the task is finished.
|
||||||
//! \threadsafe
|
//! \threadsafe Will deadlock if called by the worker thread.
|
||||||
void waitForFinished() Q_DECL_NOEXCEPT;
|
void waitForFinished() Q_DECL_NOEXCEPT;
|
||||||
|
|
||||||
|
//! Notify the task that its result is no longer needed, so it can finish early.
|
||||||
|
//! \threadsafe
|
||||||
|
void abandon() Q_DECL_NOEXCEPT;
|
||||||
|
|
||||||
|
//! Convenience to call abandon() followed by waitForFinished().
|
||||||
|
void abandonAndWait() Q_DECL_NOEXCEPT;
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
//! Emitted when the task is finished.
|
//! Emitted when the task is finished.
|
||||||
void finished();
|
void finished();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
//! For the task to check whether it can finish early.
|
||||||
|
//! \threadsafe
|
||||||
|
bool isAbandoned() const;
|
||||||
|
|
||||||
//! Mark the task as finished.
|
//! Mark the task as finished.
|
||||||
void setFinished()
|
void setFinished()
|
||||||
{
|
{
|
||||||
@@ -186,6 +197,9 @@ namespace BlackMisc
|
|||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
virtual void quit() Q_DECL_NOEXCEPT {}
|
||||||
|
virtual void quitAndWait() Q_DECL_NOEXCEPT { waitForFinished(); }
|
||||||
|
|
||||||
bool m_finished = false;
|
bool m_finished = false;
|
||||||
mutable QMutex m_finishedMutex { QMutex::Recursive };
|
mutable QMutex m_finishedMutex { QMutex::Recursive };
|
||||||
};
|
};
|
||||||
@@ -276,11 +290,11 @@ namespace BlackMisc
|
|||||||
//! Stops the thread the next time around its event loop.
|
//! Stops the thread the next time around its event loop.
|
||||||
//! The thread and the worker will then be deleted.
|
//! The thread and the worker will then be deleted.
|
||||||
//! \threadsafe
|
//! \threadsafe
|
||||||
void quit() Q_DECL_NOEXCEPT;
|
virtual void quit() Q_DECL_NOEXCEPT final override;
|
||||||
|
|
||||||
//! Calls quit() and blocks until the thread is finished.
|
//! Calls quit() and blocks until the thread is finished.
|
||||||
//! \threadsafe Will deadlock if called by the worker thread.
|
//! \threadsafe Will deadlock if called by the worker thread.
|
||||||
void quitAndWait() Q_DECL_NOEXCEPT;
|
virtual void quitAndWait() Q_DECL_NOEXCEPT final override;
|
||||||
|
|
||||||
protected slots:
|
protected slots:
|
||||||
//! Called when the thread is started.
|
//! Called when the thread is started.
|
||||||
@@ -294,6 +308,8 @@ namespace BlackMisc
|
|||||||
void ps_finish();
|
void ps_finish();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
using CWorkerBase::setFinished;
|
||||||
|
|
||||||
QObject *m_owner = nullptr;
|
QObject *m_owner = nullptr;
|
||||||
QString m_name;
|
QString m_name;
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user