GCD底层关键数据结构

·  阅读 12904

GCD底层源码十分难读,晦涩的原因在于,为了兼容,大范围的多层宏不断嵌套的方式,再加上没有较详细的注释辅助说明,导致阅读起来逻辑无法连贯。

本文针对于关键性的数据结构做了具体整理,辅助有兴趣探索底层的同学更好的阅读GCD源码

dispatch_xxx_t [dispatch_queue_t dispatch_lane_t dispatch_object_t]

struct dispatch_continuation_s *dispatch_continuation_t
struct dispatch_queue_s *dispatch_queue_t
struct dispatch_lane_s *dispatch_lane_t

typedef struct dispatch_object_s *dispatch_object_t

typedef union {
    struct _os_object_s *_os_obj;
    struct dispatch_object_s *_do;
    struct dispatch_queue_s *_dq;
    struct dispatch_queue_attr_s *_dqa;
    struct dispatch_group_s *_dg;
    struct dispatch_source_s *_ds;
    struct dispatch_channel_s *_dch;
    struct dispatch_mach_s *_dm;
    struct dispatch_mach_msg_s *_dmsg;
    struct dispatch_semaphore_s *_dsema;
    struct dispatch_data_s *_ddata;
    struct dispatch_io_s *_dchannel;
    
    struct dispatch_continuation_s *_dc;
    struct dispatch_sync_context_s *_dsc;
    struct dispatch_operation_s *_doperation;
    struct dispatch_disk_s *_ddisk;
    struct dispatch_workloop_s *_dwl;
    struct dispatch_lane_s *_dl;
    struct dispatch_queue_static_s *_dsq;
    struct dispatch_queue_global_s *_dgq;
    struct dispatch_queue_pthread_root_s *_dpq;
    dispatch_queue_class_t _dqu;
    dispatch_lane_class_t _dlu;
    uintptr_t _do_value;
} dispatch_object_t DISPATCH_TRANSPARENT_UNION;

typedef struct dispatch_lane_s *dispatch_queue_serial_t;
typedef struct dispatch_lane_s *dispatch_queue_concurrent_t;
复制代码

dispatch_lane_class_t

typedef union {
    struct dispatch_lane_s *_dl;
    struct dispatch_queue_static_s *_dsq;
    struct dispatch_queue_global_s *_dgq;
    struct dispatch_queue_pthread_root_s *_dpq;
    struct dispatch_source_s *_ds;
    struct dispatch_channel_s *_dch;
    struct dispatch_mach_s *_dm;
    
    id<OS_dispatch_queue> _objc_dq; //unsafe cast for the sake of object.m
} dispatch_lane_class_t DISPATCH_TRANSPARENT_UNION;
复制代码

dispatch_queue_class_t

typedef union {
    struct dispatch_queue_s *_dq;
    struct dispatch_workloop_s *_dwl;
    struct dispatch_lane_s *_dl;
    struct dispatch_queue_static_s *_dsq;
    struct dispatch_queue_global_s *_dgq;
    struct dispatch_queue_pthread_root_s *_dpq;
    struct dispatch_source_s *_ds;
    struct dispatch_channel_s *_dch;
    struct dispatch_mach_s *_dm;
    dispatch_lane_class_t _dlu;
    
    id<OS_dispatch_queue> _objc_dq;
} dispatch_queue_class_t DISPATCH_TRANSPARENT_UNION;
复制代码

struct dispatch_object_s

typedef struct dispatch_object_s *dispatch_object_t

struct dispatch_object_s {
    struct _os_object_s _as_os_obj[0]; \
    const struct dispatch_object_vtable_s \
        *__ptrauth_objc_isa_pointer do_vtable; \
    int volatile do_ref_cnt; \
    int volatile do_xref_cnt) \
    struct dispatch_object_s *volatile do_next; \
    struct dispatch_queue_s *do_targetq; \
    void *do_ctxt; \
    union { \
        void *do_finalizer; \
        void *do_introspection_ctxt; \
    };
}
复制代码

_os_object_vtable_s

typedef struct _os_object_vtable_s {
    const void *_os_obj_objc_isa;
} _os_object_vtable_s;
复制代码

