1 OS_object

OS_object由下面宏OS_OBJECT_DEC_BASE扩展而来:

// 1. os/object.h
OS_OBJECT_DECL_BASE(object, NSObject); // 2. os/object.h
#define OS_OBJECT_DECL_BASE(name, ...) \
@interface OS_OBJECT_CLASS(name) : __VA_ARGS__ \
- (instancetype)init OS_SWIFT_UNAVAILABLE("Unavailable in Swift"); \
@end

扩展之后的结果为:

@interface OS_object : NSObject

- (instancetype)init OS_SWIFT_UNAVAILABLE("Unavailable in Swift");

@end

从扩展的结果来看,OS_object继承自NSObject

OS_object实现部分位于 src/object.m 中:

@implementation OS_OBJECT_CLASS(object)
DISPATCH_UNAVAILABLE_INIT() -(id)retain {
return _os_object_retain(self);
} -(oneway void)release {
return _os_object_release_without_xref_dispose(self);
} -(NSUInteger)retainCount {
return _os_object_retain_count(self);
} #pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-implementations" -(BOOL)retainWeakReference {
return _os_object_retain_weak(self);
} -(BOOL)allowsWeakReference {
return _os_object_allows_weak_reference(self);
} #pragma clang diagnostic pop - (void)_xref_dispose {
return _os_object_release_internal(self);
} @end

OS_OBJECT_CLASS的作用就是给宏参数加一个OS_前缀,因此OS_OBJECT_CLASS(object)扩展之后就是OS_object

GCD 源码为OS_object定义了一个指针类型:

// 1. os/object_private.h
typedef OS_OBJECT_CLASS(object) *_os_object_t;

上面代码中将宏扩展开,就是做了如下定义:

typdef OS_object *_os_object_t;

2 OS_dispatch_object

OS_dispatch_object由宏OS_OBJECT_DECL_CLASS扩展而来:

