Re: [RFC PATCH 6/8] READ_ONCE: Drop pointer qualifiers when reading from scalar types

From: Linus Torvalds
Date: Mon Jan 13 2020 - 14:32:19 EST


On Mon, Jan 13, 2020 at 7:00 AM Will Deacon <will@xxxxxxxxxx> wrote:
>
> I can't disagree with that, but the only option we've come up with so far
> that solves this in the READ_ONCE() macro itself is the thing from PeterZ:
>
> // Insert big fat comment here
> #define unqual_typeof(x) typeof(({_Atomic typeof(x) ___x __maybe_unused; ___x; }))

I'm with Luc on this - that not only looks gcc-specific, it looks
fragile too, in that it's not obvious that "_Atomic typeof(x)" really
is guaranteed to do what we want.

> So I suppose my question is: how ill does this code really make you feel?

I wish the code was more obvious.

One way to do that might be to do your approach, but just write it as
a series of macros that makes it a bit more understandable what it
does.

Maybe it's just because of a "pee in the snow" effect, but I think
this is easier to explain:

#define __pick_scalar_type(x,type,otherwise) \
__builtin_choose_expr(__same_type(x,type), (type)0, otherwise)

#define __pick_integer_type(x, type, otherwise) \
__pick_scalar_type(x, unsigned type, \
__pick_scalar_type(x, signed type, otherwise))

#define __unqual_scalar_typeof(x) typeof( \
__pick_integer_type(x, char, \
__pick_integer_type(x, short, \
__pick_integer_type(x, int, \
__pick_integer_type(x, long, \
__pick_integer_type(x, long long, x))))))

just because you there's less repeated noise, and the repetition there
is is simpler.

So still "Eww", but maybe not quite _as_ "Eww".

Linus