mirror of
https://github.com/apple/swift-nio
synced 2026-11-24 04:37:54 +00:00
This PR gets TCP (Servers) on Windows mostly working. I'll annotate my
PR for clarity.
**The one bug:**
If you open a TCP client connection to a Windows TCP server, it doesn't
read/write yet until a second connection comes in. This happens because
`reregister0` is called after `WSAPoll` is called - but WSAPoll isn't
woken up to know this so it doesn't know the new socket requests
read/writes/...
Also the EventLoop cannot be woken up during `el.execute { .. }` because
`SleepEx` isn't running. WSAPoll doesn't respond to this signal it
seems.
This makes the TCP server functionality not yet usable, but I thought
with this draft we could figure out what I'm missing.
Alternative considered: We can make `WSAPoll` limited to wake up every 1
ms for example, that way we can still get to these events at _some_
point.
---------
Co-authored-by: Cory Benfield <lukasa@apple.com>
Co-authored-by: Fabian Fett <fabianfett@apple.com>
Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Co-authored-by: Oliver Drobnik <oliver@cocoanetics.com>
Co-authored-by: Jake Petroules <jake.petroules@apple.com>
Co-authored-by: George Barnett <gbarnett@apple.com>
Co-authored-by: Gabriele Mondada <gab@ijk.ch>
303 lines
11 KiB
Swift
303 lines
11 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the SwiftNIO open source project
|
|
//
|
|
// Copyright (c) 2017-2014 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 !os(WASI)
|
|
|
|
import NIOConcurrencyHelpers
|
|
|
|
#if os(Linux) || os(FreeBSD) || os(Android)
|
|
import CNIOLinux
|
|
#elseif os(OpenBSD)
|
|
import CNIOOpenBSD
|
|
#elseif os(Windows)
|
|
import WinSDK
|
|
#endif
|
|
|
|
enum LowLevelThreadOperations {
|
|
|
|
}
|
|
|
|
protocol ThreadOps {
|
|
associatedtype ThreadHandle: Sendable
|
|
associatedtype ThreadSpecificKey
|
|
associatedtype ThreadSpecificKeyDestructor
|
|
|
|
static func threadName(_ thread: ThreadHandle) -> String?
|
|
static func run(handle: inout ThreadHandle?, args: Box<NIOThread.ThreadBoxValue>)
|
|
static func isCurrentThread(_ thread: ThreadHandle) -> Bool
|
|
static func compareThreads(_ lhs: ThreadHandle, _ rhs: ThreadHandle) -> Bool
|
|
static var currentThread: ThreadHandle { get }
|
|
static func joinThread(_ thread: ThreadHandle)
|
|
static func allocateThreadSpecificValue(destructor: ThreadSpecificKeyDestructor) -> ThreadSpecificKey
|
|
static func deallocateThreadSpecificValue(_ key: ThreadSpecificKey)
|
|
static func getThreadSpecificValue(_ key: ThreadSpecificKey) -> UnsafeMutableRawPointer?
|
|
static func setThreadSpecificValue(key: ThreadSpecificKey, value: UnsafeMutableRawPointer?)
|
|
}
|
|
|
|
/// A Thread that executes some runnable block.
|
|
///
|
|
/// All methods exposed are thread-safe.
|
|
@usableFromInline
|
|
final class NIOThread: Sendable {
|
|
internal typealias ThreadBoxValue = (body: (NIOThread) -> Void, name: String?)
|
|
internal typealias ThreadBox = Box<ThreadBoxValue>
|
|
|
|
private let desiredName: String?
|
|
|
|
/// The thread handle used by this instance.
|
|
private let handle: NIOLockedValueBox<ThreadOpsSystem.ThreadHandle?>
|
|
|
|
enum Error: Swift.Error {
|
|
case threadAlreadyJoinedOrDetached
|
|
}
|
|
|
|
deinit {
|
|
assert(
|
|
self.handle.withLockedValue { $0 } == nil,
|
|
"Thread leak! NIOThread released without having been .join()ed"
|
|
)
|
|
}
|
|
|
|
func withHandleUnderLock<Return>(_ body: (ThreadOpsSystem.ThreadHandle) throws -> Return) throws -> Return {
|
|
try self.handle.withLockedValue { handle in
|
|
guard let handle else {
|
|
throw Error.threadAlreadyJoinedOrDetached
|
|
}
|
|
return try body(handle)
|
|
}
|
|
}
|
|
|
|
/// Create a new instance
|
|
///
|
|
/// - arguments:
|
|
/// - handle: The `ThreadOpsSystem.ThreadHandle` that is wrapped and used by the `NIOThread`.
|
|
internal init(handle: ThreadOpsSystem.ThreadHandle, desiredName: String?) {
|
|
self.handle = NIOLockedValueBox(handle)
|
|
self.desiredName = desiredName
|
|
}
|
|
|
|
/// Get current name of the `NIOThread` or `nil` if not set.
|
|
var currentName: String? {
|
|
try? self.withHandleUnderLock { handle in
|
|
ThreadOpsSystem.threadName(handle)
|
|
}
|
|
}
|
|
|
|
static var currentThreadName: String? {
|
|
#if os(Windows)
|
|
ThreadOpsSystem.threadName(.init(handle: GetCurrentThread()))
|
|
#else
|
|
ThreadOpsSystem.threadName(.init(handle: pthread_self()))
|
|
#endif
|
|
}
|
|
|
|
static var currentThreadID: UInt {
|
|
#if os(Windows)
|
|
// Use the OS thread id directly. It is stable for the lifetime of the
|
|
// thread, whereas `currentThread` hands back a fresh `DuplicateHandle`
|
|
// on every call — distinct handle values for the same thread (so not a
|
|
// stable id) that would also leak, since nothing closes them here.
|
|
UInt(GetCurrentThreadId())
|
|
#else
|
|
UInt(bitPattern: .init(bitPattern: ThreadOpsSystem.currentThread.handle))
|
|
#endif
|
|
}
|
|
|
|
@discardableResult
|
|
func takeOwnership() -> ThreadOpsSystem.ThreadHandle {
|
|
try! self.handle.withLockedValue { handle in
|
|
guard let originalHandle = handle else {
|
|
throw Error.threadAlreadyJoinedOrDetached
|
|
}
|
|
handle = nil
|
|
return originalHandle
|
|
}
|
|
}
|
|
|
|
func join() {
|
|
let handle = try! self.withHandleUnderLock { $0 }
|
|
ThreadOpsSystem.joinThread(handle)
|
|
self.handle.withLockedValue { handle in
|
|
precondition(handle != nil, "double NIOThread.join() disallowed")
|
|
handle = nil
|
|
}
|
|
}
|
|
|
|
/// Spawns and runs some task in a `NIOThread`.
|
|
///
|
|
/// - arguments:
|
|
/// - name: The name of the `NIOThread` or `nil` if no specific name should be set.
|
|
/// - body: The function to execute within the spawned `NIOThread`.
|
|
static func spawnAndRun(
|
|
name: String? = nil,
|
|
body: @escaping (NIOThread) -> Void
|
|
) {
|
|
var handle: ThreadOpsSystem.ThreadHandle? = nil
|
|
|
|
// Store everything we want to pass into the c function in a Box so we
|
|
// can hand-over the reference.
|
|
let tuple: ThreadBoxValue = (body: body, name: name)
|
|
let box = ThreadBox(tuple)
|
|
|
|
ThreadOpsSystem.run(handle: &handle, args: box)
|
|
}
|
|
|
|
/// Returns `true` if the calling thread.
|
|
///
|
|
/// - warning: Do not use in the performance path, this takes a lock and is slow.
|
|
@usableFromInline
|
|
var isCurrentSlow: Bool {
|
|
(try? self.withHandleUnderLock { handle in
|
|
ThreadOpsSystem.isCurrentThread(handle)
|
|
}) ?? false
|
|
}
|
|
|
|
internal static func withCurrentThread<Return>(_ body: (NIOThread) throws -> Return) rethrows -> Return {
|
|
let thread = NIOThread(handle: ThreadOpsSystem.currentThread, desiredName: nil)
|
|
defer {
|
|
let handle = thread.takeOwnership()
|
|
#if os(Windows)
|
|
// On Windows `currentThread` gave us an owning HANDLE via
|
|
// `DuplicateHandle`; release it now. We can't `joinThread` it (that
|
|
// would wait on the current thread and deadlock), so close directly.
|
|
CloseHandle(handle.handle)
|
|
#else
|
|
_ = handle
|
|
#endif
|
|
}
|
|
return try body(thread)
|
|
}
|
|
}
|
|
|
|
extension NIOThread: CustomStringConvertible {
|
|
public var description: String {
|
|
let desiredName = self.desiredName
|
|
let actualName = self.currentName
|
|
|
|
switch (desiredName, actualName) {
|
|
case (.some(let desiredName), .some(desiredName)):
|
|
// We know the current, actual name and the desired name and they match. This is hopefully the most common
|
|
// situation.
|
|
return "NIOThread(name = \(desiredName))"
|
|
case (.some(let desiredName), .some(let actualName)):
|
|
// We know both names but they're not equal. That's odd but not impossible, some misbehaved library might
|
|
// have changed the name.
|
|
return "NIOThread(desiredName = \(desiredName), actualName = \(actualName))"
|
|
case (.some(let desiredName), .none):
|
|
// We only know the desired name and can't get the actual thread name. The OS might not be able to provide
|
|
// the name to us.
|
|
return "NIOThread(desiredName = \(desiredName))"
|
|
case (.none, .some(let actualName)):
|
|
// We only know the actual name. This can happen when we don't have a reference to the actually spawned
|
|
// thread but rather ask for the current thread and then print it.
|
|
return "NIOThread(actualName = \(actualName))"
|
|
case (.none, .none):
|
|
// We know nothing, sorry.
|
|
return "NIOThread(n/a)"
|
|
}
|
|
}
|
|
}
|
|
|
|
/// A ``ThreadSpecificVariable`` is a variable that can be read and set like a normal variable except that it holds
|
|
/// different variables per thread.
|
|
///
|
|
/// ``ThreadSpecificVariable`` is thread-safe so it can be used with multiple threads at the same time but the value
|
|
/// returned by ``currentValue`` is defined per thread.
|
|
///
|
|
/// - Note: Though ``ThreadSpecificVariable`` is thread-safe, it is not `Sendable` unless `Value` is `Sendable`.
|
|
/// If ``ThreadSpecificVariable`` were unconditionally `Sendable`, it could be used to "smuggle"
|
|
/// non-`Sendable` state out of an actor or other isolation domain without triggering warnings. If you
|
|
/// are attempting to use ``ThreadSpecificVariable`` with non-`Sendable` data, consider using a dynamic
|
|
/// enforcement tool like `NIOLoopBoundBox` to police the access.
|
|
public final class ThreadSpecificVariable<Value: AnyObject> {
|
|
// the actual type in there is `Box<(ThreadSpecificVariable<T>, T)>` but we can't use that as C functions can't capture (even types)
|
|
private typealias BoxedType = Box<(AnyObject, AnyObject)>
|
|
|
|
internal class Key {
|
|
private var underlyingKey: ThreadOpsSystem.ThreadSpecificKey
|
|
|
|
internal init(destructor: @escaping ThreadOpsSystem.ThreadSpecificKeyDestructor) {
|
|
self.underlyingKey = ThreadOpsSystem.allocateThreadSpecificValue(destructor: destructor)
|
|
}
|
|
|
|
deinit {
|
|
ThreadOpsSystem.deallocateThreadSpecificValue(self.underlyingKey)
|
|
}
|
|
|
|
public func get() -> UnsafeMutableRawPointer? {
|
|
ThreadOpsSystem.getThreadSpecificValue(self.underlyingKey)
|
|
}
|
|
|
|
public func set(value: UnsafeMutableRawPointer?) {
|
|
ThreadOpsSystem.setThreadSpecificValue(key: self.underlyingKey, value: value)
|
|
}
|
|
}
|
|
|
|
private let key: Key
|
|
|
|
/// Initialize a new `ThreadSpecificVariable` without a current value (`currentValue == nil`).
|
|
public init() {
|
|
self.key = Key(destructor: {
|
|
Unmanaged<BoxedType>.fromOpaque(($0 as UnsafeMutableRawPointer?)!).release()
|
|
})
|
|
}
|
|
|
|
/// Initialize a new `ThreadSpecificVariable` with `value` for the calling thread. After calling this, the calling
|
|
/// thread will see `currentValue == value` but on all other threads `currentValue` will be `nil` until changed.
|
|
///
|
|
/// - Parameters:
|
|
/// - value: The value to set for the calling thread.
|
|
public convenience init(value: Value) {
|
|
self.init()
|
|
self.currentValue = value
|
|
}
|
|
|
|
/// The value for the current thread.
|
|
@available(
|
|
*,
|
|
noasync,
|
|
message: "threads can change between suspension points and therefore the thread specific value too"
|
|
)
|
|
public var currentValue: Value? {
|
|
get {
|
|
self.get()
|
|
}
|
|
set {
|
|
self.set(newValue)
|
|
}
|
|
}
|
|
|
|
/// Get the current value for the calling thread.
|
|
func get() -> Value? {
|
|
guard let raw = self.key.get() else { return nil }
|
|
// parenthesize the return value to silence the cast warning
|
|
return
|
|
(Unmanaged<BoxedType>
|
|
.fromOpaque(raw)
|
|
.takeUnretainedValue()
|
|
.value.1 as! Value)
|
|
}
|
|
|
|
/// Set the current value for the calling threads. The `currentValue` for all other threads remains unchanged.
|
|
func set(_ newValue: Value?) {
|
|
if let raw = self.key.get() {
|
|
Unmanaged<BoxedType>.fromOpaque(raw).release()
|
|
}
|
|
self.key.set(value: newValue.map { Unmanaged.passRetained(Box((self, $0))).toOpaque() })
|
|
}
|
|
}
|
|
|
|
extension ThreadSpecificVariable: @unchecked Sendable where Value: Sendable {}
|
|
#endif // !os(WASI)
|