Re: NSLock optimizations
Re: NSLock optimizations
- Subject: Re: NSLock optimizations
- From: Nathan Day <email@hidden>
- Date: Sun, 25 Aug 2002 19:11:45 +0930
If all you want to do is change some value atomicly there are a set of
utility functions in DriverSynchronization.h and DriverServices.h which
work atomicly, they are defined for use as a part of the Driver Services
Library but can be used any where, there is some documentation for them
but I can't find them at the moment only some note I made of them.
Boolean CompareAndSwap( long oldValue, long newValue, long *Value );
SInt32 IncrementAtomic( SInt32 *value );
SInt32 DecrementAtomic( SInt32 *value );
SInt32 AddAtomic( SInt32 amount, SInt32 *value );
UInt32 BitAndAtomic( UInt32 mask, UInt32 *value );
UInt32 BitOrAtomic( UInt32 mask, UInt32 *value );
UInt32 BitXorAtomic( UInt32 mask, UInt32 *value );
SInt16 IncrementAtomic16( SInt16 * value );
SInt16 DecrementAtomic16( SInt16 * value );
SInt16 AddAtomic16( SInt32 amount, SInt16 * value );
UInt16 BitAndAtomic16( UInt32 mask, UInt16 * value );
UInt16 BitOrAtomic16( UInt32 mask, UInt16 * value );
UInt16 BitXorAtomic16( UInt32 mask, UInt16 * value );
SInt8 IncrementAtomic8( SInt8 * value );
SInt8 DecrementAtomic8( SInt8 * value );
SInt8 AddAtomic8( SInt32 amount, SInt8 *value );
UInt8 BitAndAtomic8( UInt32 mask, UInt8 *value );
UInt8 BitOrAtomic8( UInt32 mask, UInt8 *value );
UInt8 BitXorAtomic8( UInt32 mask, UInt8 *value );
Boolean TestAndSet( UInt32 bit, UInt8 *startAddress );
Boolean TestAndClear( UInt32 bit, UInt8 *startAddress );
On Sunday, August 25, 2002, at 05:31 PM, Steve Gehrman wrote:
I have an object that lazily sets it's variables. For example:
- (BOOL)isPhucked;
{
if (isPhucked == -1)
{
isPhucked = (blah == foo) ? 0:1;
}
return (isPhucked == 1);
}
I want this to be thread safe, and I don't want to call lock, unlock
once the variable has been set, so I tried something like:
- (BOOL)isPhucked;
{
if (isPhucked == -1)
{
[myLock lock];
// must check again since lock could have been released by
another thread that initialized isPhucked
if (isPhucked == -1)
{
isPhucked = 0;
if (blah)
isPhucked = 1;
}
[myLock unlock];
}
return (isPhucked == 1);
}
I have discovered that this is not thread safe since the first line
check of isPhucked is not protected by the lock. The call from a
second thread could check isPhucked == -1 after the first thread sets
it to 0, before the if (blah) isPhucked = 1; is executed.
My question is: Is there a way to structure this code so it is fully
thread safe. The thing I'm trying to avoid is constantly calling lock
and unlock every time this function is called once the variable has
been successfully initialized. Is calling lock and unlock slow? Maybe
the answer is to just call lock and unlock everytime?
Nathan Day
http://homepage.mac.com/nathan_day/
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.