atomic_dec_and_test() in FreeBSD?

Craig Rodrigues rodrigc at attbi.com
Fri Apr 4 07:05:00 PST 2003


On Wed, Mar 26, 2003 at 12:13:21PM -0500, John Baldwin wrote:
> > I am the port maintainer of the Apache Portable Runtime (apr) library.
> > apr has some atomic functions.  For FreeBSD, this is what is defined:
> > 
> > /**
> >  * decrement the atomic variable by 1
> >  * @param mem pointer to the atomic value
> >  * @return zero if the value is zero, otherwise non-zero
> >  */
> > int apr_atomic_dec(volatile apr_atomic_t *mem);
> > 
> > [snip]
> > 
> >#define apr_atomic_dec(mem)          atomic_subtract_int(mem,1)
> > 
> > 
> > This is obviously quite wrong.
> > 
> > So are you saying that I should replace this with:
> > 
> > int apr_atomic_dec(volatile apr_atomic_t *mem);
> > 
> > int  apr_atomic_dec(volatile apr_atomic_t *mem){
> >          apr_atomic_t x
> >          do {
> >                  x = *mem;
> >          } while (atomic_cmpset_int(mem, x, x - 1) == 0);
> >          if (x == 1)
> >                  /* foo just dropped to zero */
> > 
> > 
> >          ???????
> > } 
> > 
> > Can you give more guidance?
> 
> You could do this:
> 
>         apr_atomic_t x;
> 
>         do {
>                 x = *mem;
>         } while (atomic_cmpset_int(mem, x, x - 1) == 0);
>         return (x - 1)



This macro exists on -CURRENT, but I can't seem to find it
on FreeBSD 4.7.  What is the equivalent solution on that platform?

Thanks.
-- 
Craig Rodrigues        
http://home.attbi.com/~rodrigc
rodrigc at attbi.com


More information about the freebsd-smp mailing list