美文网首页
synchronized 编译记录

synchronized 编译记录

作者: _一叶孤帆 | 来源:发表于2021-10-14 16:31 被阅读0次

    源码

    #import <Foundation/Foundation.h>
    
    int main(int argc, const char * argv[]) {
        @autoreleasepool {
            // insert code here...
            NSLog(@"Hello, World!");
            
            NSObject *objc = [[NSObject alloc] init];
            
            @synchronized (objc) {
                NSLog(@"加了锁的 Log ");
            }
            
            
        }
        return 0;
    }
    
    

    通过 xcrun -sdk iphoneos clang -arch arm64 -rewrite-objc main.m -o mainC.cpp 编译 cpp 文件

    int main(int argc, const char * argv[]) {
        /* @autoreleasepool */ { __AtAutoreleasePool __autoreleasepool;
    
            NSObject *objc = ((NSObject *(*)(id, SEL))(void *)objc_msgSend)((id)((NSObject *(*)(id, SEL))(void *)objc_msgSend)((id)objc_getClass("NSObject"), sel_registerName("alloc")), sel_registerName("init"));
            
            { id _rethrow = 0; id _sync_obj = (id)objc; 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);
                    
                    NSLog((NSString *)&__NSConstantStringImpl__var_folders_dv_nb1mrprn3kj3cnk3tts4918w0000gn_T_main_352584_mi_1);
                } 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);}
            }
            
            
            
        }
        return 0;
    }
    

    解析

    truct _SYNC_EXIT {
                        
                        _SYNC_EXIT(id arg) : sync_exit(arg) {}
                        
                        ~_SYNC_EXIT() {objc_sync_exit(sync_exit);}
                        
                        id sync_exit;
                        
                    } _sync_exit(_sync_obj);
    

    声明了一个结构体 _SYNC_EXIT

    _SYNC_EXIT(id arg) : sync_exit(arg) {} 是这个结构体的构造函数,sync_exit(arg) 是用arg 来初始化 sync_exit 这个成员

    ~_SYNC_EXIT() {objc_sync_exit(sync_exit);} 是这个对象的析构函数, 在释放时会执行 objc_sync_exit

    _sync_exit(_sync_obj) 是直接声明了一个 _sync_exit 变量并用 _sync_obj 初始化

    NSLog((NSString *)&__NSConstantStringImpl__var_folders_dv_nb1mrprn3kj3cnk3tts4918w0000gn_T_main_352584_mi_1); 为什么在析构函数下面,因为 _sync_exit 对象是在释放的时候销毁的所以只有这个代码块执行完才会调用 ~_SYNC_EXIT() 然后才会执行 objc_sync_exit(sync_exit)

    所以整体执行顺序为 objc_sync_enter(_sync_obj) -> NSLog -> objc_sync_exit(sync_exit)

    所以 synchronized 实现可以断定为调用

    objc_sync_enter
    objc_sync_exit

    可通过 objc 源码查看 objc_sync_enter/objc_sync_exit 函数

    相关文章

      网友评论

          本文标题:synchronized 编译记录

          本文链接:https://www.haomeiwen.com/subject/ujruoltx.html