_os_object_s

typedef struct _os_object_s {
    /* must be pointer-sized and use __ptrauth_objc_isa_pointer */ \
    const _os_object_vtable_s *__ptrauth_objc_isa_pointer os_obj_isa; \
    int volatile os_obj_ref_cnt; \ 
    int volatile os_obj_xref_cnt;
    
} _os_object_s;
复制代码

struct dispatch_lane_vtable_s

struct dispatch_lane_vtable_s { 
    const void *_os_obj_objc_isa;
    struct dispatch_lane_extra_vtable_s _os_obj_vtable; 
}; 
复制代码

struct dispatch_queue_vtable_s

struct dispatch_queue_vtable_s { 
    const void *_os_obj_objc_isa;
    struct dispatch_queue_extra_vtable_s _os_obj_vtable; 
};
复制代码

struct dispatch_object_vtable_s

struct dispatch_object_vtable_s { 
    const void *_os_obj_objc_isa;
    struct dispatch_object_extra_vtable_s _os_obj_vtable; 
};
复制代码

dispatch_object_extra_vtable_s

struct dispatch_object_extra_vtable_s { 
    DISPATCH_OBJECT_VTABLE_HEADER(dispatch_object)
};

struct dispatch_object_extra_vtable_s { 
    unsigned long const do_type; 
    void *const (do_dispose)(struct dispatch_object_s *, 
        bool *allow_free); 
    size_t *const (do_debug)(struct dispatch_object_s *, \
    char *, size_t); 
    void *const (do_invoke)(struct dispatch_object_s *, \
        dispatch_invoke_context_t, dispatch_invoke_flags_t)
};

复制代码

dispatch_source_extra_vtable_s

struct dispatch_source_extra_vtable_s { 
    DISPATCH_QUEUE_VTABLE_HEADER(dispatch_source)
}; 

struct dispatch_source_extra_vtable_s { 
    unsigned long const do_type; 
    void *const (do_dispose)(struct dispatch_source_s *, 
        bool *allow_free); 
    size_t *const (do_debug)(struct dispatch_source_s *, \
    char *, size_t); 
    void *const (do_invoke)(struct dispatch_source_s *, \
        dispatch_invoke_context_t, dispatch_invoke_flags_t)
        
    void *const (dq_activate)(dispatch_queue_class_t); \
    void *const (dq_wakeup)(dispatch_queue_class_t, \
        dispatch_qos_t, dispatch_wakeup_flags_t); \
    void *const (dq_push)(dispatch_queue_class_t, \
        dispatch_object_t, dispatch_qos_t)  
}; 
复制代码

struct dispatch_lane_s

struct dispatch_lane_s *dispatch_lane_t

typedef struct dispatch_lane_s {
    struct dispatch_queue_s _as_dq[0]; \
    struct dispatch_object_s _as_do[0]; \ 
    struct _os_object_s _as_os_obj[0]; \ 
    const struct dispatch_lane_vtable_s \ 
        *__ptrauth_objc_isa_pointer do_vtable; \ 
    int volatile do_ref_cnt; \ 
    int volatile do_xref_cnt) \ 
    struct dispatch_lane_s *volatile do_next; \ 
    struct dispatch_queue_s *do_targetq; \ 
    void *do_ctxt; \ 
    union { \ 
        void *do_finalizer; \ 
        void *do_introspection_ctxt; \ 
    } \ 
    struct dispatch_object_s *volatile dq_items_tail; \ 
    DISPATCH_UNION_LE(uint64_t volatile dq_state, \ 
    dispatch_lock dq_state_lock, \ 
    uint32_t dq_state_bits \ 
    ) /* LP64 global queue cacheline boundary */ \ 
    unsigned long dq_serialnum; \ 
    const char *dq_label; \ 
    DISPATCH_UNION_LE(uint32_t volatile dq_atomic_flags, \ 
        const uint16_t dq_width, \ 
        const uint16_t __dq_opaque2 \ 
    ); \ 
    dispatch_priority_t dq_priority; \ 
    union { \ 
        struct dispatch_queue_specific_head_s *dq_specific_head; \ 
        struct dispatch_source_refs_s *ds_refs; \ 
        struct dispatch_timer_source_refs_s *ds_timer_refs; \ 
        struct dispatch_mach_recv_refs_s *dm_recv_refs; \ 
        struct dispatch_channel_callbacks_s const *dch_callbacks; \ 
    }; \ 
    int volatile dq_sref_cnt;
    dispatch_unfair_lock_s dq_sidelock; \
    struct dispatch_object_s *volatile dq_items_head; \
    uint32_t dq_side_suspend_cnt
} DISPATCH_ATOMIC64_ALIGN *dispatch_lane_t;
复制代码

