From 795162f1bc61c1b77cfdf0a489092432ffd5ed50 Mon Sep 17 00:00:00 2001 From: Kenta Kubo <601636+kkebo@users.noreply.github.com> Date: Wed, 18 Mar 2026 19:30:06 +0900 Subject: [PATCH] Add partial support for FreeBSD (#3494) This PR adds support for FreeBSD to the `CNIOSHA1` target and the `NIOConcurrencyHelpers` target. ### Motivation: This PR is partial because the `NIOCore` target still can't be built, and the existing PR #3398 includes almost the same changes too. However, #3398 is a little bit old. Besides, the source code in `NIOConcurrencyHelpers` is used in other repositories now, for example: - https://github.com/apple/swift-log/pull/398 - https://github.com/apple/swift-log/pull/387 was merged before swift-nio is fixed. So I think it's important to fix `NIOConcurrencyHelpers` first, even if the entire swift-nio is yet to be fixed. ### Modifications: - Make `swift build --target NIOConcurrencyHelpers` work on FreeBSD - Make `swift build --target CNIOSHA1` work on FreeBSD ### Result: The following commands finished successfully. ```shell swift build --target NIOConcurrencyHelpers swift build --target CNIOSHA1 ``` Environment: - FreeBSD 15.0-RELEASE-p1 x86_64 - [Swift on FreeBSD Preview toolchain](https://forums.swift.org/t/swift-on-freebsd-preview/83064) ``` Swift version 6.3-dev (LLVM b58b2a34d509492, Swift cf535d8b998d09b) Target: x86_64-unknown-freebsd14.3 Build config: +assertions ``` --------- Co-authored-by: Cory Benfield --- Sources/CNIOSHA1/c_nio_sha1.c | 2 +- Sources/NIOConcurrencyHelpers/NIOLock.swift | 4 ++-- .../NIOThreadPoolWorkAvailable.swift | 2 +- Sources/NIOConcurrencyHelpers/lock.swift | 21 +++++++++++-------- 4 files changed, 16 insertions(+), 13 deletions(-) diff --git a/Sources/CNIOSHA1/c_nio_sha1.c b/Sources/CNIOSHA1/c_nio_sha1.c index 9d545d0ee..eb6fbc2df 100644 --- a/Sources/CNIOSHA1/c_nio_sha1.c +++ b/Sources/CNIOSHA1/c_nio_sha1.c @@ -54,7 +54,7 @@ #endif #ifdef __ANDROID__ #include -#elif defined(__linux__) || defined(__APPLE__) || defined(__wasm32__) || defined(__OpenBSD__) +#elif defined(__linux__) || defined(__APPLE__) || defined(__wasm32__) || defined(__FreeBSD__) || defined(__OpenBSD__) #include #elif defined(_WIN32) || defined(_WIN64) #ifndef LITTLE_ENDIAN diff --git a/Sources/NIOConcurrencyHelpers/NIOLock.swift b/Sources/NIOConcurrencyHelpers/NIOLock.swift index f57c8444b..49ffc3aa2 100644 --- a/Sources/NIOConcurrencyHelpers/NIOLock.swift +++ b/Sources/NIOConcurrencyHelpers/NIOLock.swift @@ -35,7 +35,7 @@ import wasi_pthread #if os(Windows) @usableFromInline typealias LockPrimitive = SRWLOCK -#elseif os(OpenBSD) +#elseif os(FreeBSD) || os(OpenBSD) @usableFromInline typealias LockPrimitive = pthread_mutex_t? #else @@ -53,7 +53,7 @@ extension LockOperations { #if os(Windows) InitializeSRWLock(mutex) - #elseif os(OpenBSD) + #elseif os(FreeBSD) || os(OpenBSD) var attr = pthread_mutexattr_t(bitPattern: 0) pthread_mutexattr_init(&attr) let err = pthread_mutex_init(mutex, &attr) diff --git a/Sources/NIOConcurrencyHelpers/NIOThreadPoolWorkAvailable.swift b/Sources/NIOConcurrencyHelpers/NIOThreadPoolWorkAvailable.swift index 04e332551..54cd3df53 100644 --- a/Sources/NIOConcurrencyHelpers/NIOThreadPoolWorkAvailable.swift +++ b/Sources/NIOConcurrencyHelpers/NIOThreadPoolWorkAvailable.swift @@ -52,7 +52,7 @@ package struct NIOThreadPoolWorkAvailable: @unchecked Sendable { @usableFromInline let cond: UnsafeMutablePointer = UnsafeMutablePointer.allocate(capacity: 1) - #elseif os(OpenBSD) + #elseif os(FreeBSD) || os(OpenBSD) @usableFromInline let cond: UnsafeMutablePointer = UnsafeMutablePointer.allocate(capacity: 1) diff --git a/Sources/NIOConcurrencyHelpers/lock.swift b/Sources/NIOConcurrencyHelpers/lock.swift index 9b1e98093..f698cbb3e 100644 --- a/Sources/NIOConcurrencyHelpers/lock.swift +++ b/Sources/NIOConcurrencyHelpers/lock.swift @@ -43,7 +43,7 @@ public final class Lock { #if os(Windows) fileprivate let mutex: UnsafeMutablePointer = UnsafeMutablePointer.allocate(capacity: 1) - #elseif os(OpenBSD) + #elseif os(FreeBSD) || os(OpenBSD) fileprivate let mutex: UnsafeMutablePointer = UnsafeMutablePointer.allocate(capacity: 1) #elseif (compiler(<6.1) && !os(WASI)) || (compiler(>=6.1) && _runtime(_multithreaded)) @@ -55,20 +55,23 @@ public final class Lock { public init() { #if os(Windows) InitializeSRWLock(self.mutex) - #elseif os(OpenBSD) + #elseif (compiler(<6.1) && !os(WASI)) || (compiler(>=6.1) && _runtime(_multithreaded)) + #if os(FreeBSD) || os(OpenBSD) var attr = pthread_mutexattr_t(bitPattern: 0) + #else + var attr = pthread_mutexattr_t() + #endif var err = pthread_mutexattr_init(&attr) precondition(err == 0, "\(#function) failed in pthread_mutexattr_init with error \(err)") - err = pthread_mutex_init(self.mutex, &attr) - precondition(err == 0, "\(#function) failed in pthread_mutex with error \(err)") - #elseif (compiler(<6.1) && !os(WASI)) || (compiler(>=6.1) && _runtime(_multithreaded)) - var attr = pthread_mutexattr_t() - pthread_mutexattr_init(&attr) debugOnly { + #if os(FreeBSD) || os(OpenBSD) + pthread_mutexattr_settype(&attr, .init(PTHREAD_MUTEX_ERRORCHECK.rawValue)) + #else pthread_mutexattr_settype(&attr, .init(PTHREAD_MUTEX_ERRORCHECK)) + #endif } - let err = pthread_mutex_init(self.mutex, &attr) + err = pthread_mutex_init(self.mutex, &attr) precondition(err == 0, "\(#function) failed in pthread_mutex with error \(err)") // `pthread_mutexattr_t` only lives during init; destroy here instead of deinit. let attrDestroyErr = pthread_mutexattr_destroy(&attr) @@ -149,7 +152,7 @@ public final class ConditionLock { #if os(Windows) private let cond: UnsafeMutablePointer = UnsafeMutablePointer.allocate(capacity: 1) - #elseif os(OpenBSD) + #elseif os(FreeBSD) || os(OpenBSD) private let cond: UnsafeMutablePointer = UnsafeMutablePointer.allocate(capacity: 1) #elseif (compiler(<6.1) && !os(WASI)) || (compiler(>=6.1) && _runtime(_multithreaded))