12
Core Technologies Overview
OS X Mountain Lion
These types of “closures”—eectively a function pointer plus its invocation context—
are common in dynamically-typed interpreted languages, but they were never before
widely available to C programmers. Apple has published both the Blocks Language
Specication and its implementation as open source under the MIT license and added
blocks support to both GCC and Clang/LLVM.
Queues
GCD dispatch queues are a powerful tool for performing tasks safely and eciently on
multiple CPUs. Dispatch queues atomically add blocks of code that can execute either
asynchronously or synchronously. Serial queues enable mutually exclusive access to
shared data or other resources without the overhead or fragility of locks. Concurrent
queues can execute tasks across multiple distinct threads, based on the number of
currently available CPUs.
Thread pools
The root level of GCD is a set of global concurrent queues for every UNIX process,
each of which is associated with a pool of threads. GCD dequeues blocks and private
queues from the global queues on a rst-in/rst-out (FIFO) basis as long as there are
available threads in the thread pool, providing an easy way to achieve concurrency.
If there is more work than available threads, GCD asks the kernel for more threads,
which are given if there are idle logical processors. Conversely, GCD eventually retires
threads from the pool if they are unused or the system is under excessive load. This
all happens as a side eect of queuing and completing work so that GCD itself doesn’t
require a separate thread. This approach provides optimal thread allocation and CPU
utilization across a wide range of loads.
Event sources
In addition to scheduling blocks directly, GCD makes it easy to run a block in response
to various system events, such as a timer, signal, I/O, or process state change. When
the source res, GCD will schedule the handler block on the specic queue if it is not
currently running, or—more importantly—coalesce pending events if it is running.
This provides excellent responsiveness without the expense of either polling or bind-
ing a thread to the event source. Plus, since the handler is never run more than once
at a time, the block doesn’t even need to be reentrant; only one thread will attempt
to read or write any local variables.
OpenCL integration
Developers traditionally needed to write custom vector code—in addition to their
usual scalar code—in order to take full advantage of modern processors. OpenCL
is an open standard, language, runtime, and framework introduced in OS X 10.6
Snow Leopard. The OpenCL standard makes it straightforward take advantage of the
immense processing power available in GPUs, vector extensions, and multi-core CPUs.
You can use OpenCL to move the most time-consuming routines into computational
“kernels” written in a simple, C-like language. The OpenCL runtime dynamically com-
piles these kernels to take advantage of the type and number of processors available
on a computer. As of OS X 10.7 Lion, the system takes care of autovectorizing kernels
to run eciently on GPUs or CPUs. OpenCL kernels can also be written as separate
les that run as blocks on the GPU or CPU using a special GCD queue.