File indexing completed on 2024-05-12 15:58:50

0001 /*
0002  *  SPDX-FileCopyrightText: 2020 Dmitry Kazakov <dimula73@gmail.com>
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 #ifndef KISBUSYWAITBROKER_H
0007 #define KISBUSYWAITBROKER_H
0008 
0009 #include <kis_types.h>
0010 #include "kritaimage_export.h"
0011 
0012 /**
0013  * @brief a simple singleton class for tracking busy-waits on the image and
0014  * breaking deadlock ties when needed.
0015  *
0016  * When KisImage::barrierLock()/waitForDone() goes into sleep, waiting for the
0017  * worker threads to finish, it notifies KisBusyWaitBroker about that. Therefore
0018  * worker threads know that GUI thread is inactive now and can resolve the ties.
0019  */
0020 class KRITAIMAGE_EXPORT KisBusyWaitBroker
0021 {
0022 public:
0023     KisBusyWaitBroker();
0024     ~KisBusyWaitBroker();
0025 
0026     static KisBusyWaitBroker* instance();
0027 
0028     void notifyWaitOnImageStarted(KisImage *image);
0029     void notifyWaitOnImageEnded(KisImage *image);
0030 
0031     void notifyGeneralWaitStarted();
0032     void notifyGeneralWaitEnded();
0033 
0034     /**
0035      * Set a callback that is called before image goes into sleep. This callback
0036      * may show the user some feedback window with a progress bar.
0037      *
0038      * This callback is expected to be initialized dorung the construction
0039      * of KisPart.
0040      */
0041     void setFeedbackCallback(std::function<void(KisImageSP)> callback);
0042 
0043     /**
0044      * @return true if GUI thread is currently sleeping on an image lock. When a worker
0045      * thread is sure that GUI thread is sleeping, it has a bit more freedom in manipulating
0046      * the image (and especially vector shapes)
0047      */
0048     bool guiThreadIsWaitingForBetterWeather() const;
0049 
0050 private:
0051 
0052 private:
0053     Q_DISABLE_COPY(KisBusyWaitBroker);
0054 
0055     struct Private;
0056     QScopedPointer<Private> m_d;
0057 };
0058 
0059 #endif // KISBUSYWAITBROKER_H