之前已經介紹了dispatch_semaphore
的底層實現,dispatch_group
的實現是基於前者的。在看原始碼之前,我們先看一下我們是如何應用的。假設有這麼場景:有一個A耗時操作,B和C兩個網路請求和一個耗時操作C當ABC都執行完成後,重新整理頁面。我們可以用dispatch_group
實現。關鍵如下:
- (void)viewDidLoad {
[super viewDidLoad];
__block NSInteger number = 0;
dispatch_group_t group = dispatch_group_create();
//A耗時操作
dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
sleep(3);
number += 2222;
});
//B網路請求
dispatch_group_enter(group);
[self sendRequestWithCompletion:^(id response) {
number += [response integerValue];
dispatch_group_leave(group);
}];
//C網路請求
dispatch_group_enter(group);
[self sendRequestWithCompletion:^(id response) {
number += [response integerValue];
dispatch_group_leave(group);
}];
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
NSLog(@"%zd", number);
});
}
- (void)sendRequestWithCompletion:(void (^)(id response))completion {
//模擬一個網路請求
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
sleep(2);
dispatch_async(dispatch_get_main_queue(), ^{
if (completion) completion(@1111);
});
});
}
複製程式碼
接下來我們根據上面的流程來看一下dispatch_group的相關API
dispatch_group_create
dispatch_group_t
dispatch_group_create(void)
{
return (dispatch_group_t)dispatch_semaphore_create(LONG_MAX);
}
複製程式碼
dispatch_group_create
其實就是建立了一個value
為LONG_MAX
的dispatch_semaphore
訊號量
dispatch_group_async
void
dispatch_group_async(dispatch_group_t dg, dispatch_queue_t dq,
dispatch_block_t db)
{
dispatch_group_async_f(dg, dq, _dispatch_Block_copy(db),
_dispatch_call_block_and_release);
}
複製程式碼
dispatch_group_async
只是dispatch_group_async_f
的封裝
dispatch_group_async_f
void
dispatch_group_async_f(dispatch_group_t dg, dispatch_queue_t dq, void *ctxt,
dispatch_function_t func)
{
dispatch_continuation_t dc;
_dispatch_retain(dg);
dispatch_group_enter(dg);
dc = fastpath(_dispatch_continuation_alloc_cacheonly());
if (!dc) {
dc = _dispatch_continuation_alloc_from_heap();
}
dc->do_vtable = (void *)(DISPATCH_OBJ_ASYNC_BIT | DISPATCH_OBJ_GROUP_BIT);
dc->dc_func = func;
dc->dc_ctxt = ctxt;
dc->dc_group = dg;
// No fastpath/slowpath hint because we simply don't know
if (dq->dq_width != 1 && dq->do_targetq) {
return _dispatch_async_f2(dq, dc);
}
_dispatch_queue_push(dq, dc);
}
複製程式碼
從上面的程式碼我們可以看出dispatch_group_async_f
和dispatch_async_f
相似。dispatch_group_async_f
多了dispatch_group_enter(dg);
,另外在do_vtable
的賦值中dispatch_group_async_f
多了一個DISPATCH_OBJ_GROUP_BIT
的標記符。既然新增了dispatch_group_enter
必定會存在dispatch_group_leave
。在之前《深入理解GCD之dispatch_queue》介紹_dispatch_continuation_pop
函式的原始碼中有一段程式碼如下:
_dispatch_client_callout(dc->dc_ctxt, dc->dc_func);
if (dg) {
//group需要進行呼叫dispatch_group_leave並釋放訊號
dispatch_group_leave(dg);
_dispatch_release(dg);
}
複製程式碼
所以dispatch_group_async_f
函式中的dispatch_group_leave
是在_dispatch_continuation_pop
函式中呼叫的。
這裡概括一下dispatch_group_async_f
的工作流程:
- 呼叫
dispatch_group_enter
; - 將block和queue等資訊記錄到
dispatch_continuation_t
結構體中,並將它加入到group的連結串列中; _dispatch_continuation_pop
執行時會判斷任務是否為group,是的話執行完任務再呼叫dispatch_group_leave
以達到訊號量的平衡。
dispatch_group_enter
void
dispatch_group_enter(dispatch_group_t dg)
{
dispatch_semaphore_t dsema = (dispatch_semaphore_t)dg;
(void)dispatch_semaphore_wait(dsema, DISPATCH_TIME_FOREVER);
}
複製程式碼
dispatch_group_enter
將dispatch_group_t
轉換成dispatch_semaphore_t
,並呼叫dispatch_semaphore_wait
,原子性減1後,進入等待狀態直到有訊號喚醒。所以說dispatch_group_enter就是對dispatch_semaphore_wait的封裝。
dispatch_group_leave
void
dispatch_group_leave(dispatch_group_t dg)
{
dispatch_semaphore_t dsema = (dispatch_semaphore_t)dg;
dispatch_atomic_release_barrier();
long value = dispatch_atomic_inc2o(dsema, dsema_value);//dsema_value原子性加1
if (slowpath(value == LONG_MIN)) {//記憶體溢位,由於dispatch_group_leave在dispatch_group_enter之前呼叫
DISPATCH_CLIENT_CRASH("Unbalanced call to dispatch_group_leave()");
}
if (slowpath(value == dsema->dsema_orig)) {//表示所有任務已經完成,喚醒group
(void)_dispatch_group_wake(dsema);
}
}
複製程式碼
從上面的原始碼中我們看到dispatch_group_leave
將dispatch_group_t
轉換成dispatch_semaphore_t
後將dsema_value
的值原子性加1。如果value
為LONG_MIN
程式crash;如果value
等於dsema_orig
表示所有任務已完成,呼叫_dispatch_group_wake
喚醒group(_dispatch_group_wake
的用於和notify有關,我們會在後面介紹)。因為在enter
的時候進行了原子性減1操作。所以在leave
的時候需要原子性加1。
這裡先說明一下enter
和leave
之間的關係:
-
dispatch_group_leave與dispatch_group_enter配對使用。當呼叫了
dispatch_group_enter
而沒有呼叫dispatch_group_leave
時,由於value
不等於dsema_orig
不會走到喚醒邏輯,dispatch_group_notify
中的任務無法執行或者dispatch_group_wait
收不到訊號而卡住執行緒。 -
dispatch_group_enter必須在dispatch_group_leave之前出現。當
dispatch_group_leave
比dispatch_group_enter
多呼叫了一次或者說在dispatch_group_enter
之前被呼叫的時候,dispatch_group_leave
進行原子性加1操作,相當於value
為LONGMAX+1
,發生資料長度溢位,變成LONG_MIN
,由於value == LONG_MIN
成立,程式發生crash。
dispatch_group_notify
void
dispatch_group_notify(dispatch_group_t dg, dispatch_queue_t dq,
dispatch_block_t db)
{
dispatch_group_notify_f(dg, dq, _dispatch_Block_copy(db),
_dispatch_call_block_and_release);
}
複製程式碼
dispatch_group_notify
是dispatch_group_notify_f
的封裝,具體實現在後者。
dispatch_group_notify_f
void
dispatch_group_notify_f(dispatch_group_t dg, dispatch_queue_t dq, void *ctxt,
void (*func)(void *))
{
dispatch_semaphore_t dsema = (dispatch_semaphore_t)dg;
struct dispatch_sema_notify_s *dsn, *prev;
//封裝dispatch_continuation_t結構體
// FIXME -- this should be updated to use the continuation cache
while (!(dsn = calloc(1, sizeof(*dsn)))) {
sleep(1);
}
dsn->dsn_queue = dq;
dsn->dsn_ctxt = ctxt;
dsn->dsn_func = func;
_dispatch_retain(dq);
dispatch_atomic_store_barrier();
//將結構體放到連結串列尾部,如果連結串列為空同時設定連結串列頭部節點並喚醒group
prev = dispatch_atomic_xchg2o(dsema, dsema_notify_tail, dsn);
if (fastpath(prev)) {
prev->dsn_next = dsn;
} else {
_dispatch_retain(dg);
(void)dispatch_atomic_xchg2o(dsema, dsema_notify_head, dsn);
if (dsema->dsema_value == dsema->dsema_orig) {//任務已經完成,喚醒group
_dispatch_group_wake(dsema);
}
}
}
複製程式碼
所以dispatch_group_notify
函式只是用連結串列把所有回撥通知儲存起來,等待呼叫。
_dispatch_group_wake
static long
_dispatch_group_wake(dispatch_semaphore_t dsema)
{
struct dispatch_sema_notify_s *next, *head, *tail = NULL;
long rval;
//將dsema的dsema_notify_head賦值為NULL,同時將之前的內容賦給head
head = dispatch_atomic_xchg2o(dsema, dsema_notify_head, NULL);
if (head) {
// snapshot before anything is notified/woken <rdar://problem/8554546>
//將dsema的dsema_notify_tail賦值為NULL,同時將之前的內容賦給tail
tail = dispatch_atomic_xchg2o(dsema, dsema_notify_tail, NULL);
}
//將dsema的dsema_group_waiters設定為0,並返回原來的值
rval = dispatch_atomic_xchg2o(dsema, dsema_group_waiters, 0);
if (rval) {
//迴圈呼叫semaphore_signal喚醒當初等待group的訊號量,使得dispatch_group_wait函式返回。
// wake group waiters
#if USE_MACH_SEM
_dispatch_semaphore_create_port(&dsema->dsema_waiter_port);
do {
kern_return_t kr = semaphore_signal(dsema->dsema_waiter_port);
DISPATCH_SEMAPHORE_VERIFY_KR(kr);
} while (--rval);
#elif USE_POSIX_SEM
do {
int ret = sem_post(&dsema->dsema_sem);
DISPATCH_SEMAPHORE_VERIFY_RET(ret);
} while (--rval);
#endif
}
if (head) {
//獲取連結串列,依次呼叫dispatch_async_f非同步執行在notify函式中的任務即Block。
// async group notify blocks
do {
dispatch_async_f(head->dsn_queue, head->dsn_ctxt, head->dsn_func);
_dispatch_release(head->dsn_queue);
next = fastpath(head->dsn_next);
if (!next && head != tail) {
while (!(next = fastpath(head->dsn_next))) {
_dispatch_hardware_pause();
}
}
free(head);
} while ((head = next));
_dispatch_release(dsema);
}
return 0;
}
複製程式碼
_dispatch_group_wake
主要的作用有兩個:
-
呼叫semaphore_signal喚醒當初等待group的訊號量,使得dispatch_group_wait函式返回。
-
獲取連結串列,依次呼叫dispatch_async_f非同步執行在notify函式中的任務即Block。
到這裡我們已經差不多知道了dispatch_group
工作過程,我們用一張圖表示:
dispatch_group_wait
long
dispatch_group_wait(dispatch_group_t dg, dispatch_time_t timeout)
{
dispatch_semaphore_t dsema = (dispatch_semaphore_t)dg;
if (dsema->dsema_value == dsema->dsema_orig) {//沒有需要執行的任務
return 0;
}
if (timeout == 0) {//返回超時
#if USE_MACH_SEM
return KERN_OPERATION_TIMED_OUT;
#elif USE_POSIX_SEM
errno = ETIMEDOUT;
return (-1);
#endif
}
return _dispatch_group_wait_slow(dsema, timeout);
}
複製程式碼
dispatch_group_wait
用於等待group中的任務完成。
_dispatch_group_wait_slow
static long
_dispatch_group_wait_slow(dispatch_semaphore_t dsema, dispatch_time_t timeout)
{
long orig;
again:
// check before we cause another signal to be sent by incrementing
// dsema->dsema_group_waiters
if (dsema->dsema_value == dsema->dsema_orig) {
return _dispatch_group_wake(dsema);
}
// Mach semaphores appear to sometimes spuriously wake up. Therefore,
// we keep a parallel count of the number of times a Mach semaphore is
// signaled (6880961).
(void)dispatch_atomic_inc2o(dsema, dsema_group_waiters);
// check the values again in case we need to wake any threads
if (dsema->dsema_value == dsema->dsema_orig) {
return _dispatch_group_wake(dsema);
}
#if USE_MACH_SEM
mach_timespec_t _timeout;
kern_return_t kr;
_dispatch_semaphore_create_port(&dsema->dsema_waiter_port);
// From xnu/osfmk/kern/sync_sema.c:
// wait_semaphore->count = -1; /* we don't keep an actual count */
//
// The code above does not match the documentation, and that fact is
// not surprising. The documented semantics are clumsy to use in any
// practical way. The above hack effectively tricks the rest of the
// Mach semaphore logic to behave like the libdispatch algorithm.
switch (timeout) {
default:
do {
uint64_t nsec = _dispatch_timeout(timeout);
_timeout.tv_sec = (typeof(_timeout.tv_sec))(nsec / NSEC_PER_SEC);
_timeout.tv_nsec = (typeof(_timeout.tv_nsec))(nsec % NSEC_PER_SEC);
kr = slowpath(semaphore_timedwait(dsema->dsema_waiter_port,
_timeout));
} while (kr == KERN_ABORTED);
if (kr != KERN_OPERATION_TIMED_OUT) {
DISPATCH_SEMAPHORE_VERIFY_KR(kr);
break;
}
// Fall through and try to undo the earlier change to
// dsema->dsema_group_waiters
case DISPATCH_TIME_NOW:
while ((orig = dsema->dsema_group_waiters)) {
if (dispatch_atomic_cmpxchg2o(dsema, dsema_group_waiters, orig,
orig - 1)) {
return KERN_OPERATION_TIMED_OUT;
}
}
// Another thread called semaphore_signal().
// Fall through and drain the wakeup.
case DISPATCH_TIME_FOREVER:
do {
kr = semaphore_wait(dsema->dsema_waiter_port);
} while (kr == KERN_ABORTED);
DISPATCH_SEMAPHORE_VERIFY_KR(kr);
break;
}
#elif USE_POSIX_SEM
//這部分程式碼省略
#endif
goto again;
}
複製程式碼
從上面的程式碼我們發現_dispatch_group_wait_slow
和_dispatch_semaphore_wait_slow
的邏輯很接近。都利用mach核心的semaphore進行訊號的傳送。區別在於_dispatch_semaphore_wait_slow
在等待結束後是return,而_dispatch_group_wait_slow
在等待結束是呼叫_dispatch_group_wake
去喚醒這個group。
總結
-
dispatch_group
是一個初始值為LONG_MAX
的訊號量,group中的任務完成是判斷其value
是否恢復成初始值。 -
dispatch_group_enter
和dispatch_group_leave
必須成對使用並且支援巢狀。 -
如果
dispatch_group_enter
比dispatch_group_leave
多,由於value
不等於dsema_orig
不會走到喚醒邏輯,dispatch_group_notify
中的任務無法執行或者dispatch_group_wait
收不到訊號而卡住執行緒。如果是dispatch_group_leave
多,則會引起崩潰。