struct dispatch_queue_s

struct dispatch_queue_s *dispatch_queue_t

struct dispatch_queue_s { 
    struct dispatch_object_s _as_do[0]; \ 
    struct _os_object_s _as_os_obj[0]; \ 
    const struct dispatch_queue_vtable_s \ 
    *__ptrauth_objc_isa_pointer do_vtable; \ 
    int volatile do_ref_cnt; \ 
    int volatile do_xref_cnt) \ 
    struct dispatch_queue_s *volatile do_next; \ 
    struct dispatch_queue_s *do_targetq; \ 
    void *do_ctxt; \ 
    union { \ 
        void *do_finalizer; \ 
        void *do_introspection_ctxt; \ 
    } \ 
    void *__dq_opaque1; \ 
    DISPATCH_UNION_LE(uint64_t volatile dq_state, \ 
        dispatch_lock dq_state_lock, \ 
        uint32_t dq_state_bits \ 
    ) /* LP64 global queue cacheline boundary */ \ 
    unsigned long dq_serialnum; \ 
    const char *dq_label; \ 
    DISPATCH_UNION_LE(uint32_t volatile dq_atomic_flags, \ 
        const uint16_t dq_width, \ 
        const uint16_t __dq_opaque2 \ 
    ); \ 
    dispatch_priority_t dq_priority; \ 
    union { \ 
        struct dispatch_queue_specific_head_s *dq_specific_head; \ 
        struct dispatch_source_refs_s *ds_refs; \ 
        struct dispatch_timer_source_refs_s *ds_timer_refs; \ 
        struct dispatch_mach_recv_refs_s *dm_recv_refs; \ 
        struct dispatch_channel_callbacks_s const *dch_callbacks; \ 
    }; \ 
    int volatile dq_sref_cnt; 
} DISPATCH_ATOMIC64_ALIGN;
复制代码

struct dispatch_continuation_s

typedef struct dispatch_continuation_s {
    union { \
        const void *__ptrauth_objc_isa_pointer do_vtable; \
        uintptr_t dc_flags; \
    }; \
    union { \
        pthread_priority_t dc_priority; \
        int dc_cache_cnt; \
        uintptr_t dc_pad; \
    }; \
    struct voucher_s *dc_voucher; \
    struct dispatch_continuation_s *volatile do_next; \
    dispatch_function_t dc_func; \
    void *dc_ctxt; \
    void *dc_data; \
    void *dc_other
} *dispatch_continuation_t;
复制代码

struct voucher_s

typedef struct voucher_s {
    struct voucher_vtable_s *__ptrauth_objc_isa_pointer os_obj_isa; /* must be pointer-sized and use __ptrauth_objc_isa_pointer */ \
    int volatile os_obj_ref_cnt; \ 
    int volatile os_obj_xref_cnt;
    
    struct voucher_hash_entry_s {
        uintptr_t vhe_next;
        uintptr_t vhe_prev_ptr;
    } v_list;
    
    mach_voucher_t v_kvoucher, v_ipc_kvoucher; // if equal, only one reference
    voucher_t v_kvbase; // if non-NULL, v_kvoucher is a borrowed reference
    firehose_activity_id_t v_activity;
    uint64_t v_activity_creator;
    firehose_activity_id_t v_parent_activity;
    _voucher_priority_t v_priority;
    unsigned int v_kv_has_importance:1;
#if VOUCHER_ENABLE_RECIPE_OBJECTS
    size_t v_recipe_extra_offset;
    mach_voucher_attr_recipe_size_t v_recipe_extra_size;
#endif
} voucher_s;

 
复制代码

