Skip to content

Add FreeBSD pthread support to NIOLock.#123

Merged
guoye-zhang merged 1 commit into
apple:mainfrom
max-potapov:freebsd-support
May 22, 2026
Merged

Add FreeBSD pthread support to NIOLock.#123
guoye-zhang merged 1 commit into
apple:mainfrom
max-potapov:freebsd-support

Conversation

@max-potapov

Copy link
Copy Markdown
Contributor

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 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.swift
after #3494 / #3587):

  • On os(FreeBSD) || os(OpenBSD) alias LockPrimitive to
    pthread_mutex_t? so a UnsafeMutablePointer<LockPrimitive> lines
    up with the UnsafeMutablePointer<pthread_mutex_t?> parameter
    pthread_mutex_init / _destroy / _lock / _unlock expect.
  • In LockOperations.create(_:), wrap pthread_mutexattr_t() with
    #if os(FreeBSD) || os(OpenBSD) to use the bitPattern: overload
    instead.

Touches one file (Sources/HTTPTypes/NIOLock.swift), +6 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 (swift_6.3-dev):

$ 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...
... 13 modules ...
Build complete! (13.83s)

$ swift test
... 20 tests, 0 failures ...
✔ Test run with 0 tests in 0 suites passed after 0.001 seconds.

Without this change every module above HTTPTypes fails to compile on
the same toolchain — verified by reverting just the NIOLock.swift
hunks and rebuilding.

Checks

  • Builds on Linux/Darwin (no change there, all changes guarded
    behind #if os(FreeBSD) || os(OpenBSD)).
  • Builds + passes existing tests on FreeBSD 15.0 with Swift
    6.3-dev preview toolchain.
  • Pattern matches apple/swift-log#387 (NIOLock copy in
    swift-log).
  • Patch is concise (6 LOC, single file, single concern).

### 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.
@guoye-zhang
guoye-zhang merged commit abccd1b into apple:main May 22, 2026
52 of 53 checks passed
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].
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🔨 semver/patch No public API change.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants