iOS底层-GCD底层原理
dispatch_async底层
#ifdef __BLOCKS__
void
dispatch_async(dispatch_queue_t dq, dispatch_block_t work)
{
dispatch_continuation_t dc = _dispatch_continuation_alloc();
uintptr_t dc_flags = DC_FLAG_CONSUME;
dispatch_qos_t qos;
qos = _dispatch_continuation_init(dc, dq, work, 0, dc_flags);
_dispatch_continuation_async(dq, dc, qos, dc->dc_flags);
}
#endif
dispatch_async的源码不多,接下来我们要探索两个方面:
- 子线程创建的时机点
- 任务block执行的时机点
1.子线程创建流程
我们先进入_dispatch_continuation_init的流程,看看是否有子线程创建?
1.1_dispatch_continuation_init
接着来到_dispatch_continuation_init_f
-
_dispatch_continuation_init_f
image.png -
_dispatch_continuation_voucher_set
image.png -
_dispatch_continuation_priority_set --> dc的dc_priority的设置
image.png
我们发现_dispatch_continuation_init里面并没有子线程创建的代码,接下来我们去_dispatch_continuation_async找找
1.2_dispatch_continuation_async
根据返回值,锁定到了dx_push。全局搜索dx_push
image.png
继续看dq_push
image.png
如果是并发队列,那么.dq_push = _dispatch_lane_concurrent_push,接着来到
_dispatch_lane_concurrent_push
image.png
显然是非栅栏函数,那么进入_dispatch_continuation_redirect_push
image.png
那do_targetq是什么呢?得回到队列的创建dispatch_queue_create去查看
image.png
image.png
image.png
那么,_dispatch_continuation_redirect_push里的dx_push时的队列是_dispatch_get_root_queue()
image.png
同理,找dispatch_queue_global_t 对应的 dq_push 的方法
image.png
image.png
image.png image.png
image.png
到这里,我们应该知道dispatch_async中子线程创建的调用流程:
1.dispatch_async --> _dispatch_continuation_async --> dx_push --> dq_push --> 并发队列:_dispatch_lane_concurrent_push --> _dispatch_continuation_redirect_push
2._dispatch_continuation_redirect_push --> dx_push(此时是global_queue) -->_dispatch_root_queue_push --> _dispatch_root_queue_push_inline-->_dispatch_root_queue_poke-->_dispatch_root_queue_poke_slow -->线程池调度,创建线程pthread_create
2.任务Block的调用流程
那么接下来的问题就是 block何时调用?
再看哪里调用的block --> 类似这样的代码block(xxx)? 我们可以在dispatch_async的任务block中打断点,然后bt查看调用栈
image.png
注意到在调用栈中有一个_dispatch_worker_thread2,现在重点就来到 --> 什么时候调起的_dispatch_worker_thread2?
我们先全局搜索一下_dispatch_worker_thread2
image.png
发现全在一个方法里面 --> _dispatch_root_queues_init_once中,同理,全局搜索
image.png
再全局搜索_dispatch_root_queues_init
image.png
_dispatch_root_queue_poke_slow是否很熟悉? 就是我们上面在查找创建子线程时调用栈走过的方法,那么此时任务block的调用和子线程的创建产生了联系,这个联系就是_dispatch_root_queue_poke_slow
2.1 _dispatch_root_queues_init
DISPATCH_STATIC_GLOBAL(dispatch_once_t _dispatch_root_queues_pred);
DISPATCH_ALWAYS_INLINE
static inline void
_dispatch_root_queues_init(void)
{
dispatch_once_f(&_dispatch_root_queues_pred, NULL,
_dispatch_root_queues_init_once);
}
dispatch_once_f是否有些熟悉?莫非是单例?我们平时写的单例是这样的
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
// input your code
});
搜索一下dispatch_once源码
image.png
接下来继续探索dispatch_once_f
DISPATCH_NOINLINE
void
dispatch_once_f(dispatch_once_t *val, void *ctxt, dispatch_function_t func)
{
dispatch_once_gate_t l = (dispatch_once_gate_t)val;
#if !DISPATCH_ONCE_INLINE_FASTPATH || DISPATCH_ONCE_USE_QUIESCENT_COUNTER
uintptr_t v = os_atomic_load(&l->dgo_once, acquire);
if (likely(v == DLOCK_ONCE_DONE)) {
return;
}
#if DISPATCH_ONCE_USE_QUIESCENT_COUNTER
if (likely(DISPATCH_ONCE_IS_GEN(v))) {
return _dispatch_once_mark_done_if_quiesced(l, v);
}
#endif
#endif
if (_dispatch_once_gate_tryenter(l)) {
return _dispatch_once_callout(l, ctxt, func);
}
return _dispatch_once_wait(l);
}
image.png
image.png
image.png image.png
总之,这个_dispatch_once_gate_tryenter判断条件,就能保证当前只有一个线程进去执行代码,那为什么只能执行一次呢?还是看_dispatch_once_gate_broadcast 里的 _dispatch_once_mark_done
image.pngimage.png
回到Block的调用时机
即什么时候调起的_dispatch_worker_thread2 ?
在_dispatch_root_queues_init时,单例执行的任务block是_dispatch_root_queues_init_once
再来看看_dispatch_root_queues_init_once
image.png
image.png
image.png
上图可知,在_dispatch_root_queues_init_once中完成了线程与任务_dispatch_worker_thread2的绑定过程。接下来就看看_dispatch_worker_thread2的大致流程
-
_dispatch_worker_thread2底层
image.png
image.png
image.png
最终,我们来到了dx_invoke 和_dispatch_continuation_invoke_inline。
-
_dispatch_continuation_invoke_inline
image.png
image.png
至此,我们跟着底层源码弄清楚了block()的调用流程👇
1.通过在block任务中打断点,LLDB bt指令查看调用栈信息,找到_dispatch_worker_thread2;
2.搜索调用_dispatch_worker_thread2的地方,找到_dispatch_root_queues_init --> _dispatch_root_queues_init_once;
3.接着我们在_dispatch_root_queues_init_once中发现了子线程的创建,并绑定了block任务_dispatch_worker_thread2;
4.接着我们继续查看_dispatch_worker_thread2的底层源码,发现了调用block任务的时机点
dispatch_sync底层
image.png image.png接着看_dispatch_barrier_sync_f的源码
image.png
image.png
再回头看看_dispatch_queue_try_acquire_barrier_sync里的流程
image.png
image.png