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的源碼不多,接下來我們要探索兩個方面:
- 子線程創(chuàng)建的時機(jī)點(diǎn)
- 任務(wù)block執(zhí)行的時機(jī)點(diǎn)
1.子線程創(chuàng)建流程
我們先進(jìn)入_dispatch_continuation_init的流程篮奄,看看是否有子線程創(chuàng)建把沼?
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的設(shè)置
image.png
我們發(fā)現(xiàn)_dispatch_continuation_init里面并沒有子線程創(chuàng)建的代碼慈省,接下來我們?nèi)dispatch_continuation_async找找
1.2_dispatch_continuation_async
根據(jù)返回值踩晶,鎖定到了dx_push纽竣。全局搜索dx_push
繼續(xù)看dq_push
如果是并發(fā)隊列,那么.dq_push = _dispatch_lane_concurrent_push,接著來到
_dispatch_lane_concurrent_push
顯然是非柵欄函數(shù)悯衬,那么進(jìn)入_dispatch_continuation_redirect_push
那do_targetq是什么呢肾扰?得回到隊列的創(chuàng)建dispatch_queue_create去查看
那么,_dispatch_continuation_redirect_push里的dx_push時的隊列是_dispatch_get_root_queue()
同理脆荷,找dispatch_queue_global_t 對應(yīng)的 dq_push 的方法
到這里凝垛,我們應(yīng)該知道dispatch_async中子線程創(chuàng)建的調(diào)用流程:
1.dispatch_async --> _dispatch_continuation_async --> dx_push --> dq_push --> 并發(fā)隊列:_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 -->線程池調(diào)度懊悯,創(chuàng)建線程pthread_create
2.任務(wù)Block的調(diào)用流程
那么接下來的問題就是 block何時調(diào)用?
再看哪里調(diào)用的block --> 類似這樣的代碼block(xxx)? 我們可以在dispatch_async的任務(wù)block中打斷點(diǎn),然后bt查看調(diào)用棧
注意到在調(diào)用棧中有一個_dispatch_worker_thread2梦皮,現(xiàn)在重點(diǎn)就來到 --> 什么時候調(diào)起的_dispatch_worker_thread2炭分?
我們先全局搜索一下_dispatch_worker_thread2
發(fā)現(xiàn)全在一個方法里面 --> _dispatch_root_queues_init_once中,同理届氢,全局搜索
再全局搜索_dispatch_root_queues_init
_dispatch_root_queue_poke_slow是否很熟悉欠窒? 就是我們上面在查找創(chuàng)建子線程時調(diào)用棧走過的方法,那么此時任務(wù)block的調(diào)用和子線程的創(chuàng)建產(chǎn)生了聯(lián)系退子,這個聯(lián)系就是_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源碼
接下來繼續(xù)探索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);
}
總之,這個_dispatch_once_gate_tryenter判斷條件寂祥,就能保證當(dāng)前只有一個線程進(jìn)去執(zhí)行代碼荐虐,那為什么只能執(zhí)行一次呢?還是看_dispatch_once_gate_broadcast 里的 _dispatch_once_mark_done
回到Block的調(diào)用時機(jī)
即什么時候調(diào)起的_dispatch_worker_thread2 丸凭?
在_dispatch_root_queues_init時福扬,單例執(zhí)行的任務(wù)block是_dispatch_root_queues_init_once
再來看看_dispatch_root_queues_init_once
上圖可知,在_dispatch_root_queues_init_once中完成了線程與任務(wù)_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()的調(diào)用流程??
1.通過在block任務(wù)中打斷點(diǎn)汽烦,LLDB bt指令查看調(diào)用棧信息,找到_dispatch_worker_thread2莉御;
2.搜索調(diào)用_dispatch_worker_thread2的地方撇吞,找到_dispatch_root_queues_init --> _dispatch_root_queues_init_once;
3.接著我們在_dispatch_root_queues_init_once中發(fā)現(xiàn)了子線程的創(chuàng)建礁叔,并綁定了block任務(wù)_dispatch_worker_thread2牍颈;
4.接著我們繼續(xù)查看_dispatch_worker_thread2的底層源碼,發(fā)現(xiàn)了調(diào)用block任務(wù)的時機(jī)點(diǎn)
dispatch_sync底層
接著看_dispatch_barrier_sync_f的源碼
再回頭看看_dispatch_queue_try_acquire_barrier_sync里的流程