Preface
GCD Semaphore
dispatch_semaphore_t
dispatch_semaphore_s is a semaphore object whose
performance is slightly lower than a spin lock. It is used to ensure
safe access to shared resources.
1 | DISPATCH_DECL(dispatch_semaphore); |
semaphore_tis amach_port_tsemaphore.
1 | typedef mach_port_t semaphore_t; |
dispatch_sema_notify_sis defined as follows:
1 | struct dispatch_sema_notify_s { |
It still looks scattered at this point, so let’s keep reading.
dispatch_semaphore_create
This creates a counting semaphore with an initial value. If two
threads need to finish one job together, you can use a semaphore whose
initial value is 0. If you need to manage a limited resource pool, use
an initial value equal to the size of the resource pool. When the
semaphore is no longer used, call dispatch_release to
destroy it and release its memory. Under ARC this is not needed, because
the system releases it automatically.
1 | dispatch_semaphore_t dispatch_semaphore_create(long value){ |
The flow is:
valuemust be greater than or equal to 0.- Allocate memory for a
dispatch_semaphore_sand initialize it to 0. - Point
dsema->do_vtableto_dispatch_semaphore_vtable, which is defined as follows:
1 | const struct dispatch_semaphore_vtable_s _dispatch_semaphore_vtable = { |
As the name suggests, _dispatch_semaphore_dispose
destroys a semaphore:
1 | void _dispatch_semaphore_dispose(dispatch_semaphore_t dsema){ |
dispatch_semaphore_wait
This waits on a semaphore, or decreases the semaphore count. Each call decrements the counter by 1. If the counter becomes less than 0 after the decrement, the current thread blocks until a signal is received.
1 | long dispatch_semaphore_wait(dispatch_semaphore_t dsema, dispatch_time_t timeout){ |
This function atomically decrements the semaphore value. If the value
is greater than or equal to 0, it means a resource is available and the
function returns directly. Otherwise, it calls
_dispatch_semaphore_wait_slow and waits for a signal in the
FIFO queue until the timeout is reached.
1 | static long _dispatch_semaphore_wait_slow(dispatch_semaphore_t dsema, dispatch_time_t timeout){ |
After entering _dispatch_semaphore_wait_slow, the
behavior depends on the timeout type:
DISPATCH_TIME_NOW: check immediately and return without waiting. Ifdsema_valueis less than 0, increment it by 1 and return the timeout signalKERN_OPERATION_TIMED_OUT.
DISPATCH_TIME_FOREVER: wait indefinitely untildsema_valuebecomes greater than 0. Once a signal arrives, it jumps toagainand runs the function flow again.
1 | /* no-timeout code */ |
- Timed wait: wait until either a signal arrives or the timeout expires.
1 | /* timeout code */ |
dispatch_semaphore_signal
This sends a signal, or increases the count of a semaphore. If the
semaphore value before incrementing, dsema_value, is
greater than or equal to 0, no thread is blocked by this semaphore, so
it returns directly.
1 | DISPATCH_NOINLINE long dispatch_semaphore_signal(dispatch_semaphore_t dsema){ |
If the semaphore value before incrementing is less than 0,
_dispatch_semaphore_signal_slow is called. It wakes one
thread waiting in dispatch_semaphore_wait. If multiple
threads are waiting, iOS decides which one to wake based on thread
priority.
1 | DISPATCH_NOINLINE static long _dispatch_semaphore_signal_slow(dispatch_semaphore_t dsema){ |
Summary
GCD semaphores are relatively simple. Just remember these three key
APIs: create, wait, and
signal.