Skip to content

feat: add FreeBSD pthread support to NIOLock#170

Open
max-potapov wants to merge 1 commit into
swift-server:mainfrom
max-potapov:freebsd-support
Open

feat: add FreeBSD pthread support to NIOLock#170
max-potapov wants to merge 1 commit into
swift-server:mainfrom
max-potapov:freebsd-support

Conversation

@max-potapov
Copy link
Copy Markdown

Motivation

NIOLock's pthread fallback branch 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 imported as
Optional<OpaquePointer>. Additionally, PTHREAD_MUTEX_ERRORCHECK is
imported as a pthread_mutextype enum case on FreeBSD whose .rawValue
must be used to construct the CInt that pthread_mutexattr_settype
expects.

This blocks swift-prometheus from compiling on FreeBSD with the
official Swift FreeBSD preview toolchain.

Modifications

Mirror the pattern landed in apple/swift-log#387; same fix is
being submitted for the sibling NIOLock copies in
apple/swift-http-types#123 and
swift-server/swift-service-lifecycle:

  • On os(FreeBSD) || os(OpenBSD) alias LockPrimitive to
    pthread_mutex_t?.
  • Initialize pthread_mutexattr_t via the bitPattern: overload.
  • On os(FreeBSD) || os(OpenBSD), access PTHREAD_MUTEX_ERRORCHECK
    via .rawValue before passing to pthread_mutexattr_settype.

Touches one file (Sources/Prometheus/NIOLock.swift), +11 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
... Compiling Prometheus NIOLock.swift ...
Build complete! (15.77s)

$ swift test
... 40 tests, with 0 failures (0 unexpected) ...
✔ Test run passed.

Without this change Prometheus fails to compile on the same
toolchain.

Checks

  • Builds on Linux/Darwin (no change there, all changes guarded
    behind #if os(FreeBSD) || os(OpenBSD)).
  • Builds + passes existing 40 tests on FreeBSD 15.0 with Swift
    6.3-dev preview toolchain.
  • Same pattern as apple/swift-log#387 /
    apple/swift-http-types#123.
  • Commit title follows the project's Conventional Commits
    convention (feat: ...).

### Motivation:

NIOLock's pthread fallback branch 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 imported as
Optional<OpaquePointer>. Additionally, PTHREAD_MUTEX_ERRORCHECK is
imported as a pthread_mutextype enum case on FreeBSD whose .rawValue
must be used to construct the CInt that pthread_mutexattr_settype
expects.

### Modifications:

Mirror the pattern landed in apple/swift-log#387:
- alias LockPrimitive to pthread_mutex_t? on FreeBSD/OpenBSD
- initialize pthread_mutexattr_t via the bitPattern overload
- on FreeBSD/OpenBSD, access PTHREAD_MUTEX_ERRORCHECK via .rawValue
  before passing to pthread_mutexattr_settype

### Result:

swift-prometheus compiles on FreeBSD with the official Swift FreeBSD
preview toolchain.
@max-potapov max-potapov marked this pull request as ready for review May 22, 2026 21:28
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