[Bug c/98965] assignment to a struct with an atomic member not atomic

2021-02-04 Thread msebor at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98965

--- Comment #5 from Martin Sebor  ---
I was thinking of the latter, i.e., splitting the assignment into a series of
ordinary and atomic, the latter per member.  The union case would be diagnosed
(it too should be straightforward to detect).

But C++ rejects such mixed assignments, so the easiest solution would be to do
the same in C (or at least warn).

[Bug c/98965] assignment to a struct with an atomic member not atomic

2021-02-04 Thread joseph at codesourcery dot com via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98965

--- Comment #4 from joseph at codesourcery dot com  ---
The difficulty with making such an assignment atomic is that atomic 
operations for different sizes of atomic access don't interoperate on the 
same memory; if the struct contains an _Atomic int, that's accessed 
atomically via appropriate int-sized atomic operations, while a larger 
structure may use locking for atomic access in libatomic, and if both 
mechanisms are used on the same memory the result doesn't properly follow 
the memory model.

So you'd need to split struct assignment up into assignment of members to 
access a member atomically.  But that couldn't work at all when you have a 
union with atomic members, because in the union case the compiler can't 
know which is the currently active member that would determine the size of 
the atomic access.

[Bug c/98965] assignment to a struct with an atomic member not atomic

2021-02-04 Thread msebor at gcc dot gnu.org via Gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98965

Martin Sebor  changed:

   What|Removed |Added

   Keywords||diagnostic

--- Comment #3 from Martin Sebor  ---
The topic came up in a discussion on the WG14 reflector.  For waht it's worth,
my impression is that it's simply an oversight, a result of an incomplete
integration of atomics into the core language.  It would be hard to justify
making this (deliberately) undefined.  The member type information is readily
available at the point of the assignment so if it's not meant to be supported
the standard could easily require a diagnostic without imposing a great burden
on implementations.

Until or unless the standard is clarified, unless we choose to implement the
assignment atomically, issuing a warning would help prevent bugs.

As a data point, of the compilers on Godbolt only Clang and GCC support _Atomic
structs and neither locks the member.