OSAtomicOperations.c [plain text]
#include <libkern/OSAtomic.h>
enum {
false = 0,
true = 1
};
#define NULL 0L
#ifndef __ppc__
SInt32 OSAddAtomic(SInt32 amount, SInt32 * value)
{
SInt32 oldValue;
SInt32 newValue;
do {
oldValue = *value;
newValue = oldValue + amount;
} while (! OSCompareAndSwap((UInt32) oldValue, (UInt32) newValue, (UInt32 *) value));
return oldValue;
}
SInt32 OSIncrementAtomic(SInt32 * value)
{
return OSAddAtomic(1, value);
}
SInt32 OSDecrementAtomic(SInt32 * value)
{
return OSAddAtomic(-1, value);
}
#endif
static UInt32 OSBitwiseAtomic(UInt32 and_mask, UInt32 or_mask, UInt32 xor_mask, UInt32 * value)
{
UInt32 oldValue;
UInt32 newValue;
do {
oldValue = *value;
newValue = ((oldValue & and_mask) | or_mask) ^ xor_mask;
} while (! OSCompareAndSwap(oldValue, newValue, value));
return oldValue;
}
UInt32 OSBitAndAtomic(UInt32 mask, UInt32 * value)
{
return OSBitwiseAtomic(mask, 0, 0, value);
}
UInt32 OSBitOrAtomic(UInt32 mask, UInt32 * value)
{
return OSBitwiseAtomic((UInt32) -1, mask, 0, value);
}
UInt32 OSBitXorAtomic(UInt32 mask, UInt32 * value)
{
return OSBitwiseAtomic((UInt32) -1, 0, mask, value);
}
static Boolean OSCompareAndSwap8(UInt8 oldValue8, UInt8 newValue8, UInt8 * value8)
{
UInt32 mask = 0x000000ff;
UInt32 newbits = (UInt32) newValue8;
int shift;
UInt32 alignment = ((UInt32) value8) & (sizeof(UInt32) - 1);
UInt32 oldValue;
UInt32 newValue;
UInt32 * value;
switch (alignment) {
default:
case 0:
value = (UInt32 *) value8;
shift = 24;
break;
case 1:
value = (UInt32 *) (value8 + 1);
shift = 16;
break;
case 2:
value = (UInt32 *) (value8 + 2);
shift = 8;
break;
case 3:
value = (UInt32 *) (value8 + 3);
shift = 0;
break;
}
mask <<= shift;
newbits <<= shift;
oldValue = *value;
newValue = (oldValue & ~mask) | (newbits & mask);
return OSCompareAndSwap(oldValue, newValue, value);
}
static Boolean OSTestAndSetClear(UInt32 bit, Boolean wantSet, UInt8 * startAddress)
{
UInt8 mask = 1;
UInt8 oldValue;
UInt8 wantValue;
startAddress += (bit / 8);
mask <<= (7 - (bit % 8));
wantValue = wantSet ? mask : 0;
do {
oldValue = *startAddress;
if ((oldValue & mask) == wantValue) {
break;
}
} while (! OSCompareAndSwap8(oldValue, (oldValue & ~mask) | wantValue, startAddress));
return (oldValue & mask) == wantValue;
}
Boolean OSTestAndSet(UInt32 bit, UInt8 * startAddress)
{
return OSTestAndSetClear(bit, true, startAddress);
}
Boolean OSTestAndClear(UInt32 bit, UInt8 * startAddress)
{
return OSTestAndSetClear(bit, false, startAddress);
}
void * OSDequeueAtomic(void ** inList, SInt32 inOffset)
{
void * oldListHead;
void * newListHead;
do {
oldListHead = *inList;
if (oldListHead == NULL) {
break;
}
newListHead = *(void **) (((char *) oldListHead) + inOffset);
} while (! OSCompareAndSwap((UInt32)oldListHead,
(UInt32)newListHead, (UInt32 *)inList));
return oldListHead;
}
void OSEnqueueAtomic(void ** inList, void * inNewLink, SInt32 inOffset)
{
void * oldListHead;
void * newListHead = inNewLink;
void ** newLinkNextPtr = (void **) (((char *) inNewLink) + inOffset);
do {
oldListHead = *inList;
*newLinkNextPtr = oldListHead;
} while (! OSCompareAndSwap((UInt32)oldListHead, (UInt32)newListHead,
(UInt32 *)inList));
}
SInt8 OSIncrementAtomic8(SInt8 * value)
{
return OSAddAtomic8(1, value);
}
SInt8 OSDecrementAtomic8(SInt8 * value)
{
return OSAddAtomic8(-1, value);
}
SInt8 OSAddAtomic8(SInt32 amount, SInt8 * value)
{
SInt8 oldValue;
SInt8 newValue;
do {
oldValue = *value;
newValue = oldValue + amount;
} while (! OSCompareAndSwap8((UInt8) oldValue, (UInt8) newValue, (UInt8 *) value));
return oldValue;
}
static UInt8 OSBitwiseAtomic8(UInt32 and_mask, UInt32 or_mask, UInt32 xor_mask, UInt8 * value)
{
UInt8 oldValue;
UInt8 newValue;
do {
oldValue = *value;
newValue = ((oldValue & and_mask) | or_mask) ^ xor_mask;
} while (! OSCompareAndSwap8(oldValue, newValue, value));
return oldValue;
}
UInt8 OSBitAndAtomic8(UInt32 mask, UInt8 * value)
{
return OSBitwiseAtomic8(mask, 0, 0, value);
}
UInt8 OSBitOrAtomic8(UInt32 mask, UInt8 * value)
{
return OSBitwiseAtomic8((UInt32) -1, mask, 0, value);
}
UInt8 OSBitXorAtomic8(UInt32 mask, UInt8 * value)
{
return OSBitwiseAtomic8((UInt32) -1, 0, mask, value);
}
static Boolean OSCompareAndSwap16(UInt16 oldValue16, UInt16 newValue16, UInt16 * value16)
{
UInt32 mask = 0x0000ffff;
UInt32 newbits = (UInt32) newValue16;
int shift;
UInt32 alignment = ((UInt32) value16) & (sizeof(UInt32) - 1);
UInt32 oldValue;
UInt32 newValue;
UInt32 * value;
if (alignment == 2) {
value = (UInt32 *) (value16 - 1);
shift = 0;
}
else {
value = (UInt32 *) value16;
shift = 16;
}
mask <<= shift;
newbits <<= shift;
oldValue = *value;
newValue = (oldValue & ~mask) | (newbits & mask);
return OSCompareAndSwap(oldValue, newValue, value);
}
SInt16 OSIncrementAtomic16(SInt16 * value)
{
return OSAddAtomic16(1, value);
}
SInt16 OSDecrementAtomic16(SInt16 * value)
{
return OSAddAtomic16(-1, value);
}
SInt16 OSAddAtomic16(SInt32 amount, SInt16 * value)
{
SInt16 oldValue;
SInt16 newValue;
do {
oldValue = *value;
newValue = oldValue + amount;
} while (! OSCompareAndSwap16((UInt16) oldValue, (UInt16) newValue, (UInt16 *) value));
return oldValue;
}
static UInt16 OSBitwiseAtomic16(UInt32 and_mask, UInt32 or_mask, UInt32 xor_mask, UInt16 * value)
{
UInt16 oldValue;
UInt16 newValue;
do {
oldValue = *value;
newValue = ((oldValue & and_mask) | or_mask) ^ xor_mask;
} while (! OSCompareAndSwap16(oldValue, newValue, value));
return oldValue;
}
UInt16 OSBitAndAtomic16(UInt32 mask, UInt16 * value)
{
return OSBitwiseAtomic16(mask, 0, 0, value);
}
UInt16 OSBitOrAtomic16(UInt32 mask, UInt16 * value)
{
return OSBitwiseAtomic16((UInt32) -1, mask, 0, value);
}
UInt16 OSBitXorAtomic16(UInt32 mask, UInt16 * value)
{
return OSBitwiseAtomic16((UInt32) -1, 0, mask, value);
}