Preface
The analysis focuses on these group APIs:
dispatch_group_createdispatch_group_enterdispatch_group_leavedispatch_group_waitdispatch_group_notifydispatch_group_async
Related API Analysis
dispatch_group_create
This was mentioned once in the opening article, mainly when analyzing
the essence of dispatch_group_s. We learned that
dispatch_group_t is essentially a semaphore whose value is
LONG_MAX. So it is easy to imagine that semaphore behavior
is inseparable from the analysis of dispatch_group_t.
1 | dispatch_group_t |
The low-level implementation of semaphores is analyzed in a separate article.
dispatch_group_enter
1 | void dispatch_group_enter(dispatch_group_t dg){ |
First, dispatch_group_t is converted into
dispatch_semaphore_t, and then
dispatch_semaphore_wait is called.
1 | long dispatch_semaphore_wait(dispatch_semaphore_t dsema, dispatch_time_t timeout){ |
This function atomically decrements the semaphore value. If it is
greater than or equal to 0, a resource is available and it returns
directly. Otherwise it calls _dispatch_semaphore_wait_slow,
waiting for a signal in the FIFO queue until timeout. For
details about _dispatch_semaphore_wait_slow, see the
dispatch_semaphore article.
Call stack:
1 | dispatch_group_enter |
dispatch_group_leave
1 | void dispatch_group_leave(dispatch_group_t dg){ |
This indicates that one block task in the group has completed, and
sends a wake-up signal when necessary. In
dispatch_group_leave, the group’s semaphore is incremented
by 1. Then it checks whether the current semaphore value is equal to the
original group semaphore value. If they are equal, it means the tasks
are complete, and the wake-up flow _dispatch_group_wake
begins.
1 | long _dispatch_group_wake(dispatch_semaphore_t dsema){ |
_dispatch_group_wake mainly does two things:
- It wakes semaphores through
semaphore_signal. - It calls
dispatch_async_fin sequence.
Call stack:
1 | dispatch_group_leave |
dispatch_group_wait
1 | long dispatch_group_wait(dispatch_group_t dg, dispatch_time_t timeout) |
1 | DISPATCH_NOINLINE |
Call stack:
1 | dispatch_group_wait |
dispatch_group_notify
1 | void dispatch_group_notify(dispatch_group_t dg, dispatch_queue_t dq, dispatch_block_t db){ |
1 | void dispatch_group_notify_f(dispatch_group_t dg, dispatch_queue_t dq, void *ctxt, void (*func)(void *)){ |
Call stack:
1 | dispatch_group_notify |
dispatch_group_async
First, use this pseudocode to understand the execution flow of
dispatch_group_async intuitively:
1 | /// Pseudocode, for understanding only. |
The actual source code:
1 | void dispatch_group_async(dispatch_group_t dg, dispatch_queue_t dq, dispatch_block_t db){ |
dispatch_group_async is a wrapper around
dispatch_group_async_f.
Inside dispatch_group_async_f, the main operations
are:
- Call
dispatch_group_enterto manage the semaphore reference count. - Create a
dispatch_continuation_tto record the group the block belongs to, its context, invocation function, and related information. - Call
_dispatch_queue_pushto enqueue thedispatch_continuation_t.
Do you still remember the implementation of
dispatch_async_f?
1 | void dispatch_async_f(dispatch_queue_t dq, void *ctxt, dispatch_function_t func) |
You can see that dispatch_group_async_f and
dispatch_async_f are highly similar. The main difference is
that dispatch_group_async_f calls
dispatch_group_enter first.
At this point, we have only seen the first half of the pseudocode. Where does the second half happen?
It actually corresponds to the logic inside
_dispatch_continuation_pop. Pay attention to the last few
lines. This article will not analyze
_dispatch_continuation_pop in detail; this is only for
understanding.
1 | static inline void _dispatch_continuation_pop(dispatch_object_t dou) |
This function, including _dispatch_queue_push and
_dispatch_continuation_pop, was already analyzed in the
dispatch_queue article, so the details are not repeated
here.
Summary
Many functions called in the group article overlap with the queue article. For repeated parts, refer to the analysis in the queue article.