// 1. dispatch/object.h
OS_OBJECT_DECL_CLASS(dispatch_object); // 2. os/object.h
#define OS_OBJECT_DECL_CLASS(name) \
OS_OBJECT_DECL_SUBCLASS_SWIFT(name, object) // 3. os/object.h
#define OS_OBJECT_DECL_SUBCLASS_SWIFT(name, super) \
OS_EXPORT OS_OBJECT_OBJC_RUNTIME_VISIBLE \
OS_OBJECT_DECL_IMPL_CLASS(name, OS_OBJECT_CLASS(super)) // 4. os/object.h
#define OS_OBJECT_DECL_IMPL_CLASS(name, ...) \
OS_OBJECT_DECL_BASE(name, ## __VA_ARGS__) \
typedef OS_OBJECT_CLASS(name) \
* OS_OBJC_INDEPENDENT_CLASS name##_t

扩展之后的结果为:

@interface OS_dispatch_object : OS_object

- (instancetype)init OS_SWIFT_UNAVAILABLE("Unavailable in Swift");

@end

typedef OS_dispatch_object *dispatch_object_t

从扩展结果看,OS_dispatch_object继承自OS_object

上面注释 3 处的宏OS_OBJECT_OBJC_RUNTIME_VISIBLE 和注释 4 处的宏OS_OBJC_INDEPENDENT_CLASS都是 Clang 编译器的属性,对数据结构本身没有影响,因此没有写出来。比如宏OS_OBJECT_OBJC_RUNTIME_VISIBLE扩展之后就是:

__attribute__((objc_runtime_visible))

OS_dispatch_object的实现位于 src/object.m 中:

@implementation DISPATCH_CLASS(object)
DISPATCH_UNAVAILABLE_INIT() - (NSString *)debugDescription {
Class nsstring = objc_lookUpClass("NSString");
if (!nsstring) return nil;
char buf[2048];
struct dispatch_object_s *obj = (struct dispatch_object_s *)self;
if (dx_vtable(obj)->do_debug) {
dx_debug(obj, buf, sizeof(buf));
} else {
strlcpy(buf, object_getClassName(self), sizeof(buf));
}
NSString *format = [nsstring stringWithUTF8String:"<%s: %s>"];
if (!format) return nil;
return [nsstring stringWithFormat:format, object_getClassName(self), buf];
} #pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wobjc-missing-super-calls"
- (void)dealloc {
return _dispatch_dispose(self);
}
#pragma clang diagnostic pop @end

DISPATCH_CLASS的作用是在宏参数前面加上前缀OS_dispatch_,因此DISPATCH_CLASS(object)扩展之后就是OS_dispatch_object

3 OS_dispatch_queue

OS_dispatch_queue由宏DISPATCH_DECL_FACTORY_CLASS_SWIFT扩展而来:

// 1. dispatch/queue.h
DISPATCH_DECL_FACTORY_CLASS_SWIFT(dispatch_queue, DispatchQueue); // 2. dispatch/object.h
#define DISPATCH_DECL_FACTORY_CLASS_SWIFT(name, swift_name) \
OS_OBJECT_SWIFT_HAS_MISSING_DESIGNATED_INIT DISPATCH_DECL_SWIFT(name, swift_name) // 3. dispatch/object.h
#define DISPATCH_DECL_SWIFT(name, swift_name) DISPATCH_SWIFT_NAME(swift_name) DISPATCH_DECL(name) // 4. dispatch/object.h
#define DISPATCH_DECL(name) OS_OBJECT_DECL_SENDABLE_SUBCLASS_SWIFT(name, dispatch_object) // 5. os/object.h
#define OS_OBJECT_DECL_SENDABLE_SUBCLASS_SWIFT(name, super) \
OS_EXPORT OS_OBJECT_OBJC_RUNTIME_VISIBLE OS_OBJECT_SWIFT_SENDABLE \
OS_OBJECT_DECL_IMPL_CLASS(name, OS_OBJECT_CLASS(super))

扩展之后的结果为:

@interface OS_dispatch_queue : OS_dispatch_object

- (instancetype)init OS_SWIFT_UNAVAILABLE("Unavailable in Swift");

@end

typedef OS_dispatch_queue *dispatch_queue_t;

从扩展结果看OS_dispatch_queue继承自OS_dispatch_object

OS_dispatch_queue的实现部分位于 src/object.m 中:

@implementation DISPATCH_CLASS(queue)
OS_OBJECT_NONLAZY_CLASS_LOAD
DISPATCH_UNAVAILABLE_INIT()
DISPATCH_OBJECT_USES_XREF_DISPOSE() - (NSString *)description {
Class nsstring = objc_lookUpClass("NSString");
if (!nsstring) return nil;
NSString *format = [nsstring stringWithUTF8String:"<%s: %s>"];
if (!format) return nil;
return [nsstring stringWithFormat:format, object_getClassName(self),
dispatch_queue_get_label(self), self];
} - (void)_xref_dispose {
_dispatch_queue_xref_dispose((struct dispatch_queue_s *)self);
[super _xref_dispose];
} @end

DISPATCH_CLASS(queue)扩展之后就是OS_dispatch_queue

4 OS_dispatch_queue_serial

OS_dispatch_queue_serial由宏DISPATCH_DECL_SERIAL_EXECUTOR_SWIFT扩展而来:

// 1. dispatch/queue.h
DISPATCH_DECL_SERIAL_EXECUTOR_SWIFT(dispatch_queue_serial, DispatchSerialQueue); // 2. dispatch/object.h
#define DISPATCH_DECL_SERIAL_EXECUTOR_SWIFT(name, swift_name) \
DISPATCH_DECL_SUBCLASS_SWIFT(name, dispatch_queue, swift_name) // 3. dispatch/object.h
#define DISPATCH_DECL_SUBCLASS_SWIFT(name, base, swift_name) \
DISPATCH_SWIFT_NAME(swift_name) DISPATCH_DECL_SUBCLASS(name, base) // 4. dispatch/object.h
#define DISPATCH_DECL_SUBCLASS(name, base) OS_OBJECT_DECL_SENDABLE_SUBCLASS_SWIFT(name, base)

扩展之后的结果为:

@interface OS_dispatch_queue_serial : OS_dispatch_queue

- (instancetype)init OS_SWIFT_UNAVAILABLE("Unavailable in Swift");

@end

typedef OS_dispatch_queue_searial *dispatch_queue_searial_t;

从扩展结果看,OS_dispatch_queue_serial继承自OS_dispatch_queue

OS_dispatch_queue_serial的实现位于 src/object.m 中:

// 1. src/object.m
DISPATCH_CLASS_IMPL(queue_serial) // 2. src/object.m
#define DISPATCH_CLASS_IMPL(name) \
EMPTY_OS_OBJECT_CLASS_IMPL(DISPATCH_CLASS(name)) // 3. src/object.m
#define EMPTY_OS_OBJECT_CLASS_IMPL(name) \
OS_OBJECT_NONLAZY_CLASS \
@implementation name \
OS_OBJECT_NONLAZY_CLASS_LOAD \
DISPATCH_UNAVAILABLE_INIT() \
@end // 4. src/object_internal.h
#define DISPATCH_UNAVAILABLE_INIT() \
- (instancetype)init { \
DISPATCH_CLIENT_CRASH(0, "-init called directly"); \
return [super init]; \
}

扩展之后的结果为:

@implementation OS_dispatch_queue_serial

- (instancetype)init {
return [super init];
} @end

5 OS_dispatch_queue_concurrent

OS_dispatch_queque_concurrent由宏DISPATCH_DECL_SUBCLASS_SWIFT扩展而来:

// 1. dispatch/queue.h
DISPATCH_DECL_SUBCLASS_SWIFT(dispatch_queue_concurrent, dispatch_queue, DispatchConcurrentQueue);

扩展结果为:

@interface OS_dispatch_queue_concurrent : OS_dispatch_queue

- (instancetype)init OS_SWIFT_UNAVAILABLE("Unavailable in Swift");

@end

typedef OS_dispatch_queue_concurrent *dispatch_queue_concurrent_t;

从扩展结果看,OS_dispatch_queue_concurrent继承自OS_dispatch_queue

OS_dispatch_queue_concurrent的实现位于 src/object.m 中:

DISPATCH_CLASS_IMPL(queue_concurrent)

扩展之后的结果为:

@implementation OS_dispatch_queue_concurrent

- (instancetype)init {
return [super init];
} @end

5 OS_dispatch_queue_main

OS_dispatch_queue_main由宏DISPATCH_DECL_SUBCLASS扩展而来:

// 1. dispatch/queue.h
DISPATCH_DECL_SUBCLASS(dispatch_queue_main, dispatch_queue_serial);

扩展的结果为:

@interface OS_dispatch_queue_main : OS_dispatch_queue_serial

- (instancetype)init OS_SWIFT_UNAVAILABLE("Unavailable in Swift");

@end

typedef OS_dispatch_queue_main *dispatch_queue_main_t;

从扩展结果看,OS_dispatch_queue_main继承自OS_dispatch_queue_serial

OS_dispatch_queue_main的实现部分位于 src/object.m 中:

// 1. src/object.m
DISPATCH_CLASS_IMPL(queue_main)

扩展之后的结果为:

@implementation OS_dispatch_queue_main

- (instancetype)init {
return [super init];
} @end

6 OS_dispatch_queue_global

OS_disaptch_queue_global由宏DISPATCH_DECL_SUBCLASS扩展而来:

// 1. dispatch/queue.h
DISPATCH_DECL_SUBCLASS(dispatch_queue_global, dispatch_queue);

扩展之后的结果为:

@interface OS_dispatch_queue_global : OS_dispatch_queue

- (instancetype)init OS_SWIFT_UNAVAILABLE("Unavailable in Swift");

@end

typedef OS_dispatch_queue_global *dispatch_queue_global_t;

从扩展结果看,OS_dispatch_queue_global继承自OS_dispatch_queue

OS_dispatch_queue_global的实现位于 src/object.m 中:

// 1. src/object.m
DISPATCH_CLASS_IMPL(queue_global)

扩展结果之后为:

@implementation OS_dispatch_queue_global

- (instancetype)init {
return [super init];
} @end

2 OC 协议

2.1 <OS_object>

<OS_object>由宏OS_OBJECT_DECL_PROTOCOL定义:

// 1. os/object_private.h
OS_OBJECT_DECL_PROTOCOL(object, <NSObject>); // 2. os/object.h
#define OS_OBJECT_DECL_PROTOCOL(name, ...) \
@protocol OS_OBJECT_CLASS(name) __VA_ARGS__ \
@end

宏扩展的结果为:

@protocol OS_object <NSObject>

@end

从扩展结果可以知道,<OS_object>继承自<NSObject>

2.2 <OS_dispatch_object>

<OS_dispatch_object>由宏_OS_OBJECT_DECL_PROTOCOL定义:

// 1. src/object_internal.h
_OS_OBJECT_DECL_PROTOCOL(dispatch_object, object); // 2. os/object_private.h
#define _OS_OBJECT_DECL_PROTOCOL(name, super) \
OS_OBJECT_DECL_PROTOCOL(name, <OS_OBJECT_CLASS(super)>)

宏扩展之后的结果为:

@protocol OS_dispatch_object <OS_object>

@end

从扩展结果可以看出,<OS_dispatch_object>继承自<OS_object>

2.3 <OS_dispatch_queue>

<OS_dispatch_queue>由宏DISPATCH_CLASS_DECL定义:

// 1. src/queue_internal.h
DISPATCH_CLASS_DECL(queue, QUEUE); // 2. src/object_internal.h
#define DISPATCH_CLASS_DECL(name, cluster) \
_OS_OBJECT_DECL_PROTOCOL(dispatch_##name, dispatch_object) \
_OS_OBJECT_CLASS_IMPLEMENTS_PROTOCOL(dispatch_##name, dispatch_##name) \
DISPATCH_CLASS_DECL_BARE(name, cluster)

DISPATCH_CLASS_DECL有 3 个宏构成,其中宏DISPATCH_CLASS_DECL_BARE后面再说。

_OS_OBJECT_DECL_PROTOCOL扩展之后的结果为:

@protocol OS_dispatch_queue <OS_dispatch_object>

@end

从扩展结果看,<OS_dispatch_queue>继承自<OS_dispatch_object>

_OS_OBJECT_CLASS_IMPLEMENTS_PROTOCOL的定义如下:

// 1. os/object_private.h
#define _OS_OBJECT_CLASS_IMPLEMENTS_PROTOCOL(name, super) \
OS_OBJECT_CLASS_IMPLEMENTS_PROTOCOL(name, super) // 2. os/object.h
#define OS_OBJECT_CLASS_IMPLEMENTS_PROTOCOL(name, proto) \
OS_OBJECT_CLASS_IMPLEMENTS_PROTOCOL_IMPL( \
OS_OBJECT_CLASS(name), OS_OBJECT_CLASS(proto)) // 3. os/object.h
#define OS_OBJECT_CLASS_IMPLEMENTS_PROTOCOL_IMPL(name, proto) \
@interface name () <proto> \
@end

宏的扩展结果为:

@interface OS_dispatch_queue () <OS_dispatch_queue>

@end

从扩展结果可知,OS_dispatch_queue实现了同名协议<OS_dispatch_queue>

2.4 <OS_dispatch_queue_serial>

<OS_dispatch_queue_serial>由宏DISPATCH_SUBCLASS_DECL定义:

// 1. src/queue_internal.h
DISPATCH_SUBCLASS_DECL(queue_serial, queue_serial_executor, lane); // 2. src/object_internal.h
#define DISPATCH_SUBCLASS_DECL(name, super, ctype) \
_OS_OBJECT_DECL_PROTOCOL(dispatch_##name, dispatch_##super); \
_OS_OBJECT_CLASS_IMPLEMENTS_PROTOCOL(dispatch_##name, dispatch_##name) \
OS_OBJECT_SUBCLASS_DECL(dispatch_##name, dispatch_##ctype)

DISPATCH_SUBCLASS_DECL由 3 个宏构成,其中宏OS_OBJECT_SUBCLASS_DECL后面再说。

_OS_OBJECT_DECL_PROTOCOL _OS_OBJECT_CLASS_IMPLEMENTS_PROTOCOL扩展的结果为:

@protocol OS_dispatch_queue_serial <OS_dispatch_queue_serial_executor>

@end

@interface OS_dispatch_queue_serial () <OS_dispatch_queue_serial>

@end

从扩展结果可以知道,<OS_dispatch_queue_serial>继承自<OS_dispatch_queue_serial_executor>。同时,OS_dispatch_queue_serial实现了同名协议<OS_dispatch_queue_serial>

协议<OS_dispatch_queue_serial_executor>是 GCD 内部的一个协议,同样也有宏DISPATCH_SUBCLASS_DECL定义:

// 1. src/queue_internal.h
DISPATCH_SUBCLASS_DECL(queue_serial_executor, queue, lane);

因此,协议<OS_dispatch_queue_serial_executor>继承自<OS_dispatch_queue>`。

2.5 <OS_dispatch_queue_concurrent>

<OS_dispatch_queue_concurrent>由宏DISPATCH_SUBCLASS_DECL定义:

// 1. src/queue_internal.h
DISPATCH_SUBCLASS_DECL(queue_concurrent, queue, lane);

宏扩展的结果为:

@protocol OS_dispatch_queue_concurrent <OS_dispatch_queue>

@end

@interface OS_dispatch_queue_concurrent () <OS_dispatch_queue_concurrent>

@end

从扩展结果可以知道,<OS_dispatch_queue_concurrent>继承自<OS_dispatch_queue>。同时,OS_dispatch_queue_concurrent实现了同名的<OS_dispatch_queue_concurrent>

2.6 <OS_dispatch_queue_main>

OS_dispatch_queue_main由宏DISPATCH_SUBCLASS_DECL定义:

// 1. src/queue_internal.h
DISPATCH_SUBCLASS_DECL(queue_main, queue_serial, lane);

宏扩展结果为:

@protocol OS_dispatch_queue_main <OS_dispatch_queue_serial>

@end

@interface OS_dispatch_queue_main () <OS_dispatch_queue_main>

@end

从扩展结果可以知道,<OS_dispatch_queue_main>继承自<OS_dispatch_queue_serial>。同时,OS_dispatch_queue_main实现了同名的<OS_dispatch_queue_main>

2.7 <OS_dispatch_queue_global>

<OS_dispatch_queue_global>由宏DISPATCH_SUBCLASS_DECL`定义:

// 1. src/queue_internal.h
DISPATCH_SUBCLASS_DECL(queue_global, queue, lane);

宏扩展的结果为:

@protocol OS_disaptch_queue_global <OS_dispatch_queue>

@end

@interface OS_dispatch_queue_global () <OS_dispatch_queue_global>

@end

从扩展结果可以知道,<OS_dispatch_queue_global>继承自<OS_dispatch_queue>。同时,OS_dispatch_queue_global实现了同名协议<OS_dispatch_queue_global>

3 Struct

3.1 _os_object_s 与 _os_object_vtable_s

_os_object_s定义如下:

// 1. src/object_internal.h
typedef struct _os_object_s {
_OS_OBJECT_HEADER(
const _os_object_vtable_s *__ptrauth_objc_isa_pointer os_obj_isa,
os_obj_ref_cnt,
os_obj_xref_cnt);
} _os_object_s;

_OS_OBJECT_HEADER定义如下:

// 1. os/object_private.h
#define _OS_OBJECT_HEADER(isa, ref_cnt, xref_cnt) \
isa; /* must be pointer-sized and use __ptrauth_objc_isa_pointer */ \
int volatile ref_cnt; \
int volatile xref_cnt

因此,_os_object_s完整的定义如下:

typedef struct _os_object_s {
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;

_os_object_vtable_s的定义如下:

// 1. src/object_internal.h
typedef struct _os_object_vtable_s {
_OS_OBJECT_CLASS_HEADER();
} _os_object_vtable_s;

_OS_OBJECT_CLASS_HEADER定义如下:

// 1. os/object_private.h
// Must match size of compiler-generated OBJC_CLASS structure rdar://10640168
#define _OS_OBJECT_CLASS_HEADER() \
void *_os_obj_objc_class_t[5]

因此,_os_object_vtalbe_s的完整定义为:

typedef struct _os_object_vtable_s {
void *_os_obj_objc_class_t[5];
} _os_object_vtable_s;

3.2 dispatch_object_s 与 dispatch_object_vtable_s

dispatch_object_s定义如下:

// 1. src/object_internal.h
struct dispatch_object_s {
_DISPATCH_OBJECT_HEADER(object);
};

_DISPATCH_OBJECT_HEADER的定义如下:

// 1. src/object_internal.h
#define _DISPATCH_OBJECT_HEADER(x) \
_DISPATCH_OBJECT_HEADER_INTERNAL(x) \
struct dispatch_queue_s *do_targetq; \
void *do_ctxt; \
union { \
dispatch_function_t DISPATCH_FUNCTION_POINTER do_finalizer; \
void *do_introspection_ctxt; \
} // 2. src/object_internal.h
#define _DISPATCH_OBJECT_HEADER(x) \
_DISPATCH_OBJECT_HEADER_INTERNAL(x) \
struct dispatch_queue_s *do_targetq; \
void *do_ctxt; \
union { \
dispatch_function_t DISPATCH_FUNCTION_POINTER do_finalizer; \
void *do_introspection_ctxt; \
} // 3. src/object_internal.h
#define _DISPATCH_OBJECT_HEADER_INTERNAL(x) \
struct _os_object_s _as_os_obj[0]; \
OS_OBJECT_STRUCT_HEADER(dispatch_##x); \
struct dispatch_##x##_s *volatile do_next; // 4. src/object_internal.h
#define OS_OBJECT_STRUCT_HEADER(x) \
_OS_OBJECT_HEADER(\
const struct x##_vtable_s *__ptrauth_objc_isa_pointer do_vtable, \
do_ref_cnt, \
do_xref_cnt)

因此,dispatch_object_s展开的完整结果为:

struct dispatch_object_s {
// 这里就是 _os_object_s 内容
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_ctx;
union {
dispatch_function_t DISPATCH_FUNCTION_POINTER do_finalizer;
void *do_introspection_ctxt;
}
};

从展开的结果可以看到,dispatch_object_s的头部区域就是_os_object_s的内容,可以认为,dispatch_object_s"继承"自_os_object_s

dispatch_object_vtable_s由宏DISPATCH_CLASS_DECL_BARE定义:

// 1. src/object_internal.h
DISPATCH_CLASS_DECL_BARE(object, OBJECT);

DISPATCH_CLASS_DECL_BARE正是组成DISPATCH_CLASS_DECL的第 3 个宏。

DISPATCH_CLASS_DECL_BARE的定义如下:

// 1. src/object_internal.h
#define DISPATCH_CLASS_DECL_BARE(name, cluster) \
OS_OBJECT_CLASS_DECL(dispatch_##name, \
DISPATCH_##cluster##_VTABLE_HEADER(dispatch_##name))

因此,宏DISPATCH_CLASS_DECL_BARE(name, OBJECT)扩展为:

OS_OBJECT_CLASS_DECL(dispatch_object, DISPATCH_OBJECT_VTALBE_HEADER(dispatch_object))

DISPATCH_OBJECT_VTABLE_HEADER的定义如下:

// 1. src/object_internal.h
#define DISPATCH_OBJECT_VTABLE_HEADER(x) \
unsigned long const do_type; \
void DISPATCH_VTABLE_ENTRY(do_dispose)(struct x##_s *, \
bool *allow_free); \
size_t DISPATCH_VTABLE_ENTRY(do_debug)(struct x##_s *, \
char *, size_t); \
void DISPATCH_VTABLE_ENTRY(do_invoke)(struct x##_s *, \
dispatch_invoke_context_t, dispatch_invoke_flags_t) // 2. src/internal.h
#define DISPATCH_VTABLE_ENTRY(op) \
(* __ptrauth(ptrauth_key_process_independent_code, true, \
ptrauth_string_discriminator("dispatch." #op)) const op)

DISPATCH_VTALBE_ENTRY中的__ptrauthPAC指针认证有关,与 GCD 功能无关,扩展结果中可以剔除这些扩展,因此宏DISPATCH_OBJECT_VTABLE_HEADER的扩展结果为:

unsigned 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);

OS_OBJECT_CLASS_DECL的定义如下:

// 1. src/object_internal.h
#define OS_OBJECT_CLASS_DECL(name, ...) \
struct name##_s; \
struct name##_extra_vtable_s { \
__VA_ARGS__; \
}; \
struct name##_vtable_s { \
_OS_OBJECT_CLASS_HEADER(); \
struct name##_extra_vtable_s _os_obj_vtable; \
}; \
OS_OBJECT_EXTRA_VTABLE_DECL(name, name) \
extern const struct name##_vtable_s OS_OBJECT_CLASS_SYMBOL(name) \
__asm__(OS_OBJC_CLASS_RAW_SYMBOL_NAME(OS_OBJECT_CLASS(name))) // 2. os/object_private.h
#define OS_OBJECT_CLASS_SYMBOL(name) OS_##name##_class // 3. os/object_private.h
#define OS_OBJC_CLASS_RAW_SYMBOL_NAME(name) "_OBJC_CLASS_$_" OS_STRINGIFY(name)

上面代码中__VA_ARGS__就是宏DISPATCH_OBJECT_VTABLE_HEADER(dispatch_object)扩展的结果。因此宏DISPATCH_CLASS_DECL_BARE(object, OBJECT)扩展之后的结果为:

struct dispatch_object_s;
struct dispatch_object_extra_vtable_s {
unsigned 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);
};
struct dispatch_object_vtable_s {
void *_os_obj_objc_class_t[5]; struct dispatch_object_extra_vtable_s _os_obj_vtable; };
extern const struct dispatch_object_vtable_s OS_dispatch_object_class __asm__("_OBJC_CLASS_$_OS_dispatch_object");

3.3 dispatch_queue_s 与 dispatch_queue_vtable_s

dispatch_queue_s的定义如下:

// 1. src/queue_internal.h
struct dispatch_queue_s {
DISPATCH_QUEUE_CLASS_HEADER(queue, void *__dq_opaque1);
/* 32bit hole on LP64 */
} DISPATCH_ATOMIC64_ALIGN; // 2. src/queue_internal.h
#define DISPATCH_QUEUE_CLASS_HEADER(x, __pointer_sized_field__) \
_DISPATCH_QUEUE_CLASS_HEADER(x, __pointer_sized_field__); \
/* 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 // 3. src/queue_internal.h
#define _DISPATCH_QUEUE_CLASS_HEADER(x, __pointer_sized_field__) \
DISPATCH_OBJECT_HEADER(x); \
__pointer_sized_field__; \
DISPATCH_UNION_LE(uint64_t volatile dq_state, \
dispatch_lock dq_state_lock, \
uint32_t dq_state_bits \
) // 4. src/object_internal.h
#define DISPATCH_OBJECT_HEADER(x) \
struct dispatch_object_s _as_do[0]; \
_DISPATCH_OBJECT_HEADER(x)

dispatch_queue_s完整的扩展结果如下:

struct disaptch_queue_s {
// 头部 dispatch_object_s 内容
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_ctx;
union {
dispatch_function_t DISPATCH_FUNCTION_POINTER do_finalizer;
void *do_introspection_ctxt;
}; void *__dq_opaque1;
union {
uint64_t volatile dq_state;
struct {
dispatch_lock dq_state_lock;
uint32_t dq_state_bits;
};
};
/* LP64 global queue cacheline boundary */
unsigned long dq_serialnum;
const char *dq_label;
union {
uint32_t volatile dq_atomic_flags;
struct {
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
}

disaptch_queue_vtable_s由宏DISPATCH_CLASS_DECL定义:

// 1. src/queue_internal.h
DISPATCH_CLASS_DECL(queue, QUEUE); // 2. src/object_internal.h
#define DISPATCH_CLASS_DECL(name, cluster) \
_OS_OBJECT_DECL_PROTOCOL(dispatch_##name, dispatch_object) \
_OS_OBJECT_CLASS_IMPLEMENTS_PROTOCOL(dispatch_##name, dispatch_##name) \
DISPATCH_CLASS_DECL_BARE(name, cluster)

具体的说,是构成DISPATCH_CLASS_DECL宏的第 3 个宏DISPATCH_CLASS_BARE定义了dispatch_queue_vtable_s,完整的扩展结果为:

struct dispatch_queue_s;
struct dispatch_queue_extra_vtable_s {
unsigned long const do_type;
void * const do_dispose(struct dispatch_queue_s *, bool *allow_free);
size_t * const do_debug(struct dispatch_queue_s *, char *, size_t);
void * const do_invoke(struct dispatch_queue_s *, dispatch_invoke_context_t, dispatch_qos_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_queue_vtable_s {
void *_os_obj_objc_class_t[5];
struct dispatch_queue_extra_vtable_s _os_obj_vtable;
}
extern const struct dispatch_queue_vtable_s OS_dispatch_queue_class __asm__("_OBJC_CLASS_$_OS_dispatch_queue");

3.4 dispatch_lane_s 与 dispatch_lane_vtable_s

dispatch_lane_s的定义如下:

// 1. src/queue_internal.h
typedef struct dispatch_lane_s {
DISPATCH_LANE_CLASS_HEADER(lane);
/* 32bit hole on LP64 */
} DISPATCH_ATOMIC64_ALIGN *dispatch_lane_t; // 2. src/queue_internal.h
#define DISPATCH_LANE_CLASS_HEADER(x) \
struct dispatch_queue_s _as_dq[0]; \
DISPATCH_QUEUE_CLASS_HEADER(x, \
struct dispatch_object_s *volatile dq_items_tail); \
dispatch_unfair_lock_s dq_sidelock; \
struct dispatch_object_s *volatile dq_items_head; \
uint32_t dq_side_suspend_cnt

扩展之后的完整结果为:

typedef struct dispatch_lane_s {
// 这里是 dispatch_queue_s
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 {
dispatch_function_t DISPATCH_FUNCTION_POINTER do_finalizer;
void *do_introspection_ctxt;
};
struct dispatch_object_s *volatile dq_items_tail;
union {
uint64_t volatile dq_state;
struct {
dispatch_lock dq_state_lock;
uint32_t dq_state_bits;
};
);
/* LP64 global queue cacheline boundary */ \
unsigned long dq_serialnum; \
const char *dq_label; \
union {
uint32_t volatile dq_atomic_flags;
struct {
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_lane_t;

dispatch_lane_vtable_s由宏DISPATCH_CLASS_DECL_BARE定义:

// 1. src/queue_internal.h
DISPATCH_CLASS_DECL_BARE(lane, QUEUE);

dispatch_lane_vtable_s由宏OS_OBJECT_SUBCLASS_DECL定义,完整的展开结果为:

struct dispatch_lane_s;
struct dispatch_lane_extra_vtable_s {
unsigned long const do_type;
void * const do_dispose(struct dispatch_lane_s * bool *allow_free);
size_t * const do_debug(struct dispatch_lane_s *, char *, size_t);
void * const do_invoke(struct dispatch_lane_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_vtable_s {
void *_os_obj_objc_class_t[5];
struct dispatch_lane_extra_vtable_s _os_obj_vtable;
}
extern const struct dispatch_lane_vtable_s OS_dispatch_lane_class __asm__("_OBJC_CLASS_$_OS_dispatch_lane")

dispatch_lane_vtable_s有关的 3 个宏定义如下:

// 1. src/queue_internal.h
DISPATCH_SUBCLASS_DECL(queue_serial, queue_serial_executor, lane);
DISPATCH_SUBCLASS_DECL(queue_main, queue_serial, lane);
DISPATCH_SUBCLASS_DECL(queue_concurrent, queue, lane);
DISPATCH_SUBCLASS_DECL(queue_global, queue, lane); // 2. src/object_internal.h
#define DISPATCH_SUBCLASS_DECL(name, super, ctype) \
_OS_OBJECT_DECL_PROTOCOL(dispatch_##name, dispatch_##super); \
_OS_OBJECT_CLASS_IMPLEMENTS_PROTOCOL(dispatch_##name, dispatch_##name) \
OS_OBJECT_SUBCLASS_DECL(dispatch_##name, dispatch_##ctype) // 3. src/object_internal.h
// define a new subclass used in a cluster
#define OS_OBJECT_SUBCLASS_DECL(name, ctype) \
struct name##_s; \
OS_OBJECT_EXTRA_VTABLE_DECL(name, ctype) \
extern const struct ctype##_vtable_s OS_OBJECT_CLASS_SYMBOL(name) \
__asm__(OS_OBJC_CLASS_RAW_SYMBOL_NAME(OS_OBJECT_CLASS(name)))

之前在第 2 部分没有介绍宏OS_OBJECT_SUBCLASS_DECL,这里继续介绍。

代码注释 1 处 4 个宏展开之后的结果为:

struct dispatch_queue_serial_s;
extern const struct dispatch_lane_vtable_s OS_dispatch_queue_serial_class __asm__("_OBJC_CLASS_$_OS_dispatch_queue_serial"); struct dispatch_queue_main_s;
extern const struct dispatch_lane_vtable_s OS_dispatch_queue_main_class __asm__("_OBJC_CLASS_$_OS_dispatch_queue_main"); struct dispatch_queue_concurrent_s;
extern const struct dispatch_lane_vtable_s OS_dispatch_queue_concurrent_class __asm__("_OBJC_CLASS_$_OS_dispatch_concurrent"); struct dispatch_queue_global_s;
extern const struct dispatch_lane_vtable_s OS_dispatch_queue_global_class __asm__("_OBJC_CLASS_$_OS_dispatch_queue_global");


## 3.6 dispatch_queue_global_s
`dispatch_queue_global_s`定义如下: ```c++
// 1. src/queue_internal.h
struct dispatch_queue_global_s {
DISPATCH_QUEUE_ROOT_CLASS_HEADER(lane);
} DISPATCH_CACHELINE_ALIGN; // 2. src/queue_internal.h
#define DISPATCH_QUEUE_ROOT_CLASS_HEADER(x) \
struct dispatch_queue_s _as_dq[0]; \
DISPATCH_QUEUE_CLASS_HEADER(x, \
struct dispatch_object_s *volatile dq_items_tail); \
int volatile dgq_thread_pool_size; \
struct dispatch_object_s *volatile dq_items_head; \
int volatile dgq_pending;

完整扩展结果如下:

typedef struct dispatch_queue_global_s {
// 这里是 dispatch_queue_s
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 {
dispatch_function_t DISPATCH_FUNCTION_POINTER do_finalizer;
void *do_introspection_ctxt;
};
struct dispatch_object_s *volatile dq_items_tail;
union {
uint64_t volatile dq_state;
struct {
dispatch_lock dq_state_lock;
uint32_t dq_state_bits;
};
);
/* LP64 global queue cacheline boundary */ \
unsigned long dq_serialnum; \
const char *dq_label; \
union {
uint32_t volatile dq_atomic_flags;
struct {
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;
int volatile dgq_thread_pool_size;
struct dispatch_object_s *volatile dq_items_head;
int volatile dgq_pending; }

3.7 dispatch_queue_class_t

dispatch_queue_class_t的定义如下:

// 1. src/internal.h
// Dispatch queue cluster class: type for any dispatch_queue_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;
#ifdef __OBJC__
id<OS_dispatch_queue> _objc_dq;
#endif
} dispatch_queue_class_t DISPATCH_TRANSPARENT_UNION;

GCD Inside: GCD 数据结构的更多相关文章

  1. HDU 5726 GCD 区间GCD=k的个数

    GCD Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submis ...

  2. UVA 1642 Magical GCD(gcd的性质,递推)

    分析:对于区间[i,j],枚举j. 固定j以后,剩下的要比较M_gcd(k,j) = gcd(ak,...,aj)*(j-k+1)的大小, i≤k≤j. 此时M_gcd(k,j)可以看成一个二元组(g ...

  3. UESTC 923 稳住GCD DP + GCD

    定义:dp[i][j] 表示 在前i个数中,使整个gcd值为j时最少取的数个数. 则有方程: gg = gcd(a[i],j) gg == j : 添加这个数gcd不变,不添加,  dp[i][j] ...

  4. UVa 11426 (欧拉函数 GCD之和) GCD - Extreme (II)

    题意: 求sum{gcd(i, j) | 1 ≤ i < j ≤ n} 分析: 有这样一个很有用的结论:gcd(x, n) = i的充要条件是gcd(x/i, n/i) = 1,因此满足条件的x ...

  5. UVa 12716 (GCD == XOR) GCD XOR

    题意: 问整数n以内,有多少对整数a.b满足(1≤b≤a)且gcd(a, b) = xor(a, b) 分析: gcd和xor看起来风马牛不相及的运算,居然有一个比较"神奇"的结论 ...

  6. 【CodeForces 803 C】Maximal GCD(GCD+思维)

    You are given positive integer number n. You should create such strictly increasingsequence of k pos ...

  7. FZU 2224 An exciting GCD problem(GCD种类预处理+树状数组维护)同hdu5869

    题目链接:http://acm.fzu.edu.cn/problem.php?pid=2224 同hdu5869 //#pragma comment(linker, "/STACK:1024 ...

  8. UVA 11827 Maximum GCD【GCD,stringstream】

    这题没什么好说的,但是输入较特别,为此还WA了一次... 题目链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge& ...

  9. luoguP2398 GCD SUM [gcd]

    题目描述 for i=1 to n for j=1 to n sum+=gcd(i,j) 给出n求sum. gcd(x,y)表示x,y的最大公约数. 输入输出格式 输入格式: n 输出格式: sum ...

  10. HDU - 6025 Coprime Sequence(前缀gcd+后缀gcd)

    题意:去除数列中的一个数字,使去除后数列中所有数字的gcd尽可能大. 分析:这个题所谓的Coprime Sequence,就是个例子而已嘛,题目中没有任何语句说明给定的数列所有数字gcd一定为1→_→ ...

随机推荐

  1. Chrome 报错: Unchecked runtime.lastError: Could not establish connection. Receiving end does not exist.

    经检查,是由浏览器中的插件导致的报错. 解决方案: 将该插件移除或关闭

  2. 浏览器中的自动化操作插件:Automa

    相信很多小伙伴跟我一样,每天都有大量基于浏览器的重复操作,比如:查看任务.查看新闻.查看各种每天要关注的内容,甚至可能还需要对其做一些操作.那么这些任务是否有办法自动化执行呢? 今天就给大家推荐一个浏 ...

  3. 论文解读(Moka‑ADA)《Moka‑ADA: adversarial domain adaptation with model‑oriented knowledge adaptation for cross‑domain sentiment analysis》

    Note:[ wechat:Y466551 | 可加勿骚扰,付费咨询 ] 论文信息 论文标题:Moka‑ADA: adversarial domain adaptation  with model‑o ...

  4. 无linux基础也能熟练掌握git的基本操作

    git是一个用来管理项目的工具,它的远程仓库有github.gitee.gitlab代码托管中心,既可以用于个人共享代码,又可以用于团队进行项目的协作与发布,那么我们一起来了解一下git该如何使用~ ...

  5. Django+anaconda(spyder)

    一.搭建django虚拟环境 打开anaconda prompt 输入:conda create -n mydjango_env 判断(y/n):y 查看虚拟环境 conda env list *号表 ...

  6. 推荐几款三维模型OBJ格式轻量化处理工具软件

    推荐几款三维模型OBJ格式轻量化处理工具软件 以下是几款常用的三维模型OBJ格式轻量化处理软件的介绍: 1.MeshLab: MeshLab是一款免费且强大的开源三维模型处理软件,支持多种文件格式,包 ...

  7. 《Python魔法大冒险》003 两个神奇的魔法工具

    魔法师:小鱼,要开始编写魔法般的Python程序,我们首先需要两个神奇的工具:Python解释器和代码编辑器. 小鱼:这两个工具是做什么的? 魔法师:你可以把Python解释器看作是一个魔法棒,只要你 ...

  8. Nginx map 实现时间格式转换

    哈喽大家好,我是咸鱼 最近我们需要把 Nginx 的日志接入到自研的日志采集平台上,但是这个平台只支持 JSON 格式,所以需要把 Nginx 日志格式改成 JSON 格式 例如下面这样的效果 刚开始 ...

  9. 使用 Python ssh 远程登陆服务器的最佳方案

    在使用 Python 写一些脚本的时候,在某些情况下,我们需要频繁登陆远程服务去执行一次命令,并返回一些结果. 在 shell 环境中,我们是这样子做的. sshpass -p ${passwd} s ...

  10. vue2和vue3使用echarts时无数据,怎么显示暂无数据图片或文字

    一开始也经历了用v-if和v-show,v-show的话echarts还会留出暂无数据图片的位置,导致echarts变形,v-if在加载和不加载切换时,dom会获取不到:后来也是在网上找的方法,时间有 ...