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

#include <kis_lock_free_cache.h>

Public Types

typedef int SeqValue
 

Public Member Functions

bool endRead (int seq) const
 
void endWrite (int seq)
 
void invalidate ()
 
bool startRead (int *seq) const
 
bool startWrite (int *seq)
 

Private Member Functions

int incrementSeqNo (int value)
 

Private Attributes

QAtomicInt m_value
 

Static Private Attributes

static const int IsValidMask = 0x0100
 
static const int SeqNoIncrement = 0x0200
 
static const int SeqNoMask = ~(WritersCountMask | IsValidMask)
 
static const int WritersCountIncrement = 0x0001
 
static const int WritersCountMask = 0x00FF
 

Detailed Description

This class implements a state variable for a lockfree cache object. The implementation is actually a variation of a 'seqlock', but with a simple modification: the values of "cache validity", "number of writers" and "sequence number" are multiplexed in a single int value.

Definition at line 19 of file kis_lock_free_cache.h.

Member Typedef Documentation

◆ SeqValue

Definition at line 27 of file kis_lock_free_cache.h.

Member Function Documentation

◆ endRead()

bool KisCacheStateValue::endRead ( int seq) const
inline

Definition at line 45 of file kis_lock_free_cache.h.

45 {
46 bool result = seq == m_value;
47 return result;
48 }

References m_value.

◆ endWrite()

void KisCacheStateValue::endWrite ( int seq)
inline

Definition at line 68 of file kis_lock_free_cache.h.

68 {
69 int oldValue;
70 int newValue;
71 do {
72 oldValue = m_value;
73
74 if (oldValue == seq) {
75 newValue = (incrementSeqNo(oldValue) - WritersCountIncrement) | IsValidMask;
76 } else {
77 newValue = (incrementSeqNo(oldValue) - WritersCountIncrement) & ~IsValidMask;
78 }
79 } while(!m_value.testAndSetOrdered(oldValue, newValue));
80 }
int incrementSeqNo(int value)
static const int IsValidMask
static const int WritersCountIncrement

References incrementSeqNo(), IsValidMask, m_value, and WritersCountIncrement.

◆ incrementSeqNo()

int KisCacheStateValue::incrementSeqNo ( int value)
inlineprivate

Definition at line 83 of file kis_lock_free_cache.h.

83 {
84 // handle overflow properly
85 if ((value & SeqNoMask) == SeqNoMask) {
86 value = value & ~SeqNoMask;
87 } else {
89 }
90 return value;
91 }
float value(const T *src, size_t ch)
static const int SeqNoMask
static const int SeqNoIncrement

References SeqNoIncrement, SeqNoMask, and value().

◆ invalidate()

void KisCacheStateValue::invalidate ( )
inline

Definition at line 29 of file kis_lock_free_cache.h.

29 {
30 int oldValue;
31 int newValue = -1;
32 do {
33 oldValue = m_value;
34 newValue = incrementSeqNo(oldValue) & ~IsValidMask;
35 } while(!m_value.testAndSetOrdered(oldValue, newValue));
36 }

References incrementSeqNo(), and m_value.

◆ startRead()

bool KisCacheStateValue::startRead ( int * seq) const
inline

Definition at line 38 of file kis_lock_free_cache.h.

38 {
39 *seq = m_value;
40
41 return (*seq & IsValidMask) &&
42 !(*seq & WritersCountMask);
43 }
static const int WritersCountMask

References IsValidMask, m_value, and WritersCountMask.

◆ startWrite()

bool KisCacheStateValue::startWrite ( int * seq)
inline

Definition at line 51 of file kis_lock_free_cache.h.

51 {
52 int oldValue;
53 int newValue;
54 do {
55 oldValue = m_value;
56 if ((oldValue & IsValidMask) ||
57 (oldValue & WritersCountMask)) {
58
59 return false;
60 }
61 newValue = incrementSeqNo(oldValue) + WritersCountIncrement;
62 } while(!m_value.testAndSetOrdered(oldValue, newValue));
63
64 *seq = newValue;
65 return true;
66 }

References incrementSeqNo(), IsValidMask, m_value, WritersCountIncrement, and WritersCountMask.

Member Data Documentation

◆ IsValidMask

const int KisCacheStateValue::IsValidMask = 0x0100
staticprivate

Definition at line 23 of file kis_lock_free_cache.h.

◆ m_value

QAtomicInt KisCacheStateValue::m_value
private

Definition at line 94 of file kis_lock_free_cache.h.

◆ SeqNoIncrement

const int KisCacheStateValue::SeqNoIncrement = 0x0200
staticprivate

Definition at line 25 of file kis_lock_free_cache.h.

◆ SeqNoMask

const int KisCacheStateValue::SeqNoMask = ~(WritersCountMask | IsValidMask)
staticprivate

Definition at line 24 of file kis_lock_free_cache.h.

◆ WritersCountIncrement

const int KisCacheStateValue::WritersCountIncrement = 0x0001
staticprivate

Definition at line 22 of file kis_lock_free_cache.h.

◆ WritersCountMask

const int KisCacheStateValue::WritersCountMask = 0x00FF
staticprivate

Definition at line 21 of file kis_lock_free_cache.h.


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