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

#include <kis_lazy_wait_condition.h>

Public Member Functions

void endWaiting ()
 
void initWaiting ()
 
bool isSomeoneWaiting ()
 
 KisLazyWaitCondition ()
 
bool wait (unsigned long time=ULONG_MAX)
 
void wakeAll ()
 

Private Attributes

QWaitCondition m_condition
 
QMutex m_mutex
 
volatile int m_waitCounter
 
int m_wakeupCounter
 

Detailed Description

This class is used for catching a particular condition met. We call it "lazy", because the decision about the condition is made by the waiting thread itself. The other thread, "wakingup" one, just points the former one the moments of time when the condition might have been satisfied. This creates some limitations for the condition (see a note in the end of the text).

Usage pattern:

Waiting thread:

KisLazyWaitCondition condition; condition.initWaiting(); // (1) while(!checkSatisfied()) { condition.wait(); } condition.endWaiting(); // (2)

Wakingup thread:

if(checkMightSatisfied()) { condition.wakeAll(); }

If the condition is met and reported, it is guaranteed that all the threads, those are currently running between lines (1) and (2) will be waken up and leave the loop.

NOTE: The condition checkSatisfied() must not change it's state, until all the waiting threads leave the waiting loop. This requirement must be guaranteed by the user of this class

Definition at line 50 of file kis_lazy_wait_condition.h.

Constructor & Destructor Documentation

◆ KisLazyWaitCondition()

KisLazyWaitCondition::KisLazyWaitCondition ( )
inline

Definition at line 53 of file kis_lazy_wait_condition.h.

Member Function Documentation

◆ endWaiting()

void KisLazyWaitCondition::endWaiting ( )
inline

Definition at line 68 of file kis_lazy_wait_condition.h.

68 {
69 QMutexLocker locker(&m_mutex);
71 }

References m_mutex, and m_waitCounter.

◆ initWaiting()

void KisLazyWaitCondition::initWaiting ( )
inline

Definition at line 59 of file kis_lazy_wait_condition.h.

59 {
60 QMutexLocker locker(&m_mutex);
61 if(!m_waitCounter) {
63 }
64
66 }

References m_mutex, m_waitCounter, and m_wakeupCounter.

◆ isSomeoneWaiting()

bool KisLazyWaitCondition::isSomeoneWaiting ( )
inline

Definition at line 95 of file kis_lazy_wait_condition.h.

95 {
96 return m_waitCounter;
97 }

References m_waitCounter.

◆ wait()

bool KisLazyWaitCondition::wait ( unsigned long time = ULONG_MAX)
inline

Definition at line 73 of file kis_lazy_wait_condition.h.

73 {
74 QMutexLocker locker(&m_mutex);
75 bool result = true;
76 if(!m_wakeupCounter) {
77 result = m_condition.wait(&m_mutex, time);
78 }
79 if(result) {
81 }
82 return result;
83 }

References m_condition, m_mutex, and m_wakeupCounter.

◆ wakeAll()

void KisLazyWaitCondition::wakeAll ( )
inline

Definition at line 85 of file kis_lazy_wait_condition.h.

85 {
86 if(!m_waitCounter) return;
87
88 QMutexLocker locker(&m_mutex);
89 if(m_waitCounter) {
91 m_condition.wakeAll();
92 }
93 }

References m_condition, m_mutex, m_waitCounter, and m_wakeupCounter.

Member Data Documentation

◆ m_condition

QWaitCondition KisLazyWaitCondition::m_condition
private

Definition at line 101 of file kis_lazy_wait_condition.h.

◆ m_mutex

QMutex KisLazyWaitCondition::m_mutex
private

Definition at line 100 of file kis_lazy_wait_condition.h.

◆ m_waitCounter

volatile int KisLazyWaitCondition::m_waitCounter
private

Definition at line 102 of file kis_lazy_wait_condition.h.

◆ m_wakeupCounter

int KisLazyWaitCondition::m_wakeupCounter
private

Definition at line 103 of file kis_lazy_wait_condition.h.


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