DISPATCH_VTABLE_INSTANCE workloop

DISPATCH_VTABLE_INSTANCE(workloop,
    .do_type        = DISPATCH_WORKLOOP_TYPE,
    .do_dispose     = _dispatch_workloop_dispose,
    .do_debug       = _dispatch_queue_debug,
    .do_invoke      = _dispatch_workloop_invoke,
    .dq_activate    = _dispatch_queue_no_activate,
    .dq_wakeup      = _dispatch_workloop_wakeup,
    .dq_push        = _dispatch_workloop_push,
);

__attribute__((section("__DATA,__objc_data"), used)) \
const struct dispatch_workloop_extra_vtable_s _OS_dispatch_workloop_vtable
 = {
    .do_kind = workloop,
    .do_type        = DISPATCH_WORKLOOP_TYPE,
    .do_dispose     = _dispatch_workloop_dispose,
    .do_debug       = _dispatch_queue_debug,
    .do_invoke      = _dispatch_workloop_invoke,
    .dq_activate    = _dispatch_queue_no_activate,
    .dq_wakeup      = _dispatch_workloop_wakeup,
    .dq_push        = _dispatch_workloop_push,
};
复制代码

DISPATCH_VTABLE_INSTANCE queue

DISPATCH_VTABLE_INSTANCE(queue,
    .do_type        = _DISPATCH_QUEUE_CLUSTER,
    .do_dispose     = _dispatch_object_no_dispose,
    .do_debug       = _dispatch_queue_debug,
    .do_invoke      = _dispatch_object_no_invoke,
    .dq_activate    = _dispatch_queue_no_activate,
);

__attribute__((section("__DATA,__objc_data"), used)) \
const struct dispatch_queue_extra_vtable_s _OS_dispatch_queue_vtable
 = {
    .do_kind        = queue,
    .do_type        = _DISPATCH_QUEUE_CLUSTER,
    .do_dispose     = _dispatch_object_no_dispose,
    .do_debug       = _dispatch_queue_debug,
    .do_invoke      = _dispatch_object_no_invoke,
    .dq_activate    = _dispatch_queue_no_activate
};
复制代码

DISPATCH_VTABLE_SUBCLASS_INSTANCE - queue_serial

DISPATCH_VTABLE_SUBCLASS_INSTANCE(queue_serial, lane,
    .do_type        = DISPATCH_QUEUE_SERIAL_TYPE,
    .do_dispose     = _dispatch_lane_dispose,
    .do_debug       = _dispatch_queue_debug,
    .do_invoke      = _dispatch_lane_invoke,
    \
    .dq_activate    = _dispatch_lane_activate,
    .dq_wakeup      = _dispatch_lane_wakeup,
    .dq_push        = _dispatch_lane_push,
);

__attribute__((section("__DATA,__objc_data"), used)) \
const struct dispatch_lane_extra_vtable_s _OS_dispatch_queue_serial_vtable
 = { 
    .do_kind        = queue_serial, 
    .do_type        = DISPATCH_QUEUE_SERIAL_TYPE,
    .do_dispose     = _dispatch_lane_dispose,
    .do_debug       = _dispatch_queue_debug,
    .do_invoke      = _dispatch_lane_invoke,
    .dq_activate    = _dispatch_lane_activate,
    .dq_wakeup      = _dispatch_lane_wakeup,
    .dq_push        = _dispatch_lane_push
 };
 
复制代码

DISPATCH_VTABLE_SUBCLASS_INSTANCE - queue_concurrent

DISPATCH_VTABLE_SUBCLASS_INSTANCE(queue_concurrent, lane,
    .do_type        = DISPATCH_QUEUE_CONCURRENT_TYPE,
    .do_dispose     = _dispatch_lane_dispose,
    .do_debug       = _dispatch_queue_debug,
    .do_invoke      = _dispatch_lane_invoke,
    .dq_activate    = _dispatch_lane_activate,
    .dq_wakeup      = _dispatch_lane_wakeup,
    .dq_push        = _dispatch_lane_concurrent_push,
);

