This is the mail archive of the
glibc-bugs@sourceware.org
mailing list for the glibc project.
[Bug nptl/16432] pthread_spin_unlock should imply a memory barrier
- From: "mikulas at artax dot karlin.mff.cuni.cz" <sourceware-bugzilla at sourceware dot org>
- To: glibc-bugs at sourceware dot org
- Date: Wed, 24 Sep 2014 20:01:59 +0000
- Subject: [Bug nptl/16432] pthread_spin_unlock should imply a memory barrier
- Auto-submitted: auto-generated
- References: <bug-16432-131 at http dot sourceware dot org/bugzilla/>
https://sourceware.org/bugzilla/show_bug.cgi?id=16432
--- Comment #4 from Mikulas Patocka <mikulas at artax dot karlin.mff.cuni.cz> ---
Suppose that I need to create some structure using non-atomic accesses, then
execute a memory barrier and then publish pointer to the structure using atomic
operation. Something like this:
#include <stdatomic.h>
#include <stdlib.h>
struct s {
int a;
int b;
int c;
int d;
struct something1 *e;
struct something2 *f;
};
struct s * _Atomic ptr = NULL;
void fn(struct something1 *q1, struct something2 *q2)
{
struct s *s = malloc(sizeof(struct s));
s->a = 1;
s->b = 2;
s->c = 3;
s->d = 4;
s->e = q1;
s->f = q2;
atomic_store_explicit(&ptr, s, __ATOMIC_SEQ_CST);
}
According to the C11 standard this program is wrong. If I wanted to write the
program correctly according to the standard, then every member of the structure
s would have to be _Atomic. Furthermore, every member of structures something1
and something2 would have to be _Atomic. Formally speaking, every variable
reachable via pointers from the structure s would have to be declared with the
_Atomic prefix.
So, the standard is really botched and unusable for this purpose :-(
--
You are receiving this mail because:
You are on the CC list for the bug.