Re: [patch] x86: atomic64_t: Improve atomic64_add_return()

From: Linus Torvalds
Date: Fri Jul 03 2009 - 13:38:42 EST




On Fri, 3 Jul 2009, Ingo Molnar wrote:
> u64 atomic64_add_return(u64 delta, atomic64_t *ptr)
> {
> + /*
> + * Try first with a (probably incorrect) assumption about
> + * what we have there. We'll do two loops most likely,
> + * but we'll get an ownership MESI transaction straight away
> + * instead of a read transaction followed by a
> + * flush-for-ownership transaction:
> + */
> + u64 old_val, new_val, real_val = 1ULL << 32;
>
> do {
> + old_val = real_val;
> new_val = old_val + delta;
>
> + real_val = atomic64_cmpxchg(ptr, old_val, new_val);
> +
> + } while (real_val != old_val);

For the case where we actually have to keep looping until we succeed,
we're probably better off starting with a good guess rather than a bad
one.

So I'd suggest using 'real_val = 0', which gives smaller code, rather than
the 1ull << 32 that is actively trying to be a bad guess.

It won't matter all that much (since 0 really isn't a much better guess
than 1ull<<32), but I'd rather have a simple constant that doesn't matter,
over an odd constant that makes no sense.

Linus
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/