__attribute__((section("__DATA,__objc_data"), used)) \
const struct dispatch_lane_extra_vtable_s _OS_dispatch_queue_concurrent_vtable
 = { 
    .do_kind        = queue_concurrent, 
    .do_type        = DISPATCH_QUEUE_CONCURRENT_TYPE,
    .do_dispose     = _dispatch_lane_dispose,
    .do_debug       = _dispatch_queue_debug,
    .do_invoke      = _dispatch_lane_invoke,
    .dq_activate    = _dispatch_lane_activate,
    .dq_wakeup      = _dispatch_lane_wakeup,
    .dq_push        = _dispatch_lane_concurrent_push
 };
 
 
复制代码

DISPATCH_VTABLE_SUBCLASS_INSTANCE - queue_global

DISPATCH_VTABLE_SUBCLASS_INSTANCE(queue_global, lane,
    .do_type        = DISPATCH_QUEUE_GLOBAL_ROOT_TYPE,
    .do_dispose     = _dispatch_object_no_dispose,
    .do_debug       = _dispatch_queue_debug,
    .do_invoke      = _dispatch_object_no_invoke,
    .dq_activate    = _dispatch_queue_no_activate,
    .dq_wakeup      = _dispatch_root_queue_wakeup,
    .dq_push        = _dispatch_root_queue_push,
);

__attribute__((section("__DATA,__objc_data"), used)) \
const struct dispatch_lane_extra_vtable_s _OS_dispatch_queue_global_vtable
 = { 
    .do_kind        = queue_concurrent, 
    .do_type        = DISPATCH_QUEUE_GLOBAL_ROOT_TYPE,
    .do_dispose     = _dispatch_object_no_dispose,
    .do_debug       = _dispatch_queue_debug,
    .do_invoke      = _dispatch_object_no_invoke,
    .dq_activate    = _dispatch_queue_no_activate,
    .dq_wakeup      = _dispatch_root_queue_wakeup,
    .dq_push        = _dispatch_root_queue_push
 };
 
复制代码

DISPATCH_VTABLE_SUBCLASS_INSTANCE - queue_pthread_root

DISPATCH_VTABLE_SUBCLASS_INSTANCE(queue_pthread_root, lane,
    .do_type        = DISPATCH_QUEUE_PTHREAD_ROOT_TYPE,
    .do_dispose     = _dispatch_pthread_root_queue_dispose,
    .do_debug       = _dispatch_queue_debug,
    .do_invoke      = _dispatch_object_no_invoke,
    .dq_activate    = _dispatch_queue_no_activate,
    .dq_wakeup      = _dispatch_root_queue_wakeup,
    .dq_push        = _dispatch_root_queue_push,
);

__attribute__((section("__DATA,__objc_data"), used)) \
const struct dispatch_lane_extra_vtable_s _OS_dispatch_queue_pthread_root_vtable
 = { 
    .do_kind        = queue_pthread_root, 
    .do_type        = DISPATCH_QUEUE_PTHREAD_ROOT_TYPE,
    .do_dispose     = _dispatch_pthread_root_queue_dispose,
    .do_debug       = _dispatch_queue_debug,
    .do_invoke      = _dispatch_object_no_invoke,
    .dq_activate    = _dispatch_queue_no_activate,
    .dq_wakeup      = _dispatch_root_queue_wakeup,
    .dq_push        = _dispatch_root_queue_push
 };
 
复制代码

DISPATCH_VTABLE_SUBCLASS_INSTANCE - queue_mgr

DISPATCH_VTABLE_SUBCLASS_INSTANCE(queue_mgr, lane,
    .do_type        = DISPATCH_QUEUE_MGR_TYPE,
    .do_dispose     = _dispatch_object_no_dispose,
    .do_debug       = _dispatch_queue_debug,
    .do_invoke      = _dispatch_mgr_thread,
    .dq_activate    = _dispatch_queue_no_activate,
    .dq_wakeup      = _dispatch_mgr_queue_wakeup,
    .dq_push        = _dispatch_mgr_queue_push,
);

