From owner-freebsd-hackers Wed Jan 31 17:27:53 2001 Delivered-To: freebsd-hackers@freebsd.org Received: from meow.osd.bsdi.com (meow.osd.bsdi.com [204.216.28.88]) by hub.freebsd.org (Postfix) with ESMTP id 6622237B6A7 for ; Wed, 31 Jan 2001 17:27:35 -0800 (PST) Received: from laptop.baldwin.cx (john@jhb-laptop.osd.bsdi.com [204.216.28.241]) by meow.osd.bsdi.com (8.11.1/8.9.3) with ESMTP id f111LZ327051; Wed, 31 Jan 2001 17:21:35 -0800 (PST) (envelope-from jhb@FreeBSD.org) Message-ID: X-Mailer: XFMail 1.4.0 on FreeBSD X-Priority: 3 (Normal) Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 8bit MIME-Version: 1.0 In-Reply-To: <010501c08bec$d48c96a0$1200a8c0@gsicomp.on.ca> Date: Wed, 31 Jan 2001 17:27:05 -0800 (PST) From: John Baldwin To: Matthew Emmerton Subject: Re: Atomic bit operations Cc: freebsd-hackers@FreeBSD.org Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG On 01-Feb-01 Matthew Emmerton wrote: >> 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? If you don't want to patch the code, then you can use a local wrapper in your code in the form of a suitable macro. We already have one atomic API, we don't need to maintain 2. :) >> > 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. This stuff likely won't be backported as it is part of the rather largish SMPng work going on in -current. In 4.x, you can use spl()'s in the kernel to protect data structures against being written to via interrupts. You don't have to worry about SMP protection as the kernel spin lock will protect you against that. > Thanks, > > -- > Matt Emmerton -- John Baldwin -- http://www.FreeBSD.org/~jhb/ PGP Key: http://www.baldwin.cx/~john/pgpkey.asc "Power Users Use the Power to Serve!" - http://www.FreeBSD.org/ To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-hackers" in the body of the message