Re: [PATCH] Documentation/atomic_t: Document cmpxchg() vs try_cmpxchg()

From: Peter Zijlstra
Date: Mon Jul 05 2021 - 13:12:37 EST


On Mon, Jul 05, 2021 at 11:25:29PM +0800, Xu, Yanfei wrote:
> > +CMPXHG vs TRY_CMPXCHG
>
> CMPXHG -> CMPXCHG

Yeah, already fixed. I spotted it a minute after sending :/

> > +---------------------
> > +
> > + int atomic_cmpxchg(atomic_t *ptr, int old, int new);
> > + bool atomic_try_cmpxchg(atomic_t *ptr, int *oldp, int new);
> > +
> > +Both provide the same functionality, but try_cmpxchg() can lead to more
> > +compact code. The functions relate like:
> > +
> > + bool atomic_try_cmpxchg(atomic_t *ptr, int *oldp, int new)
> > + {
> > + int ret, old = *oldp;
> > + ret = atomic_cmpxchg(ptr, old, new);
> > + if (ret != old)
> > + *oldp = ret;
> > + return ret == old;
> > + }
>
> I tried to search some comments about atomic_try_cmpxchg(), but failed.
> Maybe I missed it. With your this document, it is more clear now.

OK, good, thanks!

> > +
> > +and:
> > +
> > + int atomic_cmpxchg(atomic_t *ptr, int old, int new)
> > + {
> > + (void)atomic_try_cmpxchg(ptr, &old, new);
> > + return old;
> > + }
> > +
> > +Usage:
> > +
> > + old = atomic_read(&v); old = atomic_read(&v);
> > + for (;;) { do {
> > + new = func(old); new = func(old);
> > + tmp = atomic_cmpxchg(&v, old, new); } while (!atomic_try_cmpxchg(&v, &old, new));
>
> Some unnecessary spaces before "while".

That's due to the diff prepending the line with "+ " which offsets the
tabstop. If you apply the patch the actual document is fine.