From owner-freebsd-questions Tue Mar 18 08:43:58 1997 Return-Path: Received: (from root@localhost) by freefall.freebsd.org (8.8.5/8.8.5) id IAA06496 for questions-outgoing; Tue, 18 Mar 1997 08:43:58 -0800 (PST) Received: from who.cdrom.com (who.cdrom.com [204.216.27.3]) by freefall.freebsd.org (8.8.5/8.8.5) with ESMTP id IAA06465 for ; Tue, 18 Mar 1997 08:43:52 -0800 (PST) Received: from hda.hda.com (ip84-max1-fitch.ziplink.net [199.232.245.84]) by who.cdrom.com (8.8.5/8.6.11) with SMTP id DAA28537 for ; Tue, 18 Mar 1997 03:35:17 -0800 (PST) Received: (from dufault@localhost) by hda.hda.com (8.6.12/8.6.12) id EAA21402; Tue, 18 Mar 1997 04:46:48 -0500 From: Peter Dufault Message-Id: <199703180946.EAA21402@hda.hda.com> Subject: Re: test and set instruction: how? In-Reply-To: <199703172327.AAA01251@plm.xs4all.nl> from Peter Mutsaers at "Mar 18, 97 00:27:27 am" To: plm@xs4all.nl (Peter Mutsaers) Date: Tue, 18 Mar 1997 04:46:47 -0500 (EST) Cc: freebsd-questions@FreeBSD.ORG X-Mailer: ELM [version 2.4ME+ PL25 (25)] MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Sender: owner-questions@FreeBSD.ORG X-Loop: FreeBSD.org Precedence: bulk > Is there a (portable ?) way for a test-and-set instruction under > FreeBSD? (...) Compare and exchange is present on 486s and Pentiums. Early 486s have a different op code that collides with other instructions; Pentiums support 64 bit flavors. Here is what I've been using. It will compile with CAS_TEST. /* cas: Compare-and-swap using the 486 and Pentium "cmpxchg" * instruction. * * Atomically change what "ptr" points to to "n" if it is "o" * and return success. */ #if defined(CAS_TEST) typedef unsigned long castype; extern int cas(volatile castype *, const castype, const castype); #else #include "cas.h" #endif #if defined(CAS_DEBUG) #include #endif int cas(volatile castype *ptr, const castype o, const castype n) { volatile int result = 0; #if defined(CAS_DEBUG) fprintf(stderr, "cas %p %08lx %08lx %08lx, ", ptr, *ptr, o, n); #endif __asm __volatile( "cmpxchg%L3 %3,%1; jne 0f; inc%L0 %0; 0:" : "=m"(result) : "m"(*ptr), "a"(o), "r"(n) ); #if defined(CAS_DEBUG) fprintf(stderr, "%s %08lx\n", result ? "OK " : "Fail", *ptr); #endif return result; } #if defined(CAS_TEST) #include int main(int argc, char *av[]) { while (1) { volatile castype c; castype o, n; int result; printf("c old new > "); if (scanf("%ld %ld %ld", &c, &o, &n) != 3) break; result = cas(&c, o, n); printf("%d; %ld %ld %ld\n", result, c, o, n); } return 0; } #endif /* CAS_TEST */ -- Peter Dufault (dufault@hda.com) Realtime Machine Control and Simulation HD Associates, Inc. Voice: 508 433 6936