ZFS committed to the FreeBSD base.

Robert Watson rwatson at FreeBSD.org
Fri Apr 13 14:47:53 UTC 2007

On Fri, 13 Apr 2007, Craig Boston wrote:

> On Fri, Apr 13, 2007 at 05:34:56PM +1000, Bruce Evans wrote:
>> Doesn't everyone who uses atomic operations knows that they are expensive? 
>> :)
> Yes, though hopefully they should at least be faster than using a mutex, 
> though for cmpxchg8b it sounds like that may not necessarily be the case...

A common example of this not being the case is statistics updates: it doesn't 
take too many statistics being updated at once before it makes more sense to 
use a mutex than individual atomic instructions, as mutex lock and unlock, in 
the uncontended case, involve an atomic instruction each (with memory 
barriers).  Then it becomes more semantic: is using non-blocking primitives 
preferable, or are there consistency requirements between "atomically" updated 
fields?  If contention never happens, then maybe you get consistency for free 
by using a mutex.

As a general rule, unless it's a very clear-cut case (a simple counter), I 
would encourage people to program with mutexes rather than directly with 
atomic instructions.  It prevents them from having to deal with really weird 
stuff that happens with weaker memory consistency.

Robert N M Watson
Computer Laboratory
University of Cambridge

More information about the freebsd-current mailing list