mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-03-30 11:55:35 +08:00
refs #348 Added thread safety guarantees in CLogSubscriber.
This commit is contained in:
committed by
Roland Winklmeier
parent
671bb294ae
commit
cbbf9c256a
@@ -9,6 +9,7 @@
|
||||
|
||||
#include "loghandler.h"
|
||||
#include "algorithm.h"
|
||||
#include "worker.h"
|
||||
#include <QCoreApplication>
|
||||
#include <QMetaMethod>
|
||||
|
||||
@@ -139,7 +140,13 @@ namespace BlackMisc
|
||||
|
||||
void CLogSubscriber::changeSubscription(const CLogPattern &pattern)
|
||||
{
|
||||
Q_ASSERT(CLogHandler::instance()->thread() == QThread::currentThread());
|
||||
if (CLogHandler::instance()->thread() != QThread::currentThread())
|
||||
{
|
||||
Q_ASSERT(thread() == QThread::currentThread());
|
||||
singleShot(0, CLogHandler::instance()->thread(), this, [ = ]() { changeSubscription(pattern); });
|
||||
return;
|
||||
}
|
||||
|
||||
unsubscribe();
|
||||
m_handler = CLogHandler::instance()->handlerForPattern(pattern);
|
||||
|
||||
@@ -147,12 +154,18 @@ namespace BlackMisc
|
||||
{
|
||||
m_handler->enableConsoleOutput(m_enableFallThrough);
|
||||
}
|
||||
connect(m_handler.data(), &CLogPatternHandler::messageLogged, this, &CLogSubscriber::ps_logMessage);
|
||||
connect(m_handler.data(), &CLogPatternHandler::messageLogged, this, &CLogSubscriber::ps_logMessage, Qt::DirectConnection);
|
||||
}
|
||||
|
||||
void CLogSubscriber::unsubscribe()
|
||||
{
|
||||
Q_ASSERT(CLogHandler::instance()->thread() == QThread::currentThread());
|
||||
if (CLogHandler::instance()->thread() != QThread::currentThread())
|
||||
{
|
||||
Q_ASSERT(thread() == QThread::currentThread());
|
||||
singleShot(0, CLogHandler::instance()->thread(), this, [ = ]() { unsubscribe(); });
|
||||
return;
|
||||
}
|
||||
|
||||
if (m_handler)
|
||||
{
|
||||
m_handler->disconnect(this);
|
||||
@@ -161,7 +174,13 @@ namespace BlackMisc
|
||||
|
||||
void CLogSubscriber::inheritConsoleOutput()
|
||||
{
|
||||
Q_ASSERT(CLogHandler::instance()->thread() == QThread::currentThread());
|
||||
if (CLogHandler::instance()->thread() != QThread::currentThread())
|
||||
{
|
||||
Q_ASSERT(thread() == QThread::currentThread());
|
||||
singleShot(0, CLogHandler::instance()->thread(), this, [ = ]() { inheritConsoleOutput(); });
|
||||
return;
|
||||
}
|
||||
|
||||
m_inheritFallThrough = true;
|
||||
if (m_handler)
|
||||
{
|
||||
@@ -171,7 +190,13 @@ namespace BlackMisc
|
||||
|
||||
void CLogSubscriber::enableConsoleOutput(bool enable)
|
||||
{
|
||||
Q_ASSERT(CLogHandler::instance()->thread() == QThread::currentThread());
|
||||
if (CLogHandler::instance()->thread() != QThread::currentThread())
|
||||
{
|
||||
Q_ASSERT(thread() == QThread::currentThread());
|
||||
singleShot(0, CLogHandler::instance()->thread(), this, [ = ]() { enableConsoleOutput(enable); });
|
||||
return;
|
||||
}
|
||||
|
||||
m_inheritFallThrough = false;
|
||||
m_enableFallThrough = enable;
|
||||
if (m_handler)
|
||||
|
||||
@@ -175,30 +175,38 @@ namespace BlackMisc
|
||||
/*!
|
||||
* A helper class for subscribing to log messages matching a particular pattern, with the ability to
|
||||
* change the pattern at runtime.
|
||||
*
|
||||
* Also provides a thread-safe API for interacting with the CLogHandler.
|
||||
*/
|
||||
class CLogSubscriber : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
//! Default constructor, for when you're not interested in messages and just want to control the console output.
|
||||
CLogSubscriber(QObject *parent = nullptr) : QObject(parent) {}
|
||||
|
||||
//! Construct a subscriber which forwards messages to the given slot of parent.
|
||||
template <typename T, typename F>
|
||||
CLogSubscriber(T *parent, F slot) : QObject(parent)
|
||||
{
|
||||
Q_ASSERT(CLogHandler::instance()->thread() == QThread::currentThread());
|
||||
QObject::connect(this, &CLogSubscriber::ps_messageLogged, parent, slot);
|
||||
}
|
||||
|
||||
//! Change the pattern which you want to subscribe to.
|
||||
//! \threadsafe If not called from the main thread, it will run asynchronously.
|
||||
void changeSubscription(const CLogPattern &pattern);
|
||||
|
||||
//! Unsubscribe from all messages.
|
||||
//! \threadsafe If not called from the main thread, it will run asynchronously.
|
||||
void unsubscribe();
|
||||
|
||||
//! \copydoc CLogPatternHandler::enableConsoleOutput
|
||||
//! \threadsafe If not called from the main thread, it will run asynchronously.
|
||||
void enableConsoleOutput(bool enable);
|
||||
|
||||
//! \copydoc CLogPatternHandler::inheritConsoleOutput
|
||||
//! \threadsafe If not called from the main thread, it will run asynchronously.
|
||||
void inheritConsoleOutput();
|
||||
|
||||
signals:
|
||||
|
||||
@@ -15,18 +15,36 @@
|
||||
#include <QThread>
|
||||
#include <QMutex>
|
||||
#include <QTimer>
|
||||
#include <QSharedPointer>
|
||||
#include <QWeakPointer>
|
||||
#include <functional>
|
||||
#include <atomic>
|
||||
|
||||
namespace BlackMisc
|
||||
{
|
||||
|
||||
//! \private Class for synchronizing singleShot() task with its owner.
|
||||
class 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.
|
||||
*
|
||||
* Useful when a worker thread wants to push small sub-tasks back to the thread which spawned it.
|
||||
* \see QTimer::singleShot()
|
||||
*
|
||||
* 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>
|
||||
void singleShot(int msec, QThread *target, F task)
|
||||
{
|
||||
@@ -40,6 +58,18 @@ namespace BlackMisc
|
||||
});
|
||||
QMetaObject::invokeMethod(timer, "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(); }
|
||||
});
|
||||
}
|
||||
//! @}
|
||||
|
||||
/*!
|
||||
* Just a subclass of QThread whose destructor waits for the thread to finish.
|
||||
|
||||
Reference in New Issue
Block a user