Add FreeBSD pthread support to NIOLock.#123
Merged
Merged
Conversation
### Motivation: NIOLock's pthread fallback branch (used when neither Windows SRWLOCK nor Darwin os_unfair_lock is available) types LockPrimitive as pthread_mutex_t and initializes pthread_mutexattr_t with a default initializer. Both compile on Linux but fail on FreeBSD/OpenBSD, where pthread_mutex_t and pthread_mutexattr_t are opaque-pointer typedefs that Swift imports as Optional<OpaquePointer>, so the default initializers don't exist. ### Modifications: Mirror the pattern landed in apple/swift-log#387: on FreeBSD/OpenBSD alias LockPrimitive to pthread_mutex_t? and initialize pthread_mutexattr_t via the bitPattern overload. ### Result: NIOLock compiles cleanly on FreeBSD with the official Swift FreeBSD preview toolchain, enabling downstream packages such as swift-nio and hummingbird to build there as well.
max-potapov
marked this pull request as ready for review
May 22, 2026 21:19
This was referenced May 22, 2026
Merged
guoye-zhang
approved these changes
May 22, 2026
FranzBusch
pushed a commit
to swift-server/swift-service-lifecycle
that referenced
this pull request
Jul 14, 2026
### Motivation `ConcurrencyHelpers/Lock` has a pthread fallback branch (taken on every non-Windows / non-Darwin platform) that types `LockPrimitive` as `pthread_mutex_t` and initializes `pthread_mutexattr_t` with a default initializer. Both compile on Linux but fail on FreeBSD / OpenBSD, where those types are opaque-pointer typedefs Swift imports as `Optional<OpaquePointer>` — the no-argument initializers don't exist. This blocks swift-service-lifecycle (and everything that depends on it — Hummingbird, downstream HTTP servers) from compiling on FreeBSD with the official Swift FreeBSD preview toolchain. ### Modifications Mirror the pattern landed in [apple/swift-log#387][log-pr]; same change is being submitted for the sibling NIOLock copies (e.g. [apple/swift-http-types#123][sht-pr]): - On `os(FreeBSD) || os(OpenBSD)` alias `LockPrimitive` to `pthread_mutex_t?`. - In `LockOperations.create(_:)`, wrap `pthread_mutexattr_t()` with `#if os(FreeBSD) || os(OpenBSD)` to use the `bitPattern:` overload. [log-pr]: apple/swift-log#387 [sht-pr]: apple/swift-http-types#123 Touches one file (`Sources/ConcurrencyHelpers/Lock.swift`), +7 lines, all behind FreeBSD/OpenBSD conditionals. No behaviour change on existing platforms. ### Result `swift build` and `swift test` both clean on FreeBSD 15.0 with the official Swift FreeBSD preview toolchain: ``` $ uname -a FreeBSD swiftbuild 15.0-RELEASE FreeBSD 15.0-RELEASE-p9 releng/15.0-n281048-6d536196f1bd GENERIC amd64 $ swift --version Swift version 6.3-dev (LLVM 972b62858355d07, Swift 3f8c798cfe25bbb) Target: x86_64-unknown-freebsd14.3 Build config: +assertions $ swift build Building for debugging... ... 276 modules ... Build complete! (42.62s) $ swift test ... ClosureServiceTests passed ... ✔ Test run passed. ``` Without this change `ServiceLifecycle` fails to compile on the same toolchain. ### Checks - [x] Builds on Linux/Darwin (no change there, all changes guarded behind `#if os(FreeBSD) || os(OpenBSD)`). - [x] Builds + passes existing tests on FreeBSD 15.0 with Swift 6.3-dev preview toolchain. - [x] Same pattern as [apple/swift-log#387][log-pr] / [apple/swift-http-types#123][sht-pr].
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
NIOLock's pthread fallback branch (used when neither WindowsSRWLOCKnor Darwinos_unfair_lockis available) typesLockPrimitiveaspthread_mutex_tand initializespthread_mutexattr_twith a default initializer. Both compile on Linuxbut fail on FreeBSD / OpenBSD, where
pthread_mutex_tandpthread_mutexattr_tare opaque-pointer typedefs that Swift imports asOptional<OpaquePointer>, so the no-argument initializers don't exist.This blocks Swift HTTP Types — and anything that transitively depends
on it (Hummingbird, downstream HTTP servers) — from compiling on
FreeBSD with the official Swift FreeBSD preview toolchain.
Modifications
Mirror the pattern already landed in apple/swift-log#387 (and
matching apple/swift-nio's
Sources/NIOConcurrencyHelpers/NIOLock.swiftafter #3494 / #3587):
os(FreeBSD) || os(OpenBSD)aliasLockPrimitivetopthread_mutex_t?so aUnsafeMutablePointer<LockPrimitive>linesup with the
UnsafeMutablePointer<pthread_mutex_t?>parameterpthread_mutex_init/_destroy/_lock/_unlockexpect.LockOperations.create(_:), wrappthread_mutexattr_t()with#if os(FreeBSD) || os(OpenBSD)to use thebitPattern:overloadinstead.
Touches one file (
Sources/HTTPTypes/NIOLock.swift), +6 lines, allbehind FreeBSD/OpenBSD conditionals. No behaviour change on existing
platforms.
Result
swift buildandswift testboth clean on FreeBSD 15.0 with theofficial Swift FreeBSD preview toolchain (
swift_6.3-dev):Without this change every module above
HTTPTypesfails to compile onthe same toolchain — verified by reverting just the
NIOLock.swifthunks and rebuilding.
Checks
behind
#if os(FreeBSD) || os(OpenBSD)).6.3-dev preview toolchain.
swift-log).