dispatch_object_t
所有GCD结构体的联合体,可以表示任意GCD对象1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17typedef union {
struct _os_object_s *_os_obj;
struct dispatch_object_s *_do;
struct dispatch_continuation_s *_dc;
struct dispatch_queue_s *_dq;
struct dispatch_queue_attr_s *_dqa;
struct dispatch_group_s *_dg;
struct dispatch_source_s *_ds;
struct dispatch_mach_s *_dm;
struct dispatch_mach_msg_s *_dmsg;
struct dispatch_source_attr_s *_dsa;
struct dispatch_semaphore_s *_dsema;
struct dispatch_data_s *_ddata;
struct dispatch_io_s *_dchannel;
struct dispatch_operation_s *_doperation;
struct dispatch_disk_s *_ddisk;
} dispatch_object_t DISPATCH_TRANSPARENT_UNION;
_os_object_s
OC对象
1 | typedef struct _os_object_vtable_s { |
dispatch_object_s
1 | struct dispatch_object_s; |
dispatch_continuation_s
1 | typedef struct voucher_s { |
dispatch_queue_s
1 | struct dispatch_queue_s { |
dispatch_queue_attr_s
1 | struct dispatch_queue_attr_s { |
dispatch_group_s
1 | struct dispatch_group_s { |
dispatch_source_s
1 | struct dispatch_source_s { |
dispatch_semaphore_s
1 | struct dispatch_semaphore_s { |
dispatch_data_s
1 | typedef void (^dispatch_block_t)(void); |
dispatch_sync_context_s
1 | typedef struct dispatch_sync_context_s { |
dispatch queue
1 | struct dispatch_queue_s _dispatch_root_queues[] = { |
系统维护12个root queue
,可分成两组:DISPATCH_ROOT_QUEUE_IDX_XXX_QOS
和DISPATCH_ROOT_QUEUE_IDX_XXX_QOS_OVERCOMMIT
获取queue
1 | static inline dispatch_queue_t |
main/mgr/root queue init
1 | void libdispatch_init(void) { |
global queue
1 | dispatch_queue_t dispatch_get_global_queue(long priority, unsigned long flags) { |
可以发现global queue
是从root queue contexts
中取出的可以OVERCOMMIT的 queue
main queue
1 | struct dispatch_queue_s _dispatch_main_q = { |
可以发现main queue也是从root queue contexts
中取出的
user queue
1 |
|
dispatch sync/async
dispatch sync
1 | void dispatch_sync(dispatch_queue_t dq, dispatch_block_t work) { |
dispatch_barrier_async会在 flags 加 DISPATCH_BLOCK_BARRIER 值
1 | void dispatch_barrier_sync(dispatch_queue_t dq, dispatch_block_t work) { |
在dispatch wake时执行
1 | void _dispatch_queue_wakeup(dispatch_queue_t dq, dispatch_qos_t qos, |
dispatch async
1 | void dispatch_async(dispatch_queue_t dq, dispatch_block_t work) { |
dispatch_barrier_async 比 dispatch_async 的 dc_flags 多加了 DISPATCH_OBJ_BARRIER_BIT 位
1 | void dispatch_barrier_async(dispatch_queue_t dq, dispatch_block_t work) { |
dispatch semaphore
是对UNIX底层sem的封装
create
1 | dispatch_semaphore_t dispatch_semaphore_create(long value) { |
wait
1 | long dispatch_semaphore_wait(dispatch_semaphore_t dsema, dispatch_time_t timeout) { |
signal
1 | long dispatch_semaphore_signal(dispatch_semaphore_t dsema) { |
dispatch group
dispatch group依靠dispatch semaphore来工作的
create
1 | dispatch_group_t dispatch_group_create(void) { |
group async
1 | void dispatch_group_async(dispatch_group_t dg, dispatch_queue_t dq, |
wait
1 | long dispatch_group_wait(dispatch_group_t dg, dispatch_time_t timeout) { |
signal
1 | static inline void _dispatch_group_notify(dispatch_group_t dg, dispatch_queue_t dq, |
dispatch once
1 | void dispatch_once(dispatch_once_t *val, dispatch_block_t block) { |
dispatch source
常见的例子如下,使用 GCD Timer 的好处在于不依赖 runloop1
2
3
4
5
6dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
dispatch_source_set_timer(timer, dispatch_walltime(NULL, 0), 10*NSEC_PER_SEC, 1*NSEC_PER_SEC); //每10秒触发timer,误差1秒
dispatch_source_set_event_handler(timer, ^{
// 定时器触发时执行的 block
});
dispatch_resume(timer);
create
1 | dispatch_source_t dispatch_source_create(dispatch_source_type_t dst, uintptr_t handle, |
set timer
1 | void dispatch_source_set_timer(dispatch_source_t ds, dispatch_time_t start, |
set event handler
1 | void dispatch_source_set_event_handler(dispatch_source_t ds, |
具体的细节没有找到,没有搞明白source是怎么实现的