__sync_bool_compare_and_swap and structures

Robert Lemmen robertle@semistable.com
Thu Oct 16 12:46:00 GMT 2008


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?

cu  robert

-- 
Robert Lemmen                               http://www.semistable.com 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
URL: <https://gcc.gnu.org/pipermail/gcc-help/attachments/20081016/8b7b56d6/attachment.sig>


More information about the Gcc-help mailing list