__attribute__((section("__DATA,__objc_data"), used)) \
const struct dispatch_lane_extra_vtable_s _OS_dispatch_queue_mgr_vtable
 = { 
    .do_kind        = queue_mgr, 
    .do_type        = DISPATCH_QUEUE_MGR_TYPE,
    .do_dispose     = _dispatch_object_no_dispose,
    .do_debug       = _dispatch_queue_debug,
    .do_invoke      = _dispatch_mgr_thread,
    .dq_activate    = _dispatch_queue_no_activate,
    .dq_wakeup      = _dispatch_mgr_queue_wakeup,
    .dq_push        = _dispatch_mgr_queue_push
 };
 
复制代码

DISPATCH_VTABLE_SUBCLASS_INSTANCE - queue_main

DISPATCH_VTABLE_SUBCLASS_INSTANCE(queue_main, lane,
    .do_type        = DISPATCH_QUEUE_MAIN_TYPE,
    .do_dispose     = _dispatch_lane_dispose,
    .do_debug       = _dispatch_queue_debug,
    .do_invoke      = _dispatch_lane_invoke,
    .dq_activate    = _dispatch_queue_no_activate,
    .dq_wakeup      = _dispatch_main_queue_wakeup,
    .dq_push        = _dispatch_main_queue_push,
);

__attribute__((section("__DATA,__objc_data"), used)) \
const struct dispatch_lane_extra_vtable_s _OS_dispatch_queue_main_vtable
 = { 
    .do_kind        = queue_main, 
    .do_type        = DISPATCH_QUEUE_MAIN_TYPE,
    .do_dispose     = _dispatch_lane_dispose,
    .do_debug       = _dispatch_queue_debug,
    .do_invoke      = _dispatch_lane_invoke,
    .dq_activate    = _dispatch_queue_no_activate,
    .dq_wakeup      = _dispatch_main_queue_wakeup,
    .dq_push        = _dispatch_main_queue_push
 };
 
复制代码

DISPATCH_VTABLE_SUBCLASS_INSTANCE - queue_runloop

DISPATCH_VTABLE_SUBCLASS_INSTANCE(queue_runloop, lane,
    .do_type        = DISPATCH_QUEUE_RUNLOOP_TYPE,
    .do_dispose     = _dispatch_runloop_queue_dispose,
    .do_debug       = _dispatch_queue_debug,
    .do_invoke      = _dispatch_lane_invoke,
    .dq_activate    = _dispatch_queue_no_activate,
    .dq_wakeup      = _dispatch_runloop_queue_wakeup,
    .dq_push        = _dispatch_lane_push,
);

__attribute__((section("__DATA,__objc_data"), used)) \
const struct dispatch_lane_extra_vtable_s _OS_dispatch_queue_runloop_vtable
 = { 
    .do_kind        = queue_runloop, 
    .do_type        = DISPATCH_QUEUE_RUNLOOP_TYPE,
    .do_dispose     = _dispatch_runloop_queue_dispose,
    .do_debug       = _dispatch_queue_debug,
    .do_invoke      = _dispatch_lane_invoke,
    .dq_activate    = _dispatch_queue_no_activate,
    .dq_wakeup      = _dispatch_runloop_queue_wakeup,
    .dq_push        = _dispatch_lane_push
 };
 
复制代码

DISPATCH_VTABLE_INSTANCE source

DISPATCH_VTABLE_INSTANCE(source,
    .do_type        = DISPATCH_SOURCE_KEVENT_TYPE,
    .do_dispose     = _dispatch_source_dispose,
    .do_debug       = _dispatch_source_debug,
    .do_invoke      = _dispatch_source_invoke,
    .dq_activate    = _dispatch_source_activate,
    .dq_wakeup      = _dispatch_source_wakeup,
    .dq_push        = _dispatch_lane_push,
);

__attribute__((section("__DATA,__objc_data"), used)) \
const struct dispatch_source_extra_vtable_s _OS_dispatch_source_vtable
 = {
    .do_kind = source,
    .do_type        = DISPATCH_SOURCE_KEVENT_TYPE,
    .do_dispose     = _dispatch_source_dispose,
    .do_debug       = _dispatch_source_debug,
    .do_invoke      = _dispatch_source_invoke,
    .dq_activate    = _dispatch_source_activate,
    .dq_wakeup      = _dispatch_source_wakeup,
    .dq_push        = _dispatch_lane_push
};
复制代码

