|
| 1 | +<?xml version='1.0' encoding='utf-8' standalone='no'?> |
| 2 | +<!DOCTYPE issue SYSTEM "lwg-issue.dtd"> |
| 3 | + |
| 4 | +<issue num="4377" status="New"> |
| 5 | +<title>Misleading note about lock-free property of `std::atomic_ref`</title> |
| 6 | +<section> |
| 7 | +<sref ref="[atomics.ref.ops]"/> |
| 8 | +</section> |
| 9 | +<submitter>Brian Bi</submitter> |
| 10 | +<date>15 Sep 2025</date> |
| 11 | +<priority>99</priority> |
| 12 | + |
| 13 | +<discussion> |
| 14 | +<p> |
| 15 | +Note 1 to <sref ref="[atomics.ref.ops]"/> states: |
| 16 | +</p> |
| 17 | +<blockquote style="border-left: 3px solid #ccc;padding-left: 15px;"> |
| 18 | +<p> |
| 19 | +Hardware could require an object referenced by an `atomic_ref` to have stricter alignment |
| 20 | +(<sref ref="[basic.align]"/>) than other objects of type `T`. Further, whether operations |
| 21 | +on an `atomic_ref` are lock-free could depend on the alignment of the referenced object. |
| 22 | +For example, lock-free operations on <tt>std::complex<double></tt> could be supported |
| 23 | +only if aligned to `2*alignof(double)`. |
| 24 | +</p> |
| 25 | +</blockquote> |
| 26 | +<p> |
| 27 | +By using the word "Further", the note misleadingly implies that `required_alignment` may |
| 28 | +need to be greater than `alignof(T)` even before considering lock freedom, i.e., that |
| 29 | +<tt>std::atomic_ref<T></tt> may be completely unimplementable on given hardware if |
| 30 | +the stricter alignment requirement is not met. However, that can never be true because |
| 31 | +falling back to a lock-based implementation is always possible. |
| 32 | +<p/> |
| 33 | +The note could also be misinterpreted to imply that even though an object may be aligned to |
| 34 | +`required_alignment` and thus referenceable by an `atomic_ref`, operations could still fail |
| 35 | +to be lock-free because there is a stricter alignment requirement that the object does not |
| 36 | +meet. Such an interpretation is, however, at odds with p4. |
| 37 | +<p/> |
| 38 | +The example given by the note is also confusing in that it does not necessarily demonstrate |
| 39 | +a situation in which <tt>std::atomic_ref<T>::required_alignment</tt> is greater than |
| 40 | +`alignof(T)`. |
| 41 | +<p/> |
| 42 | +In conclusion, this note appears to be a convoluted way of saying that, in order to ensure |
| 43 | +that operations on <tt>atomic_ref<T></tt> are lock-free, the implementation may |
| 44 | +define `required_alignment` to a value greater than `alignof(T)`. The note should be |
| 45 | +modified to say this much more clearly. |
| 46 | +</p> |
| 47 | +</discussion> |
| 48 | + |
| 49 | +<resolution> |
| 50 | +<p> |
| 51 | +This wording is relative to <paper num="N5014"/>. |
| 52 | +</p> |
| 53 | + |
| 54 | +<ol> |
| 55 | + |
| 56 | + |
| 57 | +<li><p>Modify <sref ref="[simd.expos.defn]"/> as indicated:</p> |
| 58 | + |
| 59 | +<blockquote> |
| 60 | +<pre> |
| 61 | +static constexpr size_t required_alignment; |
| 62 | +</pre> |
| 63 | +<blockquote> |
| 64 | +<p> |
| 65 | +-1- The alignment required for an object to be referenced by an atomic reference, which is at least |
| 66 | +`alignof(T)`. |
| 67 | +<p/> |
| 68 | +-2- [<i>Note 1</i>: <del>Hardware could require an object referenced by an `atomic_ref` to have stricter |
| 69 | +alignment (<sref ref="[basic.align]"/>) than other objects of type `T`. Further, whether operations |
| 70 | +on an `atomic_ref` are lock-free could depend on the alignment of the referenced object. For example, |
| 71 | +lock-free operations on <tt>std::complex<double></tt> could be supported only if aligned to |
| 72 | +`2*alignof(double)`</del><ins>An implementation can choose to define |
| 73 | +<tt>atomic_ref<T>::required_alignment</tt> to a value greater than `alignof(T)` in order to |
| 74 | +ensure that operations on all objects of type <tt>atomic_ref<T></tt> are lock-free</ins>. |
| 75 | +— <i>end note</i>] |
| 76 | +</p> |
| 77 | +</blockquote> |
| 78 | +</blockquote> |
| 79 | + |
| 80 | +</li> |
| 81 | + |
| 82 | +</ol> |
| 83 | +</resolution> |
| 84 | + |
| 85 | +</issue> |
0 commit comments