Krita Source Code Documentation
Loading...
Searching...
No Matches
KisUpdateJobItem Class Reference

#include <kis_update_job_item.h>

+ Inheritance diagram for KisUpdateJobItem:

Public Types

enum class  Type : int {
  EMPTY = 0 , WAITING , MERGE , STROKE ,
  SPONTANEOUS
}
 

Public Member Functions

const QRect & accessRect () const
 
const QRect & changeRect () const
 
bool isRunning () const
 
 KisUpdateJobItem (KisUpdaterContext *updaterContext)
 
void run () override
 
void runMergeJob ()
 
void setDone ()
 
bool setSpontaneousJob (KisSpontaneousJob *spontaneousJob)
 
bool setStrokeJob (KisStrokeJob *strokeJob)
 
bool setWalker (KisBaseRectsWalkerSP walker)
 
KisStrokeJobData::Sequentiality strokeJobSequentiality () const
 
Type type () const
 
 ~KisUpdateJobItem () override
 

Private Member Functions

ALWAYS_INLINE void runImpl ()
 
KisStrokeJobstrokeJob () const
 
void testingSetDone ()
 
KisBaseRectsWalkerSP walker () const
 

Private Attributes

QRect m_accessRect
 
std::atomic< Typem_atomicType {Type::EMPTY}
 
QRect m_changeRect
 
bool m_exclusive {false}
 
KisAsyncMerger m_merger
 
KisRunnableWithDebugNamem_runnableJob {0}
 
volatile KisStrokeJobData::Sequentiality m_strokeJobSequentiality {KisStrokeJobData::SEQUENTIAL}
 
KisUpdaterContextm_updaterContext {0}
 
KisBaseRectsWalkerSP m_walker
 

Friends

class KisSimpleUpdateQueueTest
 
class KisStrokesQueueTest
 
class KisTestableUpdaterContext
 
class KisUpdaterContext
 
class KisUpdateSchedulerTest
 

Detailed Description

Definition at line 24 of file kis_update_job_item.h.

Member Enumeration Documentation

◆ Type

enum class KisUpdateJobItem::Type : int
strong
Enumerator
EMPTY 
WAITING 
MERGE 
STROKE 
SPONTANEOUS 

Definition at line 28 of file kis_update_job_item.h.

Constructor & Destructor Documentation

◆ KisUpdateJobItem()

KisUpdateJobItem::KisUpdateJobItem ( KisUpdaterContext * updaterContext)
inline

Definition at line 37 of file kis_update_job_item.h.

38 : m_updaterContext(updaterContext)
39 {
40 setAutoDelete(false);
42 }
std::atomic< Type > m_atomicType
KisUpdaterContext * m_updaterContext
#define KIS_SAFE_ASSERT_RECOVER_NOOP(cond)
Definition kis_assert.h:130

References KIS_SAFE_ASSERT_RECOVER_NOOP.

◆ ~KisUpdateJobItem()

KisUpdateJobItem::~KisUpdateJobItem ( )
inlineoverride

Definition at line 43 of file kis_update_job_item.h.

44 {
45 delete m_runnableJob;
46 }
KisRunnableWithDebugName * m_runnableJob

Member Function Documentation

◆ accessRect()

const QRect & KisUpdateJobItem::accessRect ( ) const
inline

Definition at line 200 of file kis_update_job_item.h.

200 {
201 return m_accessRect;
202 }

◆ changeRect()

const QRect & KisUpdateJobItem::changeRect ( ) const
inline

Definition at line 204 of file kis_update_job_item.h.

204 {
205 return m_changeRect;
206 }

◆ isRunning()

bool KisUpdateJobItem::isRunning ( ) const
inline

Definition at line 192 of file kis_update_job_item.h.

192 {
193 return m_atomicType >= Type::MERGE;
194 }

◆ run()

void KisUpdateJobItem::run ( )
inlineoverride

Definition at line 48 of file kis_update_job_item.h.

48 {
49 runImpl();
50
51 // notify that the job is exiting and wake everybody
52 // waiting on wakeForDone()
54 }
ALWAYS_INLINE void runImpl()

◆ runImpl()

ALWAYS_INLINE void KisUpdateJobItem::runImpl ( )
inlineprivate