DISPATCH_VTABLE_INSTANCE channel

DISPATCH_VTABLE_INSTANCE(channel,
    .do_type        = DISPATCH_CHANNEL_TYPE,
    .do_dispose     = _dispatch_channel_dispose,
    .do_debug       = _dispatch_channel_debug,
    .do_invoke      = _dispatch_channel_invoke,
    .dq_activate    = _dispatch_lane_activate,
    .dq_wakeup      = _dispatch_channel_wakeup,
    .dq_push        = _dispatch_lane_push,
);

__attribute__((section("__DATA,__objc_data"), used)) \
const struct dispatch_channel_extra_vtable_s _OS_dispatch_channel_vtable
 = {
    .do_kind = channel,
    .do_type        = DISPATCH_CHANNEL_TYPE,
    .do_dispose     = _dispatch_channel_dispose,
    .do_debug       = _dispatch_channel_debug,
    .do_invoke      = _dispatch_channel_invoke,
    .dq_activate    = _dispatch_lane_activate,
    .dq_wakeup      = _dispatch_channel_wakeup,
    .dq_push        = _dispatch_lane_push
};
复制代码

DISPATCH_VTABLE_INSTANCE mach

DISPATCH_VTABLE_INSTANCE(mach,
    .do_type        = DISPATCH_MACH_CHANNEL_TYPE,
    .do_dispose     = _dispatch_mach_dispose,
    .do_debug       = _dispatch_mach_debug,
    .do_invoke      = _dispatch_mach_invoke,
    .dq_activate    = _dispatch_mach_activate,
    .dq_wakeup      = _dispatch_mach_wakeup,
    .dq_push        = _dispatch_lane_push,
);

__attribute__((section("__DATA,__objc_data"), used)) \
const struct dispatch_mach_extra_vtable_s _OS_dispatch_mach_vtable
 = {
    .do_kind = mach,
    .do_type        = DISPATCH_MACH_CHANNEL_TYPE,
    .do_dispose     = _dispatch_mach_dispose,
    .do_debug       = _dispatch_mach_debug,
    .do_invoke      = _dispatch_mach_invoke,
    .dq_activate    = _dispatch_mach_activate,
    .dq_wakeup      = _dispatch_mach_wakeup,
    .dq_push        = _dispatch_lane_push
};
复制代码

struct dispatch_queue_static_s

struct dispatch_queue_static_s { 
    struct dispatch_lane_s _as_dl[0]; \
    struct dispatch_queue_s _as_dq[0]; \ 
    struct dispatch_object_s _as_do[0]; \
    struct _os_object_s _as_os_obj[0]; \
    const struct dispatch_lane_vtable_s \
        *__ptrauth_objc_isa_pointer do_vtable; \
    int volatile do_ref_cnt; \
    int volatile do_xref_cnt) \
    struct dispatch_lane_s *volatile do_next; \
    struct dispatch_queue_s *do_targetq; \
    void *do_ctxt; \
    union { \
        void *do_finalizer; \
        void *do_introspection_ctxt; \
    } \
    struct dispatch_object_s *volatile dq_items_tail; \ 
    DISPATCH_UNION_LE(uint64_t volatile dq_state, \ 
    dispatch_lock dq_state_lock, \ uint32_t dq_state_bits \
    )
    /* LP64 global queue cacheline boundary */ \
    unsigned long dq_serialnum; \
    const char *dq_label; \
    DISPATCH_UNION_LE(uint32_t volatile dq_atomic_flags, \
        const uint16_t dq_width, \
        const uint16_t __dq_opaque2 \
    ); \
    dispatch_priority_t dq_priority; \
    union { \
        struct dispatch_queue_specific_head_s *dq_specific_head; \
        struct dispatch_source_refs_s *ds_refs; \
        struct dispatch_timer_source_refs_s *ds_timer_refs; \
        struct dispatch_mach_recv_refs_s *dm_recv_refs; \
        struct dispatch_channel_callbacks_s const *dch_callbacks; \
    }; \
    int volatile dq_sref_cnt;
    dispatch_unfair_lock_s dq_sidelock; \ 
    struct dispatch_object_s *volatile dq_items_head; \ 
    uint32_t dq_side_suspend_cnt; 
} DISPATCH_CACHELINE_ALIGN;

