Skip to content

Support musl and Android in the pthread executors - #104

Merged
FranzBusch merged 3 commits into
swiftlang:mainfrom
jakepetroules:musl-and-android-support
Nov 12, 2026
Merged

FranzBusch merged 3 commits into
swiftlang:mainfrom
jakepetroules:musl-and-android-support

Conversation

@jakepetroules

Copy link
Copy Markdown
Contributor

On musl, such as with the Static Linux SDK, and on Android, the package fails to build with its default traits:

Sources/PlatformExecutors/PThread/Internal/IO/PlatformIOBackend.swift:15:8: error: Unsupported platform

On Android the C shim and the C library imports fail before that.

musl. The epoll I/O backend, and the eventfd and timerfd wrappers it uses, are selected with canImport(Glibc) alone, so on musl no backend is available. The first commit selects them for Musl as well, the same way most of the package already handles both C libraries, and gives SystemCoreCount's Musl import the @unsafe its Glibc import already has, which strict memory safety otherwise rejects.

Android. The pthread executors are compiled for Android, but no C library is imported there. The second commit:

  • imports the Android module wherever Glibc and Musl are imported outside the ExperimentalIO code, and selects the epoll backend and the pthread PlatformExecutorFactory for Android;
  • marks creating a pthread_attr_t unsafe on Android, since Bionic's holds a pointer;
  • coerces the thread function to whichever pthread_create start-routine signature the NDK declares (NDK 28 made it nullable), the way swift-nio's ThreadPosix.swift does;
  • adds the headers the C shim's Android pthread_getname_np emulation uses.

The io_uring backend stays Glibc-only, and the ExperimentalIO-only files are unchanged.

Tested with the Swift 6.4.2 SDKs, together with #103, since main does not currently build without ExperimentalIO:

  • Static Linux SDK (aarch64-swift-linux-musl): the package builds with its default traits, and a static executable with typealias DefaultExecutorFactory = PlatformExecutorFactory gets PThreadTaskExecutor as its global executor, sized to the cgroup's CPU limit.
  • Android SDK (aarch64-unknown-linux-android28, x86_64-unknown-linux-android28, NDK 30): the package builds with its default traits. I have not run it on a device.
  • glibc (Linux, aarch64): unchanged; builds and runs as before.

I don't have the toolchain the ExperimentalIO trait needs, so the ExperimentalIO paths in the changed files are not covered on musl or Android.

The epoll I/O backend and its eventfd and timerfd wrappers were selected
only for Glibc, so on musl, such as the Static Linux SDK, the build
failed with "Unsupported platform". Select them for Musl as well, and
mark SystemCoreCount's Musl import @unsafe like its Glibc import, which
strict memory safety otherwise rejects.
The pthread executors are compiled for Android, but no C library is
imported there, so the build fails with "unable to identify your C
library". Import the Android module wherever Glibc and Musl are imported
outside the ExperimentalIO code, and select the epoll backend and the
pthread executor factory for it.

Bionic's declarations also need adjusting: its pthread_attr_t holds a
pointer, so creating one is unsafe, and NDK 28 made pthread_create's
start routine nullable, so coerce the thread function to whichever
signature the NDK declares, as swift-nio does. The C shim's
pthread_getname_np emulation for Android was missing the headers it
uses.

@FranzBusch FranzBusch left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should also enable the CI to validate this

@FranzBusch
FranzBusch merged commit 823cec9 into swiftlang:main Nov 12, 2026
37 of 42 checks passed
@jakepetroules
jakepetroules deleted the musl-and-android-support branch November 12, 2026 10:43
@jakepetroules jakepetroules mentioned this pull request Nov 18, 2026
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants