[Bug nptl/16432] pthread_spin_unlock should imply a memory barrier

mikulas at artax dot karlin.mff.cuni.cz sourceware-bugzilla@sourceware.org
Wed Sep 24 20:02:00 GMT 2014


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.



More information about the Glibc-bugs mailing list