iOS底層GCD (技術總結)
// 自定義併發佇列
dispatch_queue_t queue = dispatch_queue_create("superman", DISPATCH_QUEUE_CONCURRENT);
// 建立組
dispatch_group_t group = dispatch_group_create();
// 新增任務
for (int i = 0; i < 5; i++) {
dispatch_group_async(group, queue, ^{
NSLog(@"開始執行任務 -- %d", i);
sleep(2);
NSLog(@"任務-%d執行完畢", i);
});
}
// 新增任務
for (int i = 0; i < 3; i++) {
dispatch_group_enter(group);
dispatch_async(queue, ^{
NSLog(@"開始執行終極任務 -- %d", i);
sleep(1);
NSLog(@"終極任務-%d執行完畢", i);
dispatch_group_leave(group);
});
}
// 任務執行完畢回撥
dispatch_group_notify(group, queue, ^{
NSLog(@"任務都執行完了哦");
});
滑動顯示更多
dispatch_group_tdispatch_group_create(void){ return _dispatch_group_create_with_count(0);}
滑動顯示更多
_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`。該操作是原子寫入操作。
------
滑動顯示更多
dispatch_group_enter
voiddispatch_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()"); }}
滑動顯示更多
dispatch_group_leave
void
dispatch_group_leave(dispatch_group_t dg)
{
// 該值在64位寬的原子上遞增
// the -1 -> 0 轉換以原子的方式遞增生成
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
滑動顯示更多
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);
}
滑動顯示更多
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);}
滑動顯示更多
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"); }}
滑動顯示更多
原文作者:阿華的12年
連結:
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/70005484/viewspace-2788766/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- iOS底層面試總結iOS面試
- iOS底層原理總結 – RunLoopiOSOOP
- iOS底層原理總結 - RunLoopiOSOOP
- iOS底層原理總結 - Category的本質iOSGo
- iOS底層原理總結 - 探尋KVO本質iOS
- iOS GCD知識點總結iOSGC
- iOS底層原理總結 -- 利用Runtime原始碼 分析Category的底層實現iOS原始碼Go
- iOS底層原理總結--OC物件的本質(二)iOS物件
- iOS底層原理總結--OC物件的本質(一)iOS物件
- iOS底層原理總結 - 探尋Class的本質iOS
- iOS底層原理總結 - 關聯物件實現原理iOS物件
- iOS底層原理總結 – 探尋Runtime本質(三)iOS
- iOS底層原理總結 – 探尋OC物件的本質iOS物件
- iOS底層原理總結 – 探尋Runtime本質(二)iOS
- iOS底層原理總結 - 探尋Runtime本質(四)iOS
- iOS底層原理總結 - 探尋Runtime本質(一)iOS
- iOS底層原理總結 - 探尋Runtime本質(二)iOS
- iOS底層原理總結 - 探尋block的本質(二)iOSBloC
- iOS底層原理總結 - 探尋OC物件的本質iOS物件
- iOS底層原理總結 - 探尋Runtime本質(三)iOS
- iOS底層原理總結 - 探尋block的本質(一)iOSBloC
- iOS底層原理 多執行緒之GCD 看我就夠了 --(10)iOS執行緒GC
- docker架構和底層技術Docker架構
- JPA 底層的智慧化技術
- AIGC底層技術介紹AIGC
- iOS 多執行緒:『GCD』詳盡總結iOS執行緒GC
- iOS多執行緒:『GCD』詳盡總結iOS執行緒GC
- iOS刨根問底-深入理解GCDiOSGC
- Redis-技術專區-幫從底層徹底吃透RDB技術原理Redis
- Redis-技術專區-幫從底層徹底吃透AOF技術原理Redis
- 1區塊鏈底層技術-序區塊鏈
- iOS底層原理總結篇-- 深入理解 KVC\KVO 實現機制iOS
- iOS底層原理-CategoryiOSGo
- iOS大中分術歌-神奇的多執行緒GCD技術iOS執行緒GC
- SQL Server底層架構技術對比SQLServer架構
- GCD 學習總結GC
- iOS底層原理總結–instance、class、meta-calss物件的isa和superclassiOS物件
- iOS底層原理總結--instance、class、meta-calss物件的isa和superclassiOS物件