Here we break the idea of QThreadPool a bit. Ideally, we should split the jobs into distinct QRunnable objects and pass all of them to QThreadPool. That is a nice idea, but it doesn't work well when the jobs are small enough and the number of available cores is high (>4 cores). It this case the threads just tend to execute the job very quickly and go to sleep, which is an expensive operation.

To overcome this problem we try to bulk-process the jobs. In sigJobFinished() signal (which is DirectConnection), the context may add the job to ourselves(!!!), so we switch from "done" state into "running" again.

Definition at line 58 of file kis_update_job_item.h.

58 {
59 if (!isRunning()) return;
60
74 while (1) {
76
77 if(m_exclusive) {
79 } else {
81 }
82
85 } else {
88
89 if (m_runnableJob) {
90#ifdef DEBUG_JOBS_SEQUENCE
92 qDebug() << "running: stroke" << m_runnableJob->debugName();
93 } else if (m_atomicType == Type::SPONTANEOUS) {
94 qDebug() << "running: spont " << m_runnableJob->debugName();
95 } else {
96 qDebug() << "running: unkn. " << m_runnableJob->debugName();
97 }
98#endif
99
101 }
102 }
103
104 setDone();
105
107
108 // may flip the current state from Waiting -> Running again
110
112
113 // try to exit the loop. Please note, that no one can flip the state from
114 // WAITING to EMPTY except ourselves!
115 Type expectedValue = Type::WAITING;
116 if (m_atomicType.compare_exchange_strong(expectedValue, Type::EMPTY)) {
117 break;
118 }
119 }
120 }
virtual QString debugName() const =0
virtual void run()=0
QReadWriteLock m_exclusiveJobLock
#define KIS_SAFE_ASSERT_RECOVER_RETURN(cond)
Definition kis_assert.h:128
#define KIS_ASSERT(cond)
Definition kis_assert.h:33

References KIS_ASSERT, and KIS_SAFE_ASSERT_RECOVER_RETURN.

◆ runMergeJob()

void KisUpdateJobItem::runMergeJob ( )
inline

Definition at line 124 of file kis_update_job_item.h.

124 {
127 // dbgKrita << "Executing merge job" << m_walker->changeRect()
128 // << "on thread" << QThread::currentThreadId();
129
130#ifdef DEBUG_JOBS_SEQUENCE
131 qDebug() << "running: merge " << m_walker->startNode() << m_walker->changeRect();
132
133#endif
134
136
137 QRect changeRect = m_walker->changeRect();
139 }
void startMerge(KisBaseRectsWalker &walker, bool notifyClones=true)
KisNodeSP startNode() const
KisAsyncMerger m_merger
const QRect & changeRect() const
KisBaseRectsWalkerSP m_walker
void continueUpdate(const QRect &rc)

References KIS_SAFE_ASSERT_RECOVER_RETURN.

◆ setDone()

void KisUpdateJobItem::setDone ( )
inline

Definition at line 185 of file kis_update_job_item.h.

185 {
186 m_walker = 0;
187 delete m_runnableJob;
188 m_runnableJob = 0;
190 }

◆ setSpontaneousJob()

bool KisUpdateJobItem::setSpontaneousJob ( KisSpontaneousJob * spontaneousJob)
inline

Definition at line 172 of file kis_update_job_item.h.

172 {
174
175 m_runnableJob = spontaneousJob;
176
177 m_exclusive = spontaneousJob->isExclusive();
178 m_walker = 0;
179 m_accessRect = m_changeRect = QRect();
180
181 const Type oldState = m_atomicType.exchange(Type::SPONTANEOUS);
182 return oldState == Type::EMPTY;
183 }

References KisSpontaneousJob::isExclusive(), and KIS_ASSERT.

◆ setStrokeJob()

bool KisUpdateJobItem::setStrokeJob ( KisStrokeJob * strokeJob)
inline

Definition at line 157 of file kis_update_job_item.h.

157 {
159
162
164 m_walker = 0;
165 m_accessRect = m_changeRect = QRect();
166
167 const Type oldState = m_atomicType.exchange(Type::STROKE);
168 return oldState == Type::EMPTY;
169 }
bool isExclusive() const
KisStrokeJobData::Sequentiality sequentiality() const
KisStrokeJob * strokeJob() const
volatile KisStrokeJobData::Sequentiality m_strokeJobSequentiality

References KisStrokeJob::isExclusive(), KIS_ASSERT, and KisStrokeJob::sequentiality().

