Page 1 of 1

Article 4.1: Atomic Operations

Posted: Wed Sep 15, 2004 12:56 am
by Nat!
Discusssion thread for this specific optimization article.

Posted: Wed Jan 26, 2005 11:55 am
by phip
After reading this article, I wondered if/why didn't Apple provide atomic functions. After some digging, I found them. They are located in CoreServices.framework, in a header called DriverSynchronization.h. They are all listed as "Available Mac OS X in version 10.0 and later"

What's the verdict on these functions? Probably not as fast as the roll-your-own versions, since they aren't inlined and would imply a dyld_stub call, right? On the other hand, Apple may have a few tricks to make it safer. Hmm...

Here are the 32 bit prototypes. There are also 8 and 16 bit variations, though mask and amount are always 32.

Code: Select all

extern SInt32 IncrementAtomic(SInt32 * value);
extern SInt32 DecrementAtomic(SInt32 * value);
extern SInt32 AddAtomic(SInt32 amount, SInt32 * value);
extern UInt32 BitAndAtomic(UInt32 mask, UInt32 * value);
extern UInt32 BitOrAtomic(UInt32 mask, UInt32 * value);
extern UInt32 BitXorAtomic(UInt32 mask, UInt32 * value);

Posted: Thu Jan 27, 2005 12:13 pm
by Nat!
phip wrote:What's the verdict on these functions? Probably not as fast as the roll-your-own versions, since they aren't inlined and would imply a dyld_stub call, right?


That'd be my opinion too. Faster than using a lock, probably quite a bit, because there is no need <b>sync</b>, but quite a bit slower than using the inlined method.

My gut feeling is, that if you choose atomic increment/decrement over locking, you will be calling it that often, that inlining will seem very tempting.