atomic_ref<T>::required_alignment
Section: 32.5.7.2 [atomics.ref.ops] Status: New Submitter: Andrey Semashev Opened: 2020-02-27 Last modified: 2020-09-06
Priority: 3
View all issues with New status.
Discussion:
N4849 32.5.7.2 [atomics.ref.ops]/1
describes atomic_ref<T>::required_alignment
constant as follows:
The alignment required for an object to be referenced by an atomic reference, which is at least
alignof(T)
.
This wording allows for an implementation to always define required_alignment
to be equal to alignof(T)
and implement atomic operations using locking,
even if a lock-free implementation is possible at a higher alignment. For example,
on x86-64, atomic_ref<complex<double>>
could be lock-free
only when the referred object is aligned to 16 bytes, but the above definition
allows an implementation to define required_alignment
to 8 and use locking.
required_alignment
reflects alignment required for lock-free
operations, if possible, and not just minimum alignment required for any kind of
implementation.
The suggested resolution is to change the wording so that it is clear
that required_alignment
indicates the alignment required for lock-free
implementation, if one is possible, or alignof(T)
otherwise.
Further, the note in 32.5.7.2 [atomics.ref.ops]/2 contains this sentence:
Further, whether operations on an
atomic_ref
are lock-free could depend on the alignment of the referenced object.
This sentence is misleading, because according to is_lock_free()
definition in 32.5.7.2 [atomics.ref.ops]/4, the lock-free property is not
allowed to depend on the alignment of a particular referenced object
(is_lock_free()
must return true
or false
if
operations on all objects of the given type T
are lock-free or
not). In other words, atomic_ref
can only refer to an object aligned at
least to required_alignment
and its lock-free capability cannot depend
on the actual runtime alignment of the object.
[2020-04-04 Issue Prioritization]
Priority to 3 after reflector discussion.
Proposed resolution:
This wording is relative to N4849.
Modify 32.5.7.2 [atomics.ref.ops] as indicated:
static constexpr size_t required_alignment;-1- Let
-2- [Note: Hardware could require an object referenced by anA
be tThe alignment required for an object to be referenced by an atomic reference,which is at leastso thatalignof(T)
is_always_lock_free
istrue
. If there is no such alignment orA
is less thanalignof(T)
,required_alignment
equalsalignof(T)
. Otherwise,required_alignment
equalsA
.atomic_ref
to have stricter alignment (6.7.6 [basic.align]) than other objects of typeT
.Further, whether operations on anFor example, lock-free operations onatomic_ref
are lock-free could depend on the alignment of the referenced object.std::complex<double>
could be supported only if aligned to2*alignof(double)
. — end note]