50 #ifndef __CAAtomic_h__
51 #define __CAAtomic_h__
57 #include <CoreFoundation/CFBase.h>
58 #include <libkern/OSAtomic.h>
61 inline void CAMemoryBarrier()
70 inline SInt32 CAAtomicAdd32Barrier(SInt32 theAmt,
volatile SInt32* theValue)
73 long lRetVal = InterlockedExchangeAdd((
volatile long*)theValue, theAmt);
77 return lRetVal + theAmt;
79 return OSAtomicAdd32Barrier(theAmt, (
volatile int32_t *)theValue);
83 inline SInt32 CAAtomicOr32Barrier(UInt32 theMask,
volatile UInt32* theValue)
88 long j = _InterlockedOr((
volatile long*)theValue, theMask);
91 return (SInt32)(j | theMask);
93 return OSAtomicOr32Barrier(theMask, (
volatile uint32_t *)theValue);
97 inline SInt32 CAAtomicAnd32Barrier(UInt32 theMask,
volatile UInt32* theValue)
102 long j = _InterlockedAnd((
volatile long*)theValue, theMask);
105 return (SInt32)(j & theMask);
107 return OSAtomicAnd32Barrier(theMask, (
volatile uint32_t *)theValue);
111 inline bool CAAtomicCompareAndSwap32Barrier(SInt32 oldValue, SInt32 newValue,
volatile SInt32 *theValue)
115 long lRetVal = InterlockedCompareExchange((
volatile long*)theValue, newValue, oldValue);
118 return (oldValue == lRetVal);
120 return OSAtomicCompareAndSwap32Barrier(oldValue, newValue, (
volatile int32_t *)theValue);
125 inline SInt32 CAAtomicIncrement32(
volatile SInt32* theValue)
128 return (SInt32)InterlockedIncrement((
volatile long*)theValue);
130 return OSAtomicIncrement32((
volatile int32_t *)theValue);
134 inline SInt32 CAAtomicDecrement32(
volatile SInt32* theValue)
137 return (SInt32)InterlockedDecrement((
volatile long*)theValue);
139 return OSAtomicDecrement32((
volatile int32_t *)theValue);
143 inline SInt32 CAAtomicIncrement32Barrier(
volatile SInt32* theValue)
146 return CAAtomicIncrement32(theValue);
148 return OSAtomicIncrement32Barrier((
volatile int32_t *)theValue);
152 inline SInt32 CAAtomicDecrement32Barrier(
volatile SInt32* theValue)
155 return CAAtomicDecrement32(theValue);
157 return OSAtomicDecrement32Barrier((
volatile int32_t *)theValue);
161 inline bool CAAtomicTestAndClearBarrier(
int bitToClear,
void* theAddress)
164 BOOL bOldVal = InterlockedBitTestAndReset((
long*)theAddress, bitToClear);
165 return (bOldVal ?
true :
false);
167 return OSAtomicTestAndClearBarrier(bitToClear, (
volatile void *)theAddress);
171 inline bool CAAtomicTestAndClear(
int bitToClear,
void* theAddress)
174 BOOL bOldVal = CAAtomicTestAndClearBarrier(bitToClear, (
long*)theAddress);
175 return (bOldVal ?
true :
false);
177 return OSAtomicTestAndClear(bitToClear, (
volatile void *)theAddress);
181 inline bool CAAtomicTestAndSetBarrier(
int bitToSet,
void* theAddress)
184 BOOL bOldVal = InterlockedBitTestAndSet((
long*)theAddress, bitToSet);
185 return (bOldVal ?
true :
false);
187 return OSAtomicTestAndSetBarrier(bitToSet, (
volatile void *)theAddress);
196 #if defined(__cplusplus) && defined(__MACTYPES__) && !__LP64__
197 inline int32_t CAAtomicAdd32Barrier(int32_t theAmt,
volatile int32_t* theValue)
199 return CAAtomicAdd32Barrier(theAmt, (
volatile SInt32 *)theValue);
202 inline int32_t CAAtomicOr32Barrier(uint32_t theMask,
volatile uint32_t* theValue)
204 return CAAtomicOr32Barrier(theMask, (
volatile UInt32 *)theValue);
207 inline int32_t CAAtomicAnd32Barrier(uint32_t theMask,
volatile uint32_t* theValue)
209 return CAAtomicAnd32Barrier(theMask, (
volatile UInt32 *)theValue);
212 inline bool CAAtomicCompareAndSwap32Barrier(int32_t oldValue, int32_t newValue,
volatile int32_t *theValue)
214 return CAAtomicCompareAndSwap32Barrier(oldValue, newValue, (
volatile SInt32 *)theValue);
217 inline int32_t CAAtomicIncrement32(
volatile int32_t* theValue)
219 return CAAtomicIncrement32((
volatile SInt32 *)theValue);
222 inline int32_t CAAtomicDecrement32(
volatile int32_t* theValue)
224 return CAAtomicDecrement32((
volatile SInt32 *)theValue);
227 inline int32_t CAAtomicIncrement32Barrier(
volatile int32_t* theValue)
229 return CAAtomicIncrement32Barrier((
volatile SInt32 *)theValue);
232 inline int32_t CAAtomicDecrement32Barrier(
volatile int32_t* theValue)
234 return CAAtomicDecrement32Barrier((
volatile SInt32 *)theValue);
236 #endif // __cplusplus && !__LP64__
239 inline bool CAAtomicCompareAndSwap64Barrier( int64_t __oldValue, int64_t __newValue,
volatile int64_t *__theValue )
241 return OSAtomicCompareAndSwap64Barrier(__oldValue, __newValue, __theValue );
251 #define CA_SPINLOCK_INIT 0
253 typedef int32_t CASpinLock;
255 bool CASpinLockTry(
volatile CASpinLock *__lock );
256 void CASpinLockLock(
volatile CASpinLock *__lock );
257 void CASpinLockUnlock(
volatile CASpinLock *__lock );
259 inline void CASpinLockLock(
volatile CASpinLock *__lock )
262 OSSpinLockLock(__lock);
264 while (CAAtomicTestAndSetBarrier(0, (
void*)__lock))
269 inline void CASpinLockUnlock(
volatile CASpinLock *__lock )
272 OSSpinLockUnlock(__lock);
274 CAAtomicTestAndClearBarrier(0, (
void*)__lock);
278 inline bool CASpinLockTry(
volatile CASpinLock *__lock )
281 return OSSpinLockTry(__lock);
283 return (CAAtomicTestAndSetBarrier(0, (
void*)__lock) == 0);
288 #endif // __CAAtomic_h__