马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
x
在 Objective-C 运行时中,原子操纵主要通过以下几种方式实现:
1. 基本原子操纵
- // 原子操作的基本实现
- #if __has_feature(c_atomic)
- #define OSAtomicIncrement32(p) __c11_atomic_add((_Atomic(int32_t) *)(p), 1, __ATOMIC_RELAXED)
- #define OSAtomicDecrement32(p) __c11_atomic_sub((_Atomic(int32_t) *)(p), 1, __ATOMIC_RELAXED)
- #define OSAtomicIncrement32Barrier(p) __c11_atomic_add((_Atomic(int32_t) *)(p), 1, __ATOMIC_SEQ_CST)
- #define OSAtomicDecrement32Barrier(p) __c11_atomic_sub((_Atomic(int32_t) *)(p), 1, __ATOMIC_SEQ_CST)
- #else
- // 使用内联汇编实现原子操作
- static ALWAYS_INLINE int32_t
- OSAtomicIncrement32(volatile int32_t *value) {
- return __sync_fetch_and_add(value, 1) + 1;
- }
- static ALWAYS_INLINE int32_t
- OSAtomicDecrement32(volatile int32_t *value) {
- return __sync_fetch_and_sub(value, 1) - 1;
- }
- #endif
复制代码 2. 自旋锁实现
- typedef struct {
- volatile int32_t value;
- } OSSpinLock;
- // 自旋锁的原子操作
- static ALWAYS_INLINE void
- OSSpinLockLock(volatile OSSpinLock *lock)
- {
- do {
- while (lock->value != 0) {
- // 忙等待
- __asm__ volatile ("pause");
- }
- } while (!OSAtomicCompareAndSwap32(0, 1, &lock->value));
- }
- static ALWAYS_INLINE bool
- OSSpinLockTry(volatile OSSpinLock *lock)
- {
- return OSAtomicCompareAndSwap32(0, 1, &lock->value);
- }
- static ALWAYS_INLINE void
- OSSpinLockUnlock(volatile OSSpinLock *lock)
- {
- OSAtomicAnd32Barrier(0, &lock->value);
- }
复制代码 3. 比较和互换操纵
- // 原子比较和交换操作
- static ALWAYS_INLINE bool
- OSAtomicCompareAndSwapPtr(void *oldp, void *newp, void *volatile *dst)
- {
- return __sync_bool_compare_and_swap(dst, oldp, newp);
- }
- static ALWAYS_INLINE bool
- OSAtomicCompareAndSwapLong(long oldl, long newl, volatile long *dst)
- {
- return __sync_bool_compare_and_swap(dst, oldl, newl);
- }
- static ALWAYS_INLINE bool
- OSAtomicCompareAndSwap32(int32_t old, int32_t new, volatile int32_t *dst)
- {
- return __sync_bool_compare_and_swap(dst, old, new);
- }
复制代码 4. 内存屏障
- // 内存屏障实现
- #define OSMemoryBarrier() __sync_synchronize()
- static ALWAYS_INLINE void
- OSMemoryBarrierBeforeUnlock()
- {
- #if defined(__arm__) || defined(__arm64__)
- OSMemoryBarrier();
- #endif
- }
复制代码 5. 原子引用计数操纵
- inline bool
- objc_object::rootTryRetain()
- {
- return sidetable_tryRetain() || rootRetain_overflow(true);
- }
- inline bool
- objc_object::sidetable_tryRetain()
- {
- SideTable& table = SideTables()[this];
-
- bool result = false;
-
- table.lock();
- RefcountMap::iterator it = table.refcnts.find(this);
- if (it != table.refcnts.end()) {
- RefcountMap::value_type &pair = *it;
- if (pair.second & SIDE_TABLE_RC_PINNED) {
- pair.second += SIDE_TABLE_RC_ONE;
- result = true;
- }
- else if (pair.second & SIDE_TABLE_RC_WEAKLY_REFERENCED) {
- pair.second = SIDE_TABLE_RC_ONE | SIDE_TABLE_RC_WEAKLY_REFERENCED;
- result = true;
- }
- }
- table.unlock();
-
- return result;
- }
复制代码 6. 原子属性访问器
- // 原子属性的 getter
- id objc_getProperty(id self, SEL _cmd, ptrdiff_t offset, BOOL atomic) {
- if (!atomic) return *((id *)((char *)self + offset));
-
- // 原子操作
- spinlock_t& slotlock = PropertyLocks[GOODHASH(offset)];
- slotlock.lock();
- id value = *((id *)((char *)self + offset));
- slotlock.unlock();
- return value;
- }
- // 原子属性的 setter
- static inline void reallySetProperty(id self, SEL _cmd, id newValue,
- ptrdiff_t offset, bool atomic, bool copy)
- {
- if (!atomic) {
- *((id *)((char *)self + offset)) = newValue;
- return;
- }
-
- spinlock_t& slotlock = PropertyLocks[GOODHASH(offset)];
- slotlock.lock();
- *((id *)((char *)self + offset)) = newValue;
- slotlock.unlock();
- }
复制代码 7. 原子操纵的使用场景
1. 引用计数管理
- // 原子递增引用计数
- id objc_retain(id obj) {
- if (!obj) return obj;
- if (obj->isTaggedPointer()) return obj;
- return obj->retain();
- }
复制代码 2. 属性访问
- // 原子属性的实现
- @property (atomic) NSString *name;
复制代码 3. 数据结构操纵
- // 线程安全的数组操作
- - (void)addObject:(id)object {
- @synchronized(self) {
- [_array addObject:object];
- }
- }
复制代码 这些原子操纵的实现保证了:
通过这些机制,Objective-C 运行时可以大概保证多线程情况下的数据一致性和线程安全。
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |