From owner-freebsd-smp@FreeBSD.ORG Wed Mar 26 09:13:25 2003 Return-Path: Delivered-To: freebsd-smp@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 520C137B404 for ; Wed, 26 Mar 2003 09:13:25 -0800 (PST) Received: from mail.speakeasy.net (mail16.speakeasy.net [216.254.0.216]) by mx1.FreeBSD.org (Postfix) with ESMTP id 9698B43F93 for ; Wed, 26 Mar 2003 09:13:24 -0800 (PST) (envelope-from jhb@FreeBSD.org) Received: (qmail 18407 invoked from network); 26 Mar 2003 17:13:28 -0000 Received: from unknown (HELO server.baldwin.cx) ([216.27.160.63]) (envelope-sender )encrypted SMTP for ; 26 Mar 2003 17:13:28 -0000 Received: from laptop.baldwin.cx (gw1.twc.weather.com [216.133.140.1]) by server.baldwin.cx (8.12.8/8.12.8) with ESMTP id h2QHDMOv099392; Wed, 26 Mar 2003 12:13:22 -0500 (EST) (envelope-from jhb@FreeBSD.org) Message-ID: X-Mailer: XFMail 1.5.4 on FreeBSD X-Priority: 3 (Normal) Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 8bit MIME-Version: 1.0 In-Reply-To: <20030326023737.GA85101@attbi.com> Date: Wed, 26 Mar 2003 12:13:21 -0500 (EST) From: John Baldwin To: Craig Rodrigues X-Spam-Status: No, hits=-19.5 required=5.0 tests=EMAIL_ATTRIBUTION,IN_REP_TO,QUOTED_EMAIL_TEXT, REPLY_WITH_QUOTES autolearn=ham version=2.50 X-Spam-Level: X-Spam-Checker-Version: SpamAssassin 2.50 (1.173-2003-02-20-exp) cc: freebsd-smp@FreeBSD.org Subject: Re: atomic_dec_and_test() in FreeBSD? X-BeenThere: freebsd-smp@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: FreeBSD SMP implementation group List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 26 Mar 2003 17:13:26 -0000 On 26-Mar-2003 Craig Rodrigues wrote: > 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? You could do this: apr_atomic_t x; do { x = *mem; } while (atomic_cmpset_int(mem, x, x - 1) == 0); return (x - 1) -- John Baldwin <>< http://www.FreeBSD.org/~jhb/ "Power Users Use the Power to Serve!" - http://www.FreeBSD.org/