Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 25 Mar 2003 21:37:37 -0500
From:      Craig Rodrigues <rodrigc@attbi.com>
To:        John Baldwin <jhb@FreeBSD.org>
Cc:        freebsd-smp@FreeBSD.org
Subject:   Re: atomic_dec_and_test() in FreeBSD?
Message-ID:  <20030326023737.GA85101@attbi.com>
In-Reply-To: <XFMail.20030324114810.jhb@FreeBSD.org>
References:  <20030323074119.GA43849@attbi.com> <XFMail.20030324114810.jhb@FreeBSD.org>

next in thread | previous in thread | raw e-mail | index | archive | help
On Mon, Mar 24, 2003 at 11:48:10AM -0500, John Baldwin wrote:
> > http://kernelnewbies.org/documents/kdoc/kernel-api/r287.html
> 
> If you strictly need decrement and test, you can use a while loop
> with atomic_cmpset(), e.g.
> 
>         do {
>                 x = foo;
>         } while (atomic_cmpset_int(&foo, x, x - 1) == 0);
>         if (x == 1)
>                 /* foo just dropped to zero */
> 
> What task are you trying to accomplish though?


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?

Thanks.

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

To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-smp" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20030326023737.GA85101>