__sync_bool_compare_and_swap and structures
Andrew Haley
aph@redhat.com
Thu Oct 16 13:15:00 GMT 2008
Robert Lemmen wrote:
> hi everyone,
>
> i have written a couple of algorithms using the GCC builtin CAS, which
> all works very nicely. it is a bit ugly and full of casts, and i am
> trying to clean it up. i stumbled over something that i can't really
> resolve however, and was wondering how other people do that:
>
>
> // we are on 32bit, so this structure is 64 bits, just like a long long
> struct gptr {
> void *ptr;
> int gen;
> };
>
> int main(int argc, char *argv[]) {
> long long a, b;
> long long *A = &a;
>
> // works as expected
> while (!__sync_bool_compare_and_swap(A, a, b)) {};
>
> struct gptr c, d;
> struct gptr *C = &c;
>
> // incompatible type for argument 1 of `__sync_bool_compare_and_swap'
> while (!__sync_bool_compare_and_swap(C, c, d)) {};
>
> // works but is really ugly
> while (!__sync_bool_compare_and_swap((long long*)C,
> *(long long*)&c, *(long long*)&d)) {};
>
> return 0;
> }
>
> as you can see i can use CAS on 8 byte primitives (long long int) quite
> well, but if i use an equal sized struct instead i get an error. i can
> of course cast it as shown above, but i was hoping that i just miss
> something and that it can be done cleanly.
>
> how are you guys doing that?
Use a union of an 8 byte struct and a long long.
Andrew.
More information about the Gcc-help
mailing list