atomic_compare_exchange
: What does it mean to say two structs compare equal?This issue has been automatically converted from the original issue lists and some formatting may not have been preserved.
Authors: WG14, Douglas Walls
Date: 2013-02-21
Reference document: N1675
Submitted against: C11 / C17
Status: Fixed
Fixed in: C17
Cross-references: 0423, 0474
Converted from: n2396.htm
7.17.7.4 The atomic_compare_exchange generic functions
7.17.7.4p2 Description
Atomically, compares the value pointed to by object for equality with
that in expected, and if true, replaces the value pointed to by object
with desired, and if false, updates the value in expected with the
value pointed to by object.
When object is an atomic struct type and expected is the corresponding
non-atomic struct type. What does it mean to compare two struct types
as equal?
Where does the C standard define what it means for two objects of struct
type to be equal?
7.17.7.4 NOTE 1 gives an example using memcmp on how the test for
equality might be defined. But that is non-normative.
But the padding bytes in a struct have unspecified values (6.2.6.1p6)
7.24.4.1 The memcmp function, footnote 310 reminds us that the contents
of padding in a struct is indeterminate.
Even integers can have padding bits, whose values are unspecified (6.2.6.2p1)
A similar issue probably occurs for Atomic union types.
Either define equality of objects of struct type, add a restriction disallowing
use of atomic structs as arguments for the atomic_compare_exchange generic
functions,
or note that atomic_compare_exchange generic functions for objects of atomic
struct type results in undefined behavior.
Comment from WG14 on 2017-11-03:
Apr 2013 meeting
Oct 2013 meeting
memcmp
and memcpy
(with suitable synchronization) be a common implementation for all _Atomic objects.atomic_compare_exchange
compared to what might be possible to implement in common for all types. This seems to imply that an implementation must supply something akin to a type generic implementation of atomic_compare_exchange
. Type generic macros as applied to _Atomic is the subject of DR423.Apr 2014 meeting
Oct 2014 meeting
As requested, the paper N1864 was written and provided. From our C++ liaison, however, it was learned that corresponding behavior is well defined and is in use. Further investigation revealed that
atomic_compare_exchange
in C++ is and has been explicitly defined to be that of bit comparison. C11 defines it as value comparison.It was noted that bit comparison for atomic bool would not give the expected answer if differing non-zero "true" values were compared. It was also noted that bit comparison exposes padding bits, whereas lock bits would be required to be discarded, leading to code that might work on one implementation of an architecture but fail on another.
A new paper was solicited.
Apr 2015 meeting
The paper N1906 was provided and discussed and its Proposed Technical Corrigendum was adopted. This resolution clarifies that
atomic_compare_exchange
is now aligned with C++11 as operating on bit representations. Where these representations are unpadded integer or structure values, the operation is well defined. The typebool
is padded in many implementations.
In 7.17.7.4p2 replace
Atomically, compares the value pointed to by object for equality with that in expected, and if true, replaces the value pointed to by object with desired, and if false, updates the value in expected with the value pointed to by object
with:
Atomically, compares the contents of the memory pointed to by object for equality with that in expected, and if true, replaces the contents of the memory pointed to by object with desired, and if false, updates the value in expected with the value pointed to by object.