From: "Jan Beulich" Subject: Re: [PATCH v2 2/3] xen/atomic: Implement atomic_{inc, dec}_bounded() Date: Wed, 02 Jul 2014 15:05:39 +0100 Message-ID: <53B42DD3020000780001F977@mail.emea.novell.com> References: <1404308871-23368-1-git-send-email-andrew.cooper3@citrix.com> <1404308871-23368-3-git-send-email-andrew.cooper3@citrix.com> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Return-path: In-Reply-To: <1404308871-23368-3-git-send-email-andrew.cooper3@citrix.com> Content-Disposition: inline List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org To: Andrew Cooper Cc: Keir Fraser , Xen-devel List-Id: xen-devel@lists.xenproject.org >>> On 02.07.14 at 15:47, wrote: > These will increment/decremented an atomic_t, while ensuring that the > atomic_t > doesn't hit a specific bound. > > This involves introducing atomic_cmpxchg() for x86, which previously only > existed on ARM. > > Suggested-by: Don Slutz > Signed-off-by: Andrew Cooper > CC: Keir Fraser > CC: Jan Beulich You should be getting used to Cc all "REST" maintainers on respective changes. > --- /dev/null > +++ b/xen/common/atomic.c > @@ -0,0 +1,35 @@ > +#include > + > +bool_t atomic_inc_bounded(atomic_t *v, int bound) > +{ > + int old, new, prev = atomic_read(v); > + > + do > + { > + old = prev; > + new = old + 1; > + if ( new >= bound ) > + return 0; > + > + prev = atomic_cmpxchg(v, old, new); > + } while ( prev != old ); > + > + return 1; > +} > + > +bool_t atomic_dec_bounded(atomic_t *v, int bound) > +{ > + int old, new, prev = atomic_read(v); > + > + do > + { > + old = prev; > + new = old - 1; > + if ( new <= bound ) > + return 0; > + > + prev = atomic_cmpxchg(v, old, new); > + } while ( prev != old ); > + > + return 1; > +} Same question here: Do we really need these? There are various uses of cmpxchg() in common code already, and in patch 3 you don't really need the cmpxchg to happen on an atomic_t, so plain cmpxchg() with the little bit of extra logic open coded would seem fine to me. Jan