复制代码

宏 - _DISPATCH_OBJECT_HEADER

#define _DISPATCH_OBJECT_HEADER(lane) \  
    struct _os_object_s _as_os_obj[0]; \
    const struct dispatch_lane_vtable_s \
        *__ptrauth_objc_isa_pointer do_vtable; \
    int volatile do_ref_cnt; \
    int volatile do_xref_cnt) \
    struct dispatch_lane_s *volatile do_next; \
    struct dispatch_queue_s *do_targetq; \
    void *do_ctxt; \
    union { \
        void *do_finalizer; \
        void *do_introspection_ctxt; \
    }
    
复制代码

宏 - DISPATCH_OBJECT_HEADER

#define DISPATCH_OBJECT_HEADER(x) \   x: lane / queue
    struct dispatch_object_s _as_do[0]; \
    struct _os_object_s _as_os_obj[0]; \
    const struct dispatch_lane_vtable_s \
        *__ptrauth_objc_isa_pointer do_vtable; \
    int volatile do_ref_cnt; \
    int volatile do_xref_cnt) \
    struct dispatch_lane_s *volatile do_next; \
    struct dispatch_queue_s *do_targetq; \
    void *do_ctxt; \
    union { \
        void *do_finalizer; \
        void *do_introspection_ctxt; \
    }
    
复制代码

宏 - _DISPATCH_QUEUE_CLASS_HEADER

#define _DISPATCH_QUEUE_CLASS_HEADER(x,__pointer_sized_field__)\
    struct dispatch_object_s _as_do[0]; \
    struct _os_object_s _as_os_obj[0]; \
    const struct dispatch_lane_vtable_s \
        *__ptrauth_objc_isa_pointer do_vtable; \
    int volatile do_ref_cnt; \
    int volatile do_xref_cnt) \
    struct dispatch_lane_s *volatile do_next; \
    struct dispatch_queue_s *do_targetq; \
    void *do_ctxt; \
    union { \
        void *do_finalizer; \
        void *do_introspection_ctxt; \
    } \
    __pointer_sized_field__; \ 
    DISPATCH_UNION_LE(uint64_t volatile dq_state, \ 
    dispatch_lock dq_state_lock, \ uint32_t dq_state_bits \ )
    
复制代码

宏 - DISPATCH_QUEUE_CLASS_HEADER

#define DISPATCH_QUEUE_CLASS_HEADER(x, __pointer_sized_field__) \
    struct dispatch_object_s _as_do[0]; \
    struct _os_object_s _as_os_obj[0]; \
    const struct dispatch_lane_vtable_s \
        *__ptrauth_objc_isa_pointer do_vtable; \
    int volatile do_ref_cnt; \
    int volatile do_xref_cnt) \
    struct dispatch_lane_s *volatile do_next; \
    struct dispatch_queue_s *do_targetq; \
    void *do_ctxt; \
    union { \
        void *do_finalizer; \
        void *do_introspection_ctxt; \
    } \
    __pointer_sized_field__; \ 
    DISPATCH_UNION_LE(uint64_t volatile dq_state, \ 
    dispatch_lock dq_state_lock, \ uint32_t dq_state_bits \
    )
    /* LP64 global queue cacheline boundary */ \
    unsigned long dq_serialnum; \
    const char *dq_label; \
    DISPATCH_UNION_LE(uint32_t volatile dq_atomic_flags, \
        const uint16_t dq_width, \
        const uint16_t __dq_opaque2 \
    ); \
    dispatch_priority_t dq_priority; \
    union { \
        struct dispatch_queue_specific_head_s *dq_specific_head; \
        struct dispatch_source_refs_s *ds_refs; \
        struct dispatch_timer_source_refs_s *ds_timer_refs; \
        struct dispatch_mach_recv_refs_s *dm_recv_refs; \
        struct dispatch_channel_callbacks_s const *dch_callbacks; \
    }; \
    int volatile dq_sref_cnt
    
复制代码
分类:
iOS
标签:
收藏成功!
已添加到「」, 点击更改