From owner-freebsd-hackers Wed Jan 31 17:11:45 2001 Delivered-To: freebsd-hackers@freebsd.org Received: from xena.gsicomp.on.ca (cr677933-a.ktchnr1.on.wave.home.com [24.43.230.149]) by hub.freebsd.org (Postfix) with ESMTP id 9DBA237B6A1; Wed, 31 Jan 2001 17:11:25 -0800 (PST) Received: from hermes (hermes.gsicomp.on.ca [192.168.0.18]) by xena.gsicomp.on.ca (8.11.1/8.9.3) with SMTP id f1119bi11369; Wed, 31 Jan 2001 20:09:38 -0500 (EST) (envelope-from matt@gsicomp.on.ca) Message-ID: <010501c08bec$d48c96a0$1200a8c0@gsicomp.on.ca> From: "Matthew Emmerton" To: "John Baldwin" Cc: , "John Baldwin" References: Subject: Re: Atomic bit operations Date: Wed, 31 Jan 2001 20:18:04 -0500 MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 5.50.4133.2400 X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4133.2400 Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG > On 01-Feb-01 Matthew Emmerton wrote: > >> On 31-Jan-01 Matthew Emmerton wrote: > >> > Hi all, > >> > > >> > I've taken a look around for an implementation of atomic bit operations > > in > >> > FreeBSD (similar to Linux' asm/bitopt.h, which include clear_bit() and > >> > test_and_set_bit()) but haven't found any. The only thing I've found > > are > >> > the atomic clear/set/add/sub routines in machine/atomic.h. > >> > > >> > Do we have an implementation of atomic bit operations, and if we don't, > >> > would we like some? > >> > >> Erm, atomic_set() sets's bits, and atomic_clear() clear's bits. Anything > > else > >> you might need can be done with atomic_cmpset() anyways. > > > > Where are these functions implemented? /usr/include/machine/atomic.h just > > has atomic_set_XXX and clear_XXX primitives, which work on char/short/longs, > > but not individual bits. > > 1) You need to look in -current. I only recently moved to 4-stable, so I'm not going to jump to -current quite yet :) > 2) atomic_set_int(&my_int, 4); sets bit _2_ in the integer variable my_int. > Make sense? You can't address individual bits on a machine. :-P > > > I presume that I could wrap the char operations with something that takes > > 0x01 and bit-shifts it appropriately so that atomic_set/clear could be used. > > Hmm, so you mean the API is atomic_set(&foo, x) implies foo |= 1 << x? > That means you can't set more than 1 bit atomically, which is a bit > limiting. Yes, but it's the API used by some code I recently inherited. (Recall that this API is Linux's asm/bitops.h) I realize that the existing atomic_xxx_xxx functions are much more flexible than bit-based ones, but wouldn't it make sense to have the full complement of functions available? > > However, certain other primitives are missing, such as an atomic > > test-and-set operation. Under the current scheme this would have to be done > > by two independent operations, which is not useful when atomicitiy is > > required. > > do { > x = my_int; > while (atomic_cmpset_int(&my_int, x, x | foo) == 0); > if (x & foo) > foo_was_already_set; > else > foo_was_not_already_set; I had based my assumptions on what I saw in 4-stable, which doesn't have atomic_cmpset. I'll have to take a peek at 4-current or wait until some features trickle down to 4-stable. Thanks, -- Matt Emmerton To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-hackers" in the body of the message