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 <lukasa@apple.com>
This commit is contained in:
Kenta KuboandCory Benfield authored and GitHub committed 2026-03-18 10:30:06 +00:00
1 parent 0e7d4e9534
commit 795162f1bc
4 files changed
+16 -13

No files matched your search

+1 -1
View File
@@ -54,7 +54,7 @@
#endif
#ifdef __ANDROID__
#include <sys/endian.h>
#elif defined(__linux__) || defined(__APPLE__) || defined(__wasm32__) || defined(__OpenBSD__)
#elif defined(__linux__) || defined(__APPLE__) || defined(__wasm32__) || defined(__FreeBSD__) || defined(__OpenBSD__)
#include <sys/types.h>
#elif defined(_WIN32) || defined(_WIN64)
#ifndef LITTLE_ENDIAN
+2 -2
View File
@@ -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)
@@ -52,7 +52,7 @@ package struct NIOThreadPoolWorkAvailable: @unchecked Sendable {
@usableFromInline
let cond: UnsafeMutablePointer<CONDITION_VARIABLE> =
UnsafeMutablePointer.allocate(capacity: 1)
#elseif os(OpenBSD)
#elseif os(FreeBSD) || os(OpenBSD)
@usableFromInline
let cond: UnsafeMutablePointer<pthread_cond_t?> =
UnsafeMutablePointer.allocate(capacity: 1)
+12 -9
View File
@@ -43,7 +43,7 @@ public final class Lock {
#if os(Windows)
fileprivate let mutex: UnsafeMutablePointer<SRWLOCK> =
UnsafeMutablePointer.allocate(capacity: 1)
#elseif os(OpenBSD)
#elseif os(FreeBSD) || os(OpenBSD)
fileprivate let mutex: UnsafeMutablePointer<pthread_mutex_t?> =
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<T: Equatable> {
#if os(Windows)
private let cond: UnsafeMutablePointer<CONDITION_VARIABLE> =
UnsafeMutablePointer.allocate(capacity: 1)
#elseif os(OpenBSD)
#elseif os(FreeBSD) || os(OpenBSD)
private let cond: UnsafeMutablePointer<pthread_cond_t?> =
UnsafeMutablePointer.allocate(capacity: 1)
#elseif (compiler(<6.1) && !os(WASI)) || (compiler(>=6.1) && _runtime(_multithreaded))