Skip to content

armgit / krita

Public Imported from GitHub
0

Krita is a free and open source cross-platform application that offers an end-to-end solution for creating digital art files from scratch built on the KDE and Qt frameworks.

C++

Do not block progress bar when there is contestion on the image

Dmitry Kazakove67889d0d83e
diff --git a/libs/image/kis_simple_update_queue.cpp b/libs/image/kis_simple_update_queue.cpp
index 3a9c52ac08..d441f90620 100644
--- a/libs/image/kis_simple_update_queue.cpp
+++ b/libs/image/kis_simple_update_queue.cpp
@@ -268,6 +268,13 @@ bool KisSimpleUpdateQueue::isEmpty() const
return m_updatesList.isEmpty() && m_spontaneousJobsList.isEmpty();
}
+bool KisSimpleUpdateQueue::isIdle() const
+{
+ std::unique_lock<QMutex> locker(m_lock, std::try_to_lock);
+ if (!locker.owns_lock()) return false;
+ return m_updatesList.isEmpty() && m_spontaneousJobsList.isEmpty();
+}
+
qint32 KisSimpleUpdateQueue::sizeMetric() const
{
QMutexLocker locker(&m_lock);
diff --git a/libs/image/kis_simple_update_queue.h b/libs/image/kis_simple_update_queue.h
index 3a573a7ce9..b0490344cd 100644
--- a/libs/image/kis_simple_update_queue.h
+++ b/libs/image/kis_simple_update_queue.h
@@ -42,7 +42,21 @@ public:
void optimize();
+ /**
+ * Returns true if the update queue is empty, i.e. there are
+ * no update or spontaneous jobs pending
+ */
bool isEmpty() const;
+
+ /**
+ * Works in the same way as isEmpty(), except that it will not
+ * wait on the mutex in case there is any contestion on the queue.
+ *
+ * If some other threads are contending on the queue, it will just
+ * return `false`, whatever the state of the queue is.
+ */
+ bool isIdle() const;
+
qint32 sizeMetric() const;
void updateSettings();
diff --git a/libs/image/kis_strokes_queue.cpp b/libs/image/kis_strokes_queue.cpp
index 3fd31b480f..5da6871066 100644
--- a/libs/image/kis_strokes_queue.cpp
+++ b/libs/image/kis_strokes_queue.cpp
@@ -646,6 +646,13 @@ bool KisStrokesQueue::isEmpty() const
return m_d->strokesQueue.isEmpty();
}
+bool KisStrokesQueue::isIdle() const
+{
+ std::unique_lock<QMutex> locker(m_d->mutex, std::try_to_lock);
+ if (!locker.owns_lock()) return false;
+ return m_d->strokesQueue.isEmpty();
+}
+
qint32 KisStrokesQueue::sizeMetric() const
{
QMutexLocker locker(&m_d->mutex);
diff --git a/libs/image/kis_strokes_queue.h b/libs/image/kis_strokes_queue.h
index c1b4f95a86..3c31737ab3 100644
--- a/libs/image/kis_strokes_queue.h
+++ b/libs/image/kis_strokes_queue.h
@@ -44,8 +44,22 @@ public:
void processQueue(KisUpdaterContext &updaterContext,
bool externalJobsPending);
bool needsExclusiveAccess() const;
+
+ /**
+ * Returns true if the strokes queue is empty, i.e. there are
+ * no stroke jobs pending
+ */
bool isEmpty() const;
+ /**
+ * Works in the same way as isEmpty(), except that it will not
+ * wait on the mutex in case there is any contestion on the queue.
+ *
+ * If some other threads are contending on the queue, it will just
+ * return `false`, whatever the state of the queue is.
+ */
+ bool isIdle() const;
+
qint32 sizeMetric() const;
KUndo2MagicString currentStrokeName() const;
bool hasOpenedStrokes() const;
diff --git a/libs/image/kis_update_scheduler.cpp b/libs/image/kis_update_scheduler.cpp
index d5f9681deb..8a37ce56de 100644
--- a/libs/image/kis_update_scheduler.cpp
+++ b/libs/image/kis_update_scheduler.cpp
@@ -308,6 +308,18 @@ void KisUpdateScheduler::unlock(bool resetLodLevels)
bool KisUpdateScheduler::isIdle()
{
+ /**
+ * First check the low-cost signs of the image not being
+ * idle to avoid contending over locks with the worker
+ * threads.
+ */
+ if(!m_d->updatesQueue.isIdle() ||
+ !m_d->strokesQueue.isIdle() ||
+ !m_d->updaterContext.isIdle()) {
+
+ return false;
+ }
+
bool result = false;
if (tryBarrierLock()) {
diff --git a/libs/image/kis_updater_context.cpp b/libs/image/kis_updater_context.cpp
index 85731ec035..8fece001d8 100644
--- a/libs/image/kis_updater_context.cpp
+++ b/libs/image/kis_updater_context.cpp
@@ -215,6 +215,14 @@ void KisUpdaterContext::waitForDone()
}
}
+bool KisUpdaterContext::isIdle() const
+{
+ std::unique_lock<QMutex> locker(m_runningThreadsMutex, std::try_to_lock);
+ if (!locker.owns_lock()) return false;
+
+ return m_numRunningThreads == 0;
+}
+
bool KisUpdaterContext::walkerIntersectsJob(KisBaseRectsWalkerSP walker,
const KisUpdateJobItem* job)
{
diff --git a/libs/image/kis_updater_context.h b/libs/image/kis_updater_context.h
index 7f2775b068..ae0043f8cd 100644
--- a/libs/image/kis_updater_context.h
+++ b/libs/image/kis_updater_context.h
@@ -98,6 +98,15 @@ public:
*/
void waitForDone();
+ /**
+ * Returns true if there are no jobs running in the context
+ *
+ * If there is any contestion on the context's mutex, the function
+ * will simply return false, whatever context's state really is.
+ * Obviosly, if there is a contestion, the object is not "idle" :)
+ */
+ bool isIdle() const;
+
/**
* Locks the context to guarantee an exclusive access
* to the context
@@ -149,7 +158,7 @@ protected:
QReadWriteLock m_exclusiveJobLock;
QMutex m_lock;
- QMutex m_runningThreadsMutex;
+ mutable QMutex m_runningThreadsMutex;
int m_numRunningThreads = 0;
QWaitCondition m_waitForDoneCondition;
QVector<KisUpdateJobItem*> m_jobs;