@ synchronized

2021-01-14  本文已影响0人  老黑来袭

递归锁:

synchronized

 @synchronized ([NSObject new]) {

        //加锁操作

    };

下面看看原理,上面这段代码编译之后的代码:

int main(intargc,constchar* argv[]) {

    /* @autoreleasepool */{ __AtAutoreleasePool __autoreleasepool; 

        {

            id _rethrow =0;

            id _sync_obj = (id)((NSObject *(*)(id, SEL))(void*)objc_msgSend)((id)objc_getClass("NSObject"), sel_registerName("new"));

            objc_sync_enter(_sync_obj);

try {

struct _SYNC_EXIT {

        _SYNC_EXIT(id arg) : sync_exit(arg) {}

        ~_SYNC_EXIT() {objc_sync_exit(sync_exit);}

        id sync_exit;

} _sync_exit(_sync_obj);

        }catch(id e) {_rethrow = e;}

{struct_FIN{_FIN(id reth) : rethrow(reth) {}

~_FIN() {if(rethrow) objc_exception_throw(rethrow); }

id rethrow;

} _fin_force_rethow(_rethrow);}

}

;

    }

    return0;

}

抓到了两个重点函数 objc_sync_enter 、_sync_exit, 可以猜测OC是通过这两个参数控制锁的权限.

接下来找找objc_sync_enter的源码在哪里

```

// Begin synchronizing on 'obj'. 

// Allocates recursive mutex associated with 'obj' if needed. 递归锁

// Returns OBJC_SYNC_SUCCESS once lock is acquired. 

int objc_sync_enter(id obj)

{

    int result = OBJC_SYNC_SUCCESS;

    if(obj) {

        SyncData* data =id2data(obj,ACQUIRE);

        ASSERT(data);

        data->mutex.lock();

    }else{

        // @synchronized(nil) does nothing

        if(DebugNilSync) {

            _objc_inform("NIL SYNC DEBUG: @synchronized(nil); set a breakpoint on objc_sync_nil to debug");

        }

        objc_sync_nil();

    }

    returnresult;

}

```

如果对象不存在则不会加锁。

接下来看到 SyncData 这个么对象, 貌似是这个对象进行的加锁操作

```

typedef struc talignas(CacheLineSize) SyncData {

    struct SyncData* nextData;

    DisguisedPtr<objc_object> object;

    int32_t threadCount;  // number of THREADS using this block

    recursive_mutex_t mutex;

} SyncData;

```

nextData 这说明这个东西是个链表结构。

object 当前锁住的对象

threadCount 等待的线程数

mutex 锁

那说明 这个方法是吧对象和锁关联起来

```

id2data(obj, ACQUIRE)

```

上一篇 下一篇

猜你喜欢

热点阅读