[Development] atomic reference counting implementation

Mutz, Marc marc at kdab.com
Thu Aug 8 12:57:39 CEST 2019


On 2019-08-08 12:48, Lars Knoll wrote:
> Even if we don’t change QAtomic, there seems to be also little need to
> introduce whole new classes because QSD, QSDP and QESDP do *not*
> specify that refcounting is ordered.

Once more, please don't mix these things up:

- QISP is a replacement for QSDP and QESDP, because the existing classes 
have (among others) the following problems:

   * QSDP is detach-on-mutable access, which means it emits tons of 
unneeded atomic operations

   * In QSDP something simple as if (!d) becomes dead code, because 
d.data() is called, which detaches, and thus always returns a true 
value.

   * QESDP is not propagating const deeply, which has led to some 
non-reentrancy issues

QISP was designed for fixing _these_, as I believe changing either of 
the existing classes breaks a lot of code, sometimes silently. I 
originally wanted to take Qt ref-counting private with 
https://codereview.qt-project.org/c/qt/qtbase/+/115213 but as this was 
discussed in length in the thread about classes under consideration for 
deprecation/removal in Qt 6, people wanted a public replacement for QSDP 
and QESDP, thus QISP was devised.

So, yes, QSDP and QESDP work without QSharedData, they also work with 
QtPrivate::RefCount as well as a manual member QAtomicInt as long as 
it's called 'ref'. They don't work with std::atomic, though, and they 
don't work with a plain int. QISP does. It probably even works for 
release/retain API like Apple of MS COM.



Now Phillipe brought the issue of the inefficient QAtomic::ref() to the 
table again, I realized that QISP can solve that, too: 
https://codereview.qt-project.org/c/qt/qtbase/+/269919 adds a QSD 
replacement and ports QTimeZone to it. Minimally-invasive, invisible to 
users (as in BC and SC) and ... it leaves QSD alone.

Finally, as much as I'd've liked to take Qt ref-counting private, I now 
think that the Herb-style ref-counting optimisation pulls the weight of 
adding a new version of QSharedData: QIntrusivelyRefCounted.

Thanks,
Marc


More information about the Development mailing list