refs #783 Remove the flawed 4-arg version of BlackMisc::singleShot and change signature of 3-arg version.

This commit is contained in:
Mathew Sutcliffe
2016-10-21 00:24:19 +01:00
committed by Klaus Basan
parent 64e2414b56
commit b62d1c303c
2 changed files with 7 additions and 34 deletions

View File

@@ -213,7 +213,7 @@ namespace BlackMisc
if (CLogHandler::instance()->thread() != QThread::currentThread()) if (CLogHandler::instance()->thread() != QThread::currentThread())
{ {
Q_ASSERT(thread() == QThread::currentThread()); Q_ASSERT(thread() == QThread::currentThread());
singleShot(0, CLogHandler::instance()->thread(), this, [ = ]() { changeSubscription(pattern); }); singleShot(0, CLogHandler::instance(), [pattern, self = QPointer<CLogSubscriber>(this)]() { if (self) { self->changeSubscription(pattern); } });
return; return;
} }
@@ -232,7 +232,7 @@ namespace BlackMisc
if (CLogHandler::instance()->thread() != QThread::currentThread()) if (CLogHandler::instance()->thread() != QThread::currentThread())
{ {
Q_ASSERT(thread() == QThread::currentThread()); Q_ASSERT(thread() == QThread::currentThread());
singleShot(0, CLogHandler::instance()->thread(), this, [ = ]() { unsubscribe(); }); singleShot(0, CLogHandler::instance(), [self = QPointer<CLogSubscriber>(this)]() { if (self) { self->unsubscribe(); } });
return; return;
} }
@@ -248,7 +248,7 @@ namespace BlackMisc
if (CLogHandler::instance()->thread() != QThread::currentThread()) if (CLogHandler::instance()->thread() != QThread::currentThread())
{ {
Q_ASSERT(thread() == QThread::currentThread()); Q_ASSERT(thread() == QThread::currentThread());
singleShot(0, CLogHandler::instance()->thread(), this, [ = ]() { inheritConsoleOutput(); }); singleShot(0, CLogHandler::instance(), [self = QPointer<CLogSubscriber>(this)]() { if (self) { self->inheritConsoleOutput(); } });
return; return;
} }
@@ -264,7 +264,7 @@ namespace BlackMisc
if (CLogHandler::instance()->thread() != QThread::currentThread()) if (CLogHandler::instance()->thread() != QThread::currentThread())
{ {
Q_ASSERT(thread() == QThread::currentThread()); Q_ASSERT(thread() == QThread::currentThread());
singleShot(0, CLogHandler::instance()->thread(), this, [ = ]() { enableConsoleOutput(enable); }); singleShot(0, CLogHandler::instance(), [enable, self = QPointer<CLogSubscriber>(this)]() { if (self) { self->enableConsoleOutput(enable); } });
return; return;
} }

View File

@@ -37,30 +37,14 @@
namespace BlackMisc namespace BlackMisc
{ {
//! \private Class for synchronizing singleShot() task with its owner.
class BLACKMISC_EXPORT CSingleShotController : public QObject
{
Q_OBJECT
public:
CSingleShotController(QObject *parent) : QObject(parent), m_strongRef(QSharedPointer<int>::create(0)) {}
~CSingleShotController() { auto wr = weakRef(); m_strongRef.clear(); waitForNull(wr); }
QWeakPointer<int> weakRef() const { return m_strongRef.toWeakRef(); }
private:
static void waitForNull(QWeakPointer<int> wp) { while (wp) { QThread::msleep(10); } }
QSharedPointer<int> m_strongRef; // pointee type doesn't matter, we only care about the reference count
};
/*! /*!
* Starts a single-shot timer which will run in an existing thread and call a task when it times out. * Starts a single-shot timer which will call a task in the thread of the given object when it times out.
* *
* Useful when a worker thread wants to push small sub-tasks back to the thread which spawned it. * Differs from QTimer::singleShot in that this implementation interacts better with QObject::moveToThread.
*
* If an owner pointer is specified, then the task may be cancelled if the owner is deleted, but the
* owner will not be deleted while the task is running (its destructor will wait for the task to end).
*/ */
//! @{ //! @{
template <typename F> template <typename F>
void singleShot(int msec, QThread *target, F &&task) void singleShot(int msec, QObject *target, F &&task)
{ {
QSharedPointer<QTimer> timer(new QTimer, [](QObject *o) { QMetaObject::invokeMethod(o, "deleteLater"); }); QSharedPointer<QTimer> timer(new QTimer, [](QObject *o) { QMetaObject::invokeMethod(o, "deleteLater"); });
timer->setSingleShot(true); timer->setSingleShot(true);
@@ -73,17 +57,6 @@ namespace BlackMisc
}); });
QMetaObject::invokeMethod(timer.data(), "start", Q_ARG(int, msec)); QMetaObject::invokeMethod(timer.data(), "start", Q_ARG(int, msec));
} }
template <typename F>
void singleShot(int msec, QThread *target, QObject *owner, F task)
{
Q_ASSERT(QThread::currentThread() == owner->thread());
auto weakRef = (new CSingleShotController(owner))->weakRef();
singleShot(msec, target, [ = ]()
{
auto strongRef = weakRef.toStrongRef();
if (strongRef) { task(); }
});
}
//! @} //! @}
/*! /*!