OC代碼
在main.m文件中定義ZYPerson類,分別有atomic修飾的屬性name和nonatomic修飾的屬性gender
@interface ZYPerson : NSObject
@property (atomic, copy) NSString *name;
@property (nonatomic, copy) NSString *gender;
@end
@implementation ZYPerson
@end
通過終端命令將main.m里的內(nèi)容轉(zhuǎn)換成c++代碼
xcrun -sdk iphoneos clang -arch arm64 -rewrite-objc main.m
setter
先來看main.cpp中name和gender的setter方法
static void _I_ZYPerson_setName_(ZYPerson * self, SEL _cmd, NSString *name) {
objc_setProperty (
self,
_cmd,
__OFFSETOFIVAR__(struct ZYPerson, _name),
(id)name,
1,
1);
}
static void _I_ZYPerson_setGender_(ZYPerson * self, SEL _cmd, NSString *gender) {
objc_setProperty (
self,
_cmd,
__OFFSETOFIVAR__(struct ZYPerson, _gender),
(id)gender,
0,
1);
}
發(fā)現(xiàn)objc_setProperty中只有第5個(gè)參數(shù)不同,猜測跟atomic有關(guān)。
去runtime源碼中搜索方法objc_setProperty,可以發(fā)現(xiàn):
- 第5個(gè)參數(shù)的確是用來判斷atomic的。
- 第6個(gè)參數(shù)shouldCopy,從實(shí)現(xiàn)來看是用來判斷copy關(guān)鍵字的
#define MUTABLE_COPY 2
void objc_setProperty(id self, SEL _cmd, ptrdiff_t offset, id newValue, BOOL atomic, signed char shouldCopy)
{
bool copy = (shouldCopy && shouldCopy != MUTABLE_COPY);
bool mutableCopy = (shouldCopy == MUTABLE_COPY);
reallySetProperty(self, _cmd, newValue, offset, atomic, copy, mutableCopy);
}
objc_setProperty內(nèi)部調(diào)用了真正的屬性賦值方法reallySetProperty,前6個(gè)參數(shù)和objc_setProperty一致,最后一個(gè)參數(shù)用來判斷shouldCopy是否等于MUTABLE_COPY=2。
static inline void reallySetProperty(id self, SEL _cmd, id newValue, ptrdiff_t offset, bool atomic, bool copy, bool mutableCopy)
{
if (offset == 0) {
object_setClass(self, newValue);
return;
}
id oldValue;
id *slot = (id*) ((char*)self + offset);
if (copy) {
newValue = [newValue copyWithZone:nil];
} else if (mutableCopy) {
newValue = [newValue mutableCopyWithZone:nil];
} else {
if (*slot == newValue) return;
newValue = objc_retain(newValue);
}
if (!atomic) {
oldValue = *slot;
*slot = newValue;
} else {
spinlock_t& slotlock = PropertyLocks[slot];
slotlock.lock();
oldValue = *slot;
*slot = newValue;
slotlock.unlock();
}
objc_release(oldValue);
}
可以看出:不管是copy、mutableCopy、strong、retain關(guān)鍵詞修飾的OC對象,newValue都會(huì)retain一次,copy是操作內(nèi)部retain的。
- nonatomic:1. 是將新的屬性值直接賦值給對應(yīng)屬性; 2. release原值
- atomic:在賦值前后進(jìn)行spinlock_t加鎖/解鎖操作,其他操作跟nonatomic是一致的。
using spinlock_t = mutex_tt<LOCKDEBUG>;
class mutex_tt : nocopy_t {
os_unfair_lock mLock;
}
查找源碼發(fā)現(xiàn)spinlock_t是os_unfair_lock,是自旋鎖。
getter
同樣getter方法也是如此,atomic修飾的屬性進(jìn)行加鎖處理,說明atomic屬性在setter和getter方法中是利用自旋鎖保證線程安全的。
static NSString * _I_ZYPerson_name(ZYPerson * self, SEL _cmd) {
typedef NSString * _TYPE;
return (_TYPE)objc_getProperty(
self,
_cmd,
__OFFSETOFIVAR__(struct ZYPerson, _name),
1);
}
id objc_getProperty(id self, SEL _cmd, ptrdiff_t offset, BOOL atomic) {
if (offset == 0) {
return object_getClass(self);
}
// Retain release world
id *slot = (id*) ((char*)self + offset);
if (!atomic) return *slot;
// Atomic retain release world
spinlock_t& slotlock = PropertyLocks[slot];
slotlock.lock();
id value = objc_retain(*slot);
slotlock.unlock();
// for performance, we (safely) issue the autorelease OUTSIDE of the spinlock.
return objc_autoreleaseReturnValue(value);
}
注意:atomic保證setter和getter方法是線程安全的,但是不能保證屬性在使用過程中的安全。即person.name=@"001"是安全的,但是不能保證[person.name stringByAppendingString: @"test"];也是安全的。
閱讀源碼過程中涉及到的其他方法
判斷一個(gè)實(shí)例對象是否是TaggedPointer
#if (TARGET_OS_OSX || TARGET_OS_IOSMAC) && __x86_64__
// 64-bit Mac - tag bit is LSB
# define OBJC_MSB_TAGGED_POINTERS 0
#else
// Everything else - tag bit is MSB
# define OBJC_MSB_TAGGED_POINTERS 1
#endif
#if OBJC_MSB_TAGGED_POINTERS
# define _OBJC_TAG_MASK (1UL<<63)
#else
# define _OBJC_TAG_MASK 1UL
#endif
static inline bool
_objc_isTaggedPointer(const void * _Nullable ptr)
{
return ((uintptr_t)ptr & _OBJC_TAG_MASK) == _OBJC_TAG_MASK;
}
修改對象的isa指針指向新的Class對象
Class object_setClass(id obj, Class cls)
{
if (!obj) return nil;
if (!cls->isFuture() && !cls->isInitialized()) {
lookUpImpOrNil(nil, @selector(initialize), cls, LOOKUP_INITIALIZE);
}
return obj->changeIsa(cls);
}