GCD Inside: GCD 資料結構

chaoguo1234發表於2023-11-13

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;