12年iOS開發(fā)老人傳授我的底層GCD技術(shù)總結(jié)
引言
今天主要是給大家分享一個致力于iOS開發(fā)12年的老人對于iOS底層GCD的一個技術(shù)總結(jié)。主要內(nèi)容是GCD部分的柵欄函數(shù)底層實(shí)現(xiàn)恳不,信號量和調(diào)度組的應(yīng)用。知識是死的次泽,但是人是有溫情了变秦,最重要是快樂成榜!廢話不多說,干貨時間蹦玫。
調(diào)度組
調(diào)度組最直接的作用就是:控制任務(wù)執(zhí)行順序
調(diào)度組的使用
dispatch_group_create :創(chuàng)建調(diào)度組
dispatch_group_async :進(jìn)組任務(wù)
dispatch_group_notify :進(jìn)組任務(wù)執(zhí)行完畢通知
dispatch_group_wait :進(jìn)組任務(wù)執(zhí)行等待時間
搭配使用
dispatch_group_enter :進(jìn)組
dispatch_group_leave :出組
例子 // 自定義并發(fā)隊列
dispatch_queue_t queue = dispatch_queue_create("superman", DISPATCH_QUEUE_CONCURRENT);
// 創(chuàng)建組
dispatch_group_t group = dispatch_group_create();
// 添加任務(wù)
for (int i = 0; i < 5; i++) {
dispatch_group_async(group, queue, ^{
NSLog(@"開始執(zhí)行任務(wù) -- %d", i);
sleep(2);
NSLog(@"任務(wù)-%d執(zhí)行完畢", i);
});
}
// 添加任務(wù)
for (int i = 0; i < 3; i++) {
dispatch_group_enter(group);
dispatch_async(queue, ^{
NSLog(@"開始執(zhí)行終極任務(wù) -- %d", i);
sleep(1);
NSLog(@"終極任務(wù)-%d執(zhí)行完畢", i);
dispatch_group_leave(group);
});
}
// 任務(wù)執(zhí)行完畢回調(diào)
dispatch_group_notify(group, queue, ^{
NSLog(@"任務(wù)都執(zhí)行完了哦");
});
復(fù)制代碼
\
我們發(fā)現(xiàn) 赎婚, dispatch_group_async = dispatch_group_enter + dispatch_group_leave, 不知你有沒有發(fā)現(xiàn)樱溉,并且 出組 進(jìn)組 搭配不對稱的話挣输,會奔潰掉。并且調(diào)度組是如何來實(shí)現(xiàn)所有的任務(wù)都執(zhí)行完畢再執(zhí)行最后的通知的呢福贞?帶著這些疑問撩嚼,我們看下其內(nèi)部實(shí)現(xiàn)是怎樣的。
dispatch_group_create
dispatch_group_t
dispatch_group_create(void)
{
return _dispatch_group_create_with_count(0);
}
復(fù)制代碼
_dispatch_group_create_with_count
static inline dispatch_group_t
_dispatch_group_create_with_count(uint32_t n)
{
dispatch_group_t dg = _dispatch_object_alloc(DISPATCH_VTABLE(group),
sizeof(struct dispatch_group_s));
dg->do_next = DISPATCH_OBJECT_LISTLESS;
dg->do_targetq = _dispatch_get_default_queue(false);
if (n) {
os_atomic_store2o(dg, dg_bits,
(uint32_t)-n * DISPATCH_GROUP_VALUE_INTERVAL, relaxed);
os_atomic_store2o(dg, do_ref_cnt, 1, relaxed); // <rdar://22318411>
}
return dg;
}
...
#define os_atomic_store2o(p, f, v, m) \
os_atomic_store(&(p)->f, (v), m)
...
#define os_atomic_store(p, v, m) \
atomic_store_explicit(_os_atomic_c11_atomic(p), v, memory_order_##m)
...
#define atomic_store_explicit __c11_atomic_store
-------
在頭文件<stdatomic.h>中定義
| ---------------------------------------------------------------------------
| void atomic_store(volatile A * obj挖帘,需要C);
原子替換`obj`指向的原子變量的值`desired`完丽。該操作是原子寫入操作。
------
復(fù)制代碼
dispatch_group_enter
void
dispatch_group_enter(dispatch_group_t dg)
{
// The value is decremented on a 32bits wide atomic so that the carry
// for the 0 -> -1 transition is not propagated to the upper 32bits.
uint32_t old_bits = os_atomic_sub_orig2o(dg, dg_bits,
DISPATCH_GROUP_VALUE_INTERVAL, acquire);
uint32_t old_value = old_bits & DISPATCH_GROUP_VALUE_MASK;
if (unlikely(old_value == 0)) {
_dispatch_retain(dg); // <rdar://problem/22318411>
}
if (unlikely(old_value == DISPATCH_GROUP_VALUE_MAX)) {
DISPATCH_CLIENT_CRASH(old_bits,
"Too many nested calls to dispatch_group_enter()");
}
}
復(fù)制代碼
dispatch_group_leave
void
dispatch_group_leave(dispatch_group_t dg)
{
// 該值在64位寬的原子上遞增
// the -1 -> 0 轉(zhuǎn)換以原子的方式遞增生成
uint64_t new_state, old_state = os_atomic_add_orig2o(dg, dg_state,
DISPATCH_GROUP_VALUE_INTERVAL, release);
uint32_t old_value = (uint32_t)(old_state & DISPATCH_GROUP_VALUE_MASK);
if (unlikely(old_value == DISPATCH_GROUP_VALUE_1)) {
old_state += DISPATCH_GROUP_VALUE_INTERVAL;
do {
new_state = old_state;
if ((old_state & DISPATCH_GROUP_VALUE_MASK) == 0) {
new_state &= ~DISPATCH_GROUP_HAS_WAITERS;
new_state &= ~DISPATCH_GROUP_HAS_NOTIFS;
} else {
// If the group was entered again since the atomic_add above,
// we can't clear the waiters bit anymore as we don't know for
// which generation the waiters are for
new_state &= ~DISPATCH_GROUP_HAS_NOTIFS;
}
if (old_state == new_state) break;
} while (unlikely(!os_atomic_cmpxchgv2o(dg, dg_state,
old_state, new_state, &old_state, relaxed)));
// 喚醒的是 notify
return _dispatch_group_wake(dg, old_state, true);
}
if (unlikely(old_value == 0)) {
DISPATCH_CLIENT_CRASH((uintptr_t)old_value,
"Unbalanced call to dispatch_group_leave()");
}
}
...
#define DISPATCH_GROUP_GEN_MASK 0xffffffff00000000ULL
#define DISPATCH_GROUP_VALUE_MASK 0x00000000fffffffcULL
#define DISPATCH_GROUP_VALUE_INTERVAL 0x0000000000000004ULL
#define DISPATCH_GROUP_VALUE_1 DISPATCH_GROUP_VALUE_MASK
#define DISPATCH_GROUP_VALUE_MAX DISPATCH_GROUP_VALUE_INTERVAL
#define DISPATCH_GROUP_HAS_NOTIFS 0x0000000000000002ULL
#define DISPATCH_GROUP_HAS_WAITERS 0x0000000000000001ULL
復(fù)制代碼
最后拇舀,如果 old_value 和 DISPATCH_GROUP_VALUE_1 相等逻族,
最后會喚醒 notify 的執(zhí)行。
dispatch_group_async
void
dispatch_group_async(dispatch_group_t dg, dispatch_queue_t dq,
dispatch_block_t db)
{
dispatch_continuation_t dc = _dispatch_continuation_alloc();
uintptr_t dc_flags = DC_FLAG_CONSUME | DC_FLAG_GROUP_ASYNC;
dispatch_qos_t qos;
qos = _dispatch_continuation_init(dc, dq, db, 0, dc_flags);
_dispatch_continuation_group_async(dg, dq, dc, qos);
}
復(fù)制代碼
代碼執(zhí)行來到了_dispatch_continuation_group_async
_dispatch_continuation_group_async
在這里調(diào)用了 dispatch_group_enter()
static inline void
_dispatch_continuation_group_async(dispatch_group_t dg, dispatch_queue_t dq,
dispatch_continuation_t dc, dispatch_qos_t qos)
{
dispatch_group_enter(dg);
dc->dc_data = dg;
_dispatch_continuation_async(dq, dc, qos, dc->dc_flags);
}
復(fù)制代碼
那么骄崩,在調(diào)用完block執(zhí)行后聘鳞,才會有 leave 的調(diào)用所以薄辅,我們在自定義并發(fā)隊列執(zhí)行的最后 找到了leave:
static inline void
_dispatch_continuation_with_group_invoke(dispatch_continuation_t dc)
{
struct dispatch_object_s *dou = dc->dc_data;
unsigned long type = dx_type(dou);
if (type == DISPATCH_GROUP_TYPE) {
_dispatch_client_callout(dc->dc_ctxt, dc->dc_func);
_dispatch_trace_item_complete(dc);
dispatch_group_leave((dispatch_group_t)dou);
} else {
DISPATCH_INTERNAL_CRASH(dx_type(dou), "Unexpected object type");
}
}
復(fù)制代碼
這也就解釋了為什么dispatch_group_async 使用后
有 dispatch_group_enter + dispatch_group_leave 的效果。
\
總結(jié):作為十幾年的老碼農(nóng)搁痛,以上都是他自己的一些經(jīng)驗和感想长搀,歡迎大家點(diǎn)
評指正,也希望能夠?qū)γ刃掠兴鶐椭Φ洹m槺闱笠徊P(guān)注,需要學(xué)習(xí)資料
和咨詢編程問題的同學(xué)枪芒,可以點(diǎn)擊領(lǐng)取資料