@@ -20,19 +20,24 @@ #import "ObjFWRT.h" #import "private.h" #ifdef OF_HAVE_THREADS # import "OFPlainMutex.h" -# define NUM_SPINLOCKS 8 /* needs to be a power of 2 */ -# define SPINLOCK_HASH(p) ((unsigned)((uintptr_t)p >> 4) & (NUM_SPINLOCKS - 1)) -static OFSpinlock spinlocks[NUM_SPINLOCKS]; +# define numSpinlocks 8 /* needs to be a power of 2 */ +static OFSpinlock spinlocks[numSpinlocks]; + +static OF_INLINE size_t +spinlockSlot(const void *ptr) +{ + return ((size_t)((uintptr_t)ptr >> 4) & (numSpinlocks - 1)); +} #endif #ifdef OF_HAVE_THREADS OF_CONSTRUCTOR() { - for (size_t i = 0; i < NUM_SPINLOCKS; i++) + for (size_t i = 0; i < numSpinlocks; i++) if (OFSpinlockNew(&spinlocks[i]) != 0) OBJC_ERROR("Failed to initialize spinlocks!"); } #endif @@ -40,17 +45,17 @@ objc_getProperty(id self, SEL _cmd, ptrdiff_t offset, bool atomic) { if (atomic) { id *ptr = (id *)(void *)((char *)self + offset); #ifdef OF_HAVE_THREADS - unsigned hash = SPINLOCK_HASH(ptr); + size_t slot = spinlockSlot(ptr); - OFEnsure(OFSpinlockLock(&spinlocks[hash]) == 0); + OFEnsure(OFSpinlockLock(&spinlocks[slot]) == 0); @try { return [[*ptr retain] autorelease]; } @finally { - OFEnsure(OFSpinlockUnlock(&spinlocks[hash]) == 0); + OFEnsure(OFSpinlockUnlock(&spinlocks[slot]) == 0); } #else return [[*ptr retain] autorelease]; #endif } @@ -63,13 +68,13 @@ signed char copy) { if (atomic) { id *ptr = (id *)(void *)((char *)self + offset); #ifdef OF_HAVE_THREADS - unsigned hash = SPINLOCK_HASH(ptr); + size_t slot = spinlockSlot(ptr); - OFEnsure(OFSpinlockLock(&spinlocks[hash]) == 0); + OFEnsure(OFSpinlockLock(&spinlocks[slot]) == 0); @try { #endif id old = *ptr; switch (copy) { @@ -84,11 +89,11 @@ } [old release]; #ifdef OF_HAVE_THREADS } @finally { - OFEnsure(OFSpinlockUnlock(&spinlocks[hash]) == 0); + OFEnsure(OFSpinlockUnlock(&spinlocks[slot]) == 0); } #endif return; } @@ -115,17 +120,17 @@ objc_getPropertyStruct(void *dest, const void *src, ptrdiff_t size, bool atomic, bool strong) { if (atomic) { #ifdef OF_HAVE_THREADS - unsigned hash = SPINLOCK_HASH(src); + size_t slot = spinlockSlot(src); - OFEnsure(OFSpinlockLock(&spinlocks[hash]) == 0); + OFEnsure(OFSpinlockLock(&spinlocks[slot]) == 0); #endif memcpy(dest, src, size); #ifdef OF_HAVE_THREADS - OFEnsure(OFSpinlockUnlock(&spinlocks[hash]) == 0); + OFEnsure(OFSpinlockUnlock(&spinlocks[slot]) == 0); #endif return; } @@ -136,17 +141,17 @@ objc_setPropertyStruct(void *dest, const void *src, ptrdiff_t size, bool atomic, bool strong) { if (atomic) { #ifdef OF_HAVE_THREADS - unsigned hash = SPINLOCK_HASH(src); + size_t slot = spinlockSlot(src); - OFEnsure(OFSpinlockLock(&spinlocks[hash]) == 0); + OFEnsure(OFSpinlockLock(&spinlocks[slot]) == 0); #endif memcpy(dest, src, size); #ifdef OF_HAVE_THREADS - OFEnsure(OFSpinlockUnlock(&spinlocks[hash]) == 0); + OFEnsure(OFSpinlockUnlock(&spinlocks[slot]) == 0); #endif return; } @@ -165,11 +170,11 @@ *outCount = 0; return NULL; } - objc_global_mutex_lock(); + objc_globalMutex_lock(); count = 0; if (class->info & OBJC_CLASS_INFO_NEW_ABI) for (iter = class->propertyList; iter != NULL; iter = iter->next) @@ -177,11 +182,11 @@ if (count == 0) { if (outCount != NULL) *outCount = 0; - objc_global_mutex_unlock(); + objc_globalMutex_unlock(); return NULL; } properties = malloc((count + 1) * sizeof(objc_property_t)); if (properties == NULL) @@ -195,11 +200,11 @@ properties[count] = NULL; if (outCount != NULL) *outCount = count; - objc_global_mutex_unlock(); + objc_globalMutex_unlock(); return properties; } const char *