Files
swift-nio/Sources/NIOConcurrencyHelpers/NIOThreadPoolWorkAvailable.swift
Kenta KuboandCory Benfield 795162f1bc 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>
2026-03-18 10:30:06 +00:00

188 lines
6.7 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2026 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
#if canImport(Darwin)
import Darwin
#elseif os(Windows)
import ucrt
import WinSDK
#elseif canImport(Glibc)
@preconcurrency import Glibc
#elseif canImport(Musl)
@preconcurrency import Musl
#elseif canImport(Bionic)
@preconcurrency import Bionic
#elseif canImport(WASILibc)
@preconcurrency import WASILibc
#if canImport(wasi_pthread)
import wasi_pthread
#endif
#else
#error("The NIOThreadPoolWorkAvailable module was unable to identify your C library.")
#endif
/// A specialized synchronization primitive for ``NIOThreadPool`` that uses
/// `pthread_cond_signal` (wake-one) instead of `pthread_cond_broadcast` (wake-all)
/// when work is enqueued, eliminating the thundering-herd problem.
///
/// This type manages a `workAvailable` counter under a mutex, paired with a
/// condition variable. Threads waiting for work block until `workAvailable > 0`.
@usableFromInline
package struct NIOThreadPoolWorkAvailable: @unchecked Sendable {
@usableFromInline
final class _Storage {
@usableFromInline
let lock: NIOLock
@usableFromInline
var workAvailable: Int
#if os(Windows)
@usableFromInline
let cond: UnsafeMutablePointer<CONDITION_VARIABLE> =
UnsafeMutablePointer.allocate(capacity: 1)
#elseif os(FreeBSD) || os(OpenBSD)
@usableFromInline
let cond: UnsafeMutablePointer<pthread_cond_t?> =
UnsafeMutablePointer.allocate(capacity: 1)
#elseif (compiler(<6.1) && !os(WASI)) || (compiler(>=6.1) && _runtime(_multithreaded))
@usableFromInline
let cond: UnsafeMutablePointer<pthread_cond_t> =
UnsafeMutablePointer.allocate(capacity: 1)
#endif
@usableFromInline
init() {
self.lock = NIOLock()
self.workAvailable = 0
#if os(Windows)
InitializeConditionVariable(self.cond)
#elseif (compiler(<6.1) && !os(WASI)) || (compiler(>=6.1) && _runtime(_multithreaded))
let err = pthread_cond_init(self.cond, nil)
precondition(err == 0, "\(#function) failed in pthread_cond_init with error \(err)")
#endif
}
deinit {
#if os(Windows)
// condition variables do not need to be explicitly destroyed
self.cond.deallocate()
#elseif (compiler(<6.1) && !os(WASI)) || (compiler(>=6.1) && _runtime(_multithreaded))
let err = pthread_cond_destroy(self.cond)
precondition(err == 0, "\(#function) failed in pthread_cond_destroy with error \(err)")
self.cond.deallocate()
#endif
}
}
@usableFromInline
let _storage: _Storage
@usableFromInline
package enum Signal: Sendable {
/// No signal after unlock.
case none
/// Wake one waiting thread (``pthread_cond_signal``).
case signalOne
/// Wake all waiting threads (``pthread_cond_broadcast``).
case broadcastAll
}
@inlinable
package init() {
self._storage = _Storage()
}
/// Lock, run `body`, apply the returned delta to `workAvailable`,
/// unlock, then signal as indicated by the return value.
@inlinable
package func withLock<Result>(
_ body: () -> (workDelta: Int, signal: Signal, result: Result)
) -> Result {
self._storage.lock.lock()
let (workDelta, signal, result) = body()
self._storage.workAvailable += workDelta
self._storage.lock.unlock()
self._signal(signal)
return result
}
/// Lock, run `body`, set `workAvailable` to the returned value if non-nil,
/// unlock, then signal as indicated. Use for shutdown where an
/// absolute value is needed rather than a delta.
@inlinable
package func withLockSettingWorkAvailable<Result>(
_ body: () -> (workAvailable: Int?, signal: Signal, result: Result)
) -> Result {
self._storage.lock.lock()
let (workAvailable, signal, result) = body()
if let workAvailable = workAvailable {
self._storage.workAvailable = workAvailable
}
self._storage.lock.unlock()
self._signal(signal)
return result
}
/// Lock, wait while `workAvailable <= 0`, run `body`, apply the
/// returned delta to `workAvailable`, unlock, then signal as indicated.
@inlinable
package func withLockWaitingForWork<Result>(
_ body: () -> (workDelta: Int, signal: Signal, result: Result)
) -> Result {
self._storage.lock.lock()
while self._storage.workAvailable <= 0 {
self._storage.lock.withLockPrimitive { mutex in
#if os(Windows)
let ok = SleepConditionVariableSRW(self._storage.cond, mutex, INFINITE, 0)
precondition(
ok,
"\(#function) failed in SleepConditionVariableSRW with error \(GetLastError())"
)
#elseif (compiler(<6.1) && !os(WASI)) || (compiler(>=6.1) && _runtime(_multithreaded))
let err = pthread_cond_wait(self._storage.cond, mutex)
precondition(err == 0, "\(#function) failed in pthread_cond_wait with error \(err)")
#endif
}
}
let (workDelta, signal, result) = body()
self._storage.workAvailable += workDelta
self._storage.lock.unlock()
self._signal(signal)
return result
}
@inlinable
func _signal(_ signal: Signal) {
switch signal {
case .none:
break
case .signalOne:
#if os(Windows)
WakeConditionVariable(self._storage.cond)
#elseif (compiler(<6.1) && !os(WASI)) || (compiler(>=6.1) && _runtime(_multithreaded))
let err = pthread_cond_signal(self._storage.cond)
precondition(err == 0, "\(#function) failed in pthread_cond_signal with error \(err)")
#endif
case .broadcastAll:
#if os(Windows)
WakeAllConditionVariable(self._storage.cond)
#elseif (compiler(<6.1) && !os(WASI)) || (compiler(>=6.1) && _runtime(_multithreaded))
let err = pthread_cond_broadcast(self._storage.cond)
precondition(err == 0, "\(#function) failed in pthread_cond_broadcast with error \(err)")
#endif
}
}
}