Skip to content

Commit e89d201

Browse files
committed
QBasicAtomicInteger: Add (internal) refRelaxed
It is known that incrementing the refcount can use relaxed semantics, compare https://web.archive.org/web/20251016043603/https://devblogs.microsoft.com/oldnewthing/20251015-00/?p=111686 However, we can't modify QBasicAtomic::ref, because that is documented to use "ordered" semantics. So introduce a new (internal) refRelaxed method, which simply calls fetchAndAddRelaxed(1) instead. Compared to ref, we also do not return anything, as no expected user has a need for the return value (and it only causes more work for the compiler to get rid of it again). Our deref operation is still using acquire_release semantics, so everything is fine. Port QArrayData to use it as a first user so that the functionality is tested. Change-Id: I678870551fe85b83d9bb073ddb5947e649845264 Reviewed-by: Marc Mutz <[email protected]>
1 parent 13f521e commit e89d201

File tree

4 files changed

+16
-1
lines changed

4 files changed

+16
-1
lines changed

src/corelib/thread/qatomic.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,19 @@
315315
\sa deref(), operator++()
316316
*/
317317

318+
/*!
319+
\fn template <typename T> void QAtomicInteger<T>::refRelaxed()
320+
\internal
321+
Atomically increments the value of this QAtomicInteger.
322+
323+
In contrast to ref(), this uses relaxed semantics, which is
324+
all that is needed for reference counting (together with deref's
325+
acquire-release semantics).
326+
It also doesn't return anything.
327+
328+
\sa deref(), operator++()
329+
*/
330+
318331
/*!
319332
\fn template <typename T> T QAtomicInteger<T>::operator++()
320333
\since 5.3

src/corelib/thread/qatomic.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class QAtomicInteger : public QBasicAtomicInteger<T>
4646
static constexpr bool isReferenceCountingWaitFree();
4747

4848
bool ref();
49+
void refRelaxed();
4950
bool deref();
5051

5152
static constexpr bool isTestAndSetNative();

src/corelib/thread/qbasicatomic.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class QBasicAtomicInteger
4646
static constexpr bool isReferenceCountingWaitFree() noexcept { return Ops::isReferenceCountingWaitFree(); }
4747

4848
bool ref() noexcept { return Ops::ref(_q_value); }
49+
void refRelaxed() noexcept { Ops::fetchAndAddRelaxed(_q_value, 1); }
4950
bool deref() noexcept { return Ops::deref(_q_value); }
5051

5152
static constexpr bool isTestAndSetNative() noexcept { return Ops::isTestAndSetNative(); }

src/corelib/tools/qarraydata.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ struct QArrayData
5858
/// Returns true if sharing took place
5959
bool ref() noexcept
6060
{
61-
ref_.ref();
61+
ref_.refRelaxed(); // suffices for ref-counting
6262
return true;
6363
}
6464

0 commit comments

Comments
 (0)