From owner-freebsd-smp Tue Mar 25 18:49:34 2003 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 71F2F37B481; Tue, 25 Mar 2003 18:49:14 -0800 (PST) Received: from h00609772adf0.ne.client2.attbi.com (h00609772adf0.ne.client2.attbi.com [24.61.43.152]) by mx1.FreeBSD.org (Postfix) with ESMTP id 2D98F440AA; Tue, 25 Mar 2003 18:37:31 -0800 (PST) (envelope-from rodrigc@attbi.com) Received: from h00609772adf0.ne.client2.attbi.com (localhost.ne.attbi.com [127.0.0.1]) by h00609772adf0.ne.client2.attbi.com (8.12.8/8.12.7) with ESMTP id h2Q2bdri085168; Tue, 25 Mar 2003 21:37:39 -0500 (EST) (envelope-from rodrigc@h00609772adf0.ne.client2.attbi.com) Received: (from rodrigc@localhost) by h00609772adf0.ne.client2.attbi.com (8.12.8/8.12.7/Submit) id h2Q2bbRf085167; Tue, 25 Mar 2003 21:37:37 -0500 (EST) Date: Tue, 25 Mar 2003 21:37:37 -0500 From: Craig Rodrigues To: John Baldwin Cc: freebsd-smp@FreeBSD.org Subject: Re: atomic_dec_and_test() in FreeBSD? Message-ID: <20030326023737.GA85101@attbi.com> References: <20030323074119.GA43849@attbi.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.4i X-Spam-Status: No, hits=-32.5 required=5.0 tests=EMAIL_ATTRIBUTION,IN_REP_TO,QUOTED_EMAIL_TEXT,REFERENCES, REPLY_WITH_QUOTES,USER_AGENT_MUTT autolearn=ham version=2.50 X-Spam-Level: X-Spam-Checker-Version: SpamAssassin 2.50 (1.173-2003-02-20-exp) Sender: owner-freebsd-smp@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.org 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