swift-corelibs-libdispatch
repository·main·Indexed 25 days ago
https://github.com/swiftlang/swift-corelibs-libdispatchA portable implementation of the Grand Central Dispatch (GCD/libdispatch) concurrency framework for non-Darwin Swift platforms. It provides a user-space implementation of the libdispatch API using pthread primitives, specifically targeting Linux, FreeBSD, and Android.
What's inside swift-corelibs-libdispatch
- Grand Central Dispatch (GCD or libdispatch) is a framework that provides support for concurrent code execution on multicore hardware. While natively available on Darwin platforms via a combination of kernel and user-space logic, this project provides a portable version of the libdispatch API for other Swift platforms (such as Linux) using a user-space implementation based on pthread primitives.
Build libdispatch for Linux
mainlibdispatch development is targeted at Ubuntu (versions 14.04, 15.10, and 16.04).
1. Install Dependencies
Install the required system packages using
apt-get:sudo apt-get install cmake ninja-build clang systemtap-sdt-dev libbsd-dev linux-libc-devNote: Requires clang 3.8+ and the gold linker. On older Ubuntu releases, you may need to install
binutils-gold.2. Build and Install
Use
cmakewith the Ninja generator:cmake -G Ninja -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ <path-to-source> ninja ninja installsudo apt-get install cmake ninja-build clang systemtap-sdt-dev libbsd-dev linux-libc-dev cmake -G Ninja -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ <path-to-source> ninja ninja installBuild and Install libdispatch
mainTo build and install libdispatch, please refer to the detailed instructions provided in theINSTALL.mdfile.Run the libdispatch test suite
mainThe libdispatch test suite is C-based and uses the automake testing harness.
To run the default set of tests that are expected to pass, use:
make checkTo run an extended test suite that includes tests which may occasionally fail, you must enable it during the configuration step:
./configure --enable-extended-test-suite make checkmake checkBuild libdispatch for FreeBSD
mainTo build libdispatch for FreeBSD 8.x and 9.x with
clangandblockssupport, usecmakewith the Ninja generator:cmake -G Ninja -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ -DBlocksRuntime_INCLUDE_DIR=/usr/local/include -DBlocksRuntime_LIBRARIES=/usr/local/lib/libBlocksRuntime.so <path-to-source> ninja ninja testcmake -G Ninja -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ -DBlocksRuntime_INCLUDE_DIR=/usr/local/include -DBlocksRuntime_LIBRARIES=/usr/local/lib/libBlocksRuntime.so <path-to-source> ninja ninja testBuild libdispatch for Android
mainBuilding for Android assumes you are on a Linux host and have the Android NDK available. It has been tested against API Level 21.
cmake -G Ninja -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ -DCMAKE_SYSTEM_NAME=Android -DCMAKE_SYSTEM_VERSION=21 -DCMAKE_ANDROID_NDK=<path to android NDK> <path-to-source> ninjacmake -G Ninja -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ -DCMAKE_SYSTEM_NAME=Android -DCMAKE_SYSTEM_VERSION=21 -DCMAKE_ANDROID_NDK=<path to android NDK> <path-to-source> ninjaBuild libdispatch with Swift API support
mainYou can optionally build libdispatch to include a Swift API. The method depends on whether you are building your own Swift toolchain or using a pre-built one.
Using a custom Swift toolchain build script
If you are building your own Swift toolchain from source, use the
swift/utils/build-scriptwith additional arguments:./swift/utils/build-script --libdispatch -- --install-libdispatchUsing a pre-built Swift toolchain
To build libdispatch using an existing toolchain and install it into that toolchain (enabling
import Dispatchin Swift code), use the following configuration:- Generate build files:
sh autogen.sh - Configure with toolchain paths:
./configure --with-swift-toolchain=<PATH_TO_SWIFT_TOOLCHAIN> --prefix=<PATH_TO_SWIFT_TOOLCHAIN> - Compile and install:
makeandmake install
Warning: Once installed into a toolchain, that toolchain cannot be used to compile libdispatch again without running
make uninstallfirst../swift/utils/build-script --libdispatch -- --install-libdispatch- Generate build files:
Install the C-API of libdispatch
mainTo perform an uncustomized installation of the libdispatch C-API, use the standard autoconf workflow:
- Generate build files:
sh autogen.sh - Configure the build:
./configure - Compile:
make - Install:
make install
sh autogen.sh ./configure make make install- Generate build files:
Overview of the dispatch framework
mainThe
dispatchframework (Grand Central Dispatch) provides a mechanism for scheduling blocks for asynchronous and concurrent execution. It is organized around three primary concepts:- Dispatch Queues: The basic units of organization for blocks. The framework provides several default queues, and developers can create custom queues using
dispatch_queue_create. - Dispatch Groups: Used to track the progress of blocks submitted to queues, allowing applications to perform actions once a group of blocks completes. Use
dispatch_group_createto initialize a group. - Dispatch Sources: Functions that monitor underlying system events and automatically submit event handler blocks to dispatch queues.
To use the framework, include the following header:
#include <dispatch/dispatch.h>- Dispatch Queues: The basic units of organization for blocks. The framework provides several default queues, and developers can create custom queues using
Manage groups of asynchronous blocks with Dispatch Groups
mainA dispatch group allows you to associate one or more blocks submitted to dispatch queues for asynchronous invocation. You can use groups to wait for the completion of multiple blocks or to receive a notification when all blocks in a group have finished.Access the main queue and process blocks with dispatch_main
mainThe framework provides a default serial queue for the application, accessible via
dispatch_get_main_queue.Important for POSIX programs: To process blocks submitted to the main queue, you must call
dispatch_mainat the end of yourmainfunction. Note thatdispatch_mainnever returns.Integrate dispatch objects with Objective-C and ARC
mainWhen using an Objective-C or Objective-C++ compiler, dispatch objects are treated as Objective-C types.
- ARC (Automated Reference Counting): If ARC is enabled, dispatch objects are managed by the Objective-C runtime. Explicit calls to
dispatch_retainanddispatch_releasewill cause build errors. - Blocks: The Blocks runtime automatically retains and releases dispatch objects captured by blocks during
Block_copyandBlock_release(e.g., duringdispatch_async). - Retain Cycles: If a dispatch source object is captured by its own handler block, a retain cycle may occur. Break these by using
__weakreferences or by callingdispatch_source_cancelto explicitly release the handler blocks. - Interior Pointers: When using functions that return interior pointers (like
dispatch_data_create_mapordispatch_data_apply), ensure the associated object is not prematurely released. You may need to use theobjc_precise_lifetimeattribute or store the object in a__stronginstance variable.
- ARC (Automated Reference Counting): If ARC is enabled, dispatch objects are managed by the Objective-C runtime. Explicit calls to