◆ setWalker()

bool KisUpdateJobItem::setWalker ( KisBaseRectsWalkerSP walker)
inline

Definition at line 142 of file kis_update_job_item.h.

142 {
144
148
149 m_exclusive = false;
150 m_runnableJob = 0;
151
152 const Type oldState = m_atomicType.exchange(Type::MERGE);
153 return oldState == Type::EMPTY;
154 }
KisBaseRectsWalkerSP walker() const

References KisBaseRectsWalker::accessRect(), KisBaseRectsWalker::changeRect(), and KIS_ASSERT.

◆ strokeJob()

KisStrokeJob * KisUpdateJobItem::strokeJob ( ) const
inlineprivate

Definition at line 227 of file kis_update_job_item.h.

227 {
228 KisStrokeJob *job = dynamic_cast<KisStrokeJob*>(m_runnableJob);
229 Q_ASSERT(job);
230
231 return job;
232 }

◆ strokeJobSequentiality()

KisStrokeJobData::Sequentiality KisUpdateJobItem::strokeJobSequentiality ( ) const
inline

Definition at line 208 of file kis_update_job_item.h.

208 {
210 }

◆ testingSetDone()

void KisUpdateJobItem::testingSetDone ( )
inlineprivate

Definition at line 234 of file kis_update_job_item.h.

234 {
235 setDone();
236 }

◆ type()

Type KisUpdateJobItem::type ( ) const
inline

Definition at line 196 of file kis_update_job_item.h.

196 {
197 return m_atomicType;
198 }

◆ walker()

KisBaseRectsWalkerSP KisUpdateJobItem::walker ( ) const
inlineprivate

Definition at line 223 of file kis_update_job_item.h.

223 {
224 return m_walker;
225 }

Friends And Related Symbol Documentation

◆ KisSimpleUpdateQueueTest

friend class KisSimpleUpdateQueueTest
friend

Definition at line 218 of file kis_update_job_item.h.

◆ KisStrokesQueueTest

friend class KisStrokesQueueTest
friend

Definition at line 219 of file kis_update_job_item.h.

◆ KisTestableUpdaterContext

friend class KisTestableUpdaterContext
friend

Open walker and stroke job for the testing suite. Please, do not use it in production code.

Definition at line 217 of file kis_update_job_item.h.

◆ KisUpdaterContext

friend class KisUpdaterContext
friend

Definition at line 221 of file kis_update_job_item.h.

◆ KisUpdateSchedulerTest

friend class KisUpdateSchedulerTest
friend

Definition at line 220 of file kis_update_job_item.h.

Member Data Documentation

◆ m_accessRect

QRect KisUpdateJobItem::m_accessRect
private

These rects cache actual values from the walker to eliminate concurrent access to a walker structure

Definition at line 260 of file kis_update_job_item.h.

◆ m_atomicType

std::atomic<Type> KisUpdateJobItem::m_atomicType {Type::EMPTY}
private

Definition at line 241 of file kis_update_job_item.h.

◆ m_changeRect

QRect KisUpdateJobItem::m_changeRect
private

Definition at line 261 of file kis_update_job_item.h.

◆ m_exclusive

bool KisUpdateJobItem::m_exclusive {false}
private

Definition at line 240 of file kis_update_job_item.h.

240{false};

◆ m_merger

KisAsyncMerger KisUpdateJobItem::m_merger
private

Definition at line 254 of file kis_update_job_item.h.

◆ m_runnableJob

KisRunnableWithDebugName* KisUpdateJobItem::m_runnableJob {0}
private

Runnable jobs part The job is owned by the context and deleted after completion

Definition at line 248 of file kis_update_job_item.h.

248{0};

◆ m_strokeJobSequentiality

volatile KisStrokeJobData::Sequentiality KisUpdateJobItem::m_strokeJobSequentiality {KisStrokeJobData::SEQUENTIAL}
private

◆ m_updaterContext

KisUpdaterContext* KisUpdateJobItem::m_updaterContext {0}
private

Definition at line 239 of file kis_update_job_item.h.

239{0};

◆ m_walker

KisBaseRectsWalkerSP KisUpdateJobItem::m_walker
private

Merge jobs part

Definition at line 253 of file kis_update_job_item.h.


The documentation for this class was generated from the following file: