optimize QFileSystemWatcher

Signed-off-by: Ivailo Monev <xakepa10@laimg.moc>
This commit is contained in:
Ivailo Monev 2020-01-12 17:03:33 +00:00
parent 991ece5a50
commit f4b2e2ef65
2 changed files with 34 additions and 54 deletions

View file

@ -224,20 +224,20 @@ QFileSystemWatcherEngine *QFileSystemWatcherPrivate::createNativeEngine()
} }
QFileSystemWatcherPrivate::QFileSystemWatcherPrivate() QFileSystemWatcherPrivate::QFileSystemWatcherPrivate()
: native(Q_NULLPTR), poller(Q_NULLPTR), forced(Q_NULLPTR) : watcher(Q_NULLPTR)
{ {
} }
void QFileSystemWatcherPrivate::init() void QFileSystemWatcherPrivate::init()
{ {
Q_Q(QFileSystemWatcher); Q_Q(QFileSystemWatcher);
native = createNativeEngine(); watcher = createNativeEngine();
if (native) { if (watcher) {
QObject::connect(native, QObject::connect(watcher,
SIGNAL(fileChanged(QString,bool)), SIGNAL(fileChanged(QString,bool)),
q, q,
SLOT(_q_fileChanged(QString,bool))); SLOT(_q_fileChanged(QString,bool)));
QObject::connect(native, QObject::connect(watcher,
SIGNAL(directoryChanged(QString,bool)), SIGNAL(directoryChanged(QString,bool)),
q, q,
SLOT(_q_directoryChanged(QString,bool))); SLOT(_q_directoryChanged(QString,bool)));
@ -246,25 +246,25 @@ void QFileSystemWatcherPrivate::init()
void QFileSystemWatcherPrivate::initForcedEngine(const QString &forceName) void QFileSystemWatcherPrivate::initForcedEngine(const QString &forceName)
{ {
if(forced) if (watcher)
return; return;
Q_Q(QFileSystemWatcher); Q_Q(QFileSystemWatcher);
#if defined(Q_OS_LINUX) #if defined(Q_OS_LINUX)
if(forceName == QLatin1String("inotify")) { if (forceName == QLatin1String("inotify")) {
forced = QInotifyFileSystemWatcherEngine::create(); watcher = QInotifyFileSystemWatcherEngine::create();
} }
#else #else
Q_UNUSED(forceName); Q_UNUSED(forceName);
#endif #endif
if(forced) { if (watcher) {
QObject::connect(forced, QObject::connect(watcher,
SIGNAL(fileChanged(QString,bool)), SIGNAL(fileChanged(QString,bool)),
q, q,
SLOT(_q_fileChanged(QString,bool))); SLOT(_q_fileChanged(QString,bool)));
QObject::connect(forced, QObject::connect(watcher,
SIGNAL(directoryChanged(QString,bool)), SIGNAL(directoryChanged(QString,bool)),
q, q,
SLOT(_q_directoryChanged(QString,bool))); SLOT(_q_directoryChanged(QString,bool)));
@ -273,16 +273,16 @@ void QFileSystemWatcherPrivate::initForcedEngine(const QString &forceName)
void QFileSystemWatcherPrivate::initPollerEngine() void QFileSystemWatcherPrivate::initPollerEngine()
{ {
if(poller) if (watcher)
return; return;
Q_Q(QFileSystemWatcher); Q_Q(QFileSystemWatcher);
poller = new QPollingFileSystemWatcherEngine; // that was a mouthful watcher = new QPollingFileSystemWatcherEngine; // that was a mouthful
QObject::connect(poller, QObject::connect(watcher,
SIGNAL(fileChanged(QString,bool)), SIGNAL(fileChanged(QString,bool)),
q, q,
SLOT(_q_fileChanged(QString,bool))); SLOT(_q_fileChanged(QString,bool)));
QObject::connect(poller, QObject::connect(watcher,
SIGNAL(directoryChanged(QString,bool)), SIGNAL(directoryChanged(QString,bool)),
q, q,
SLOT(_q_directoryChanged(QString,bool))); SLOT(_q_directoryChanged(QString,bool)));
@ -390,17 +390,9 @@ QFileSystemWatcher::QFileSystemWatcher(const QStringList &paths, QObject *parent
QFileSystemWatcher::~QFileSystemWatcher() QFileSystemWatcher::~QFileSystemWatcher()
{ {
Q_D(QFileSystemWatcher); Q_D(QFileSystemWatcher);
if (d->native) { if (d->watcher) {
delete d->native; delete d->watcher;
d->native = Q_NULLPTR; d->watcher = Q_NULLPTR;
}
if (d->poller) {
delete d->poller;
d->poller = Q_NULLPTR;
}
if (d->forced) {
delete d->forced;
d->forced = Q_NULLPTR;
} }
} }
@ -423,7 +415,7 @@ QFileSystemWatcher::~QFileSystemWatcher()
*/ */
void QFileSystemWatcher::addPath(const QString &path) void QFileSystemWatcher::addPath(const QString &path)
{ {
if (path.isEmpty()) { if (Q_UNLIKELY(path.isEmpty())) {
qWarning("QFileSystemWatcher::addPath: path is empty"); qWarning("QFileSystemWatcher::addPath: path is empty");
return; return;
} }
@ -451,49 +443,42 @@ void QFileSystemWatcher::addPath(const QString &path)
void QFileSystemWatcher::addPaths(const QStringList &paths) void QFileSystemWatcher::addPaths(const QStringList &paths)
{ {
Q_D(QFileSystemWatcher); Q_D(QFileSystemWatcher);
if (paths.isEmpty()) { if (Q_UNLIKELY(paths.isEmpty())) {
qWarning("QFileSystemWatcher::addPaths: list is empty"); qWarning("QFileSystemWatcher::addPaths: list is empty");
return; return;
} }
QStringList p = paths;
QFileSystemWatcherEngine *engine = Q_NULLPTR;
#ifdef QT_BUILD_INTERNAL #ifdef QT_BUILD_INTERNAL
if(!objectName().startsWith(QLatin1String("_qt_autotest_force_engine_"))) { if (!objectName().startsWith(QLatin1String("_qt_autotest_force_engine_"))) {
#endif // QT_BUILD_INTERNAL #endif // QT_BUILD_INTERNAL
// Normal runtime case - search intelligently for best engine // Normal runtime case - search intelligently for best engine
if(d->native) { if (!d->watcher) {
engine = d->native;
} else {
d_func()->initPollerEngine(); d_func()->initPollerEngine();
engine = d->poller;
} }
#ifdef QT_BUILD_INTERNAL #ifdef QT_BUILD_INTERNAL
} else { } else {
// Autotest override case - use the explicitly selected engine only // Autotest override case - use the explicitly selected engine only
QString forceName = objectName().mid(26); QString forceName = objectName().mid(26);
if(forceName == QLatin1String("poller")) { if (forceName == QLatin1String("poller")) {
qDebug() << "QFileSystemWatcher: skipping native engine, using only polling engine"; qDebug() << "QFileSystemWatcher: skipping native engine, using only polling engine";
d_func()->initPollerEngine(); d_func()->initPollerEngine();
engine = d->poller; } else if (forceName == QLatin1String("native")) {
} else if(forceName == QLatin1String("native")) {
qDebug() << "QFileSystemWatcher: skipping polling engine, using only native engine"; qDebug() << "QFileSystemWatcher: skipping polling engine, using only native engine";
engine = d->native;
} else { } else {
qDebug() << "QFileSystemWatcher: skipping polling and native engine, using only explicit" << forceName << "engine"; qDebug() << "QFileSystemWatcher: skipping polling and native engine, using only explicit" << forceName << "engine";
d_func()->initForcedEngine(forceName); d_func()->initForcedEngine(forceName);
engine = d->forced;
} }
} }
#endif // QT_BUILD_INTERNAL #endif // QT_BUILD_INTERNAL
if(engine) QStringList p = paths;
p = engine->addPaths(p, &d->files, &d->directories); if (Q_LIKELY(d->watcher))
p = d->watcher->addPaths(paths, &d->files, &d->directories);
if (!p.isEmpty()) if (Q_UNLIKELY(!p.isEmpty())) {
qWarning("QFileSystemWatcher: failed to add paths: %s", qWarning("QFileSystemWatcher: failed to add paths: %s",
qPrintable(p.join(QLatin1String(", ")))); qPrintable(p.join(QLatin1String(", "))));
}
} }
/*! /*!
@ -503,7 +488,7 @@ void QFileSystemWatcher::addPaths(const QStringList &paths)
*/ */
void QFileSystemWatcher::removePath(const QString &path) void QFileSystemWatcher::removePath(const QString &path)
{ {
if (path.isEmpty()) { if (Q_UNLIKELY(path.isEmpty())) {
qWarning("QFileSystemWatcher::removePath: path is empty"); qWarning("QFileSystemWatcher::removePath: path is empty");
return; return;
} }
@ -517,18 +502,13 @@ void QFileSystemWatcher::removePath(const QString &path)
*/ */
void QFileSystemWatcher::removePaths(const QStringList &paths) void QFileSystemWatcher::removePaths(const QStringList &paths)
{ {
if (paths.isEmpty()) { if (Q_UNLIKELY(paths.isEmpty())) {
qWarning("QFileSystemWatcher::removePaths: list is empty"); qWarning("QFileSystemWatcher::removePaths: list is empty");
return; return;
} }
Q_D(QFileSystemWatcher); Q_D(QFileSystemWatcher);
QStringList p = paths; if (Q_LIKELY(d->watcher))
if (d->native) d->watcher->removePaths(paths, &d->files, &d->directories);
p = d->native->removePaths(p, &d->files, &d->directories);
if (d->poller)
p = d->poller->removePaths(p, &d->files, &d->directories);
if (d->forced)
p = d->forced->removePaths(p, &d->files, &d->directories);
} }
/*! /*!

View file

@ -94,7 +94,7 @@ public:
void initPollerEngine(); void initPollerEngine();
void initForcedEngine(const QString &); void initForcedEngine(const QString &);
QFileSystemWatcherEngine *native, *poller, *forced; QFileSystemWatcherEngine *watcher;
QStringList files, directories; QStringList files, directories;
// private slots // private slots