From dbbdabb24ac6087eebb5f7e6815076cb34fca33d Mon Sep 17 00:00:00 2001 From: David Nadoba Date: Mon, 9 May 2022 16:31:36 +0200 Subject: [PATCH] Adopt `Sendable` for Types in `ChannelOptions` Namespace (#2098) * Adopt `Sendable` for Types in `ChannelOptions` Namespace * mark `ChannelOption` as @preconcurrecy Sendable and introduce `NIOPreconcurrencySendable` --- Sources/NIOCore/ChannelOption.swift | 34 ++++++++++++++++------------- Sources/NIOCore/NIOSendable.swift | 6 +++++ 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/Sources/NIOCore/ChannelOption.swift b/Sources/NIOCore/ChannelOption.swift index d84ef0673..4e6b8cc66 100644 --- a/Sources/NIOCore/ChannelOption.swift +++ b/Sources/NIOCore/ChannelOption.swift @@ -13,7 +13,7 @@ //===----------------------------------------------------------------------===// /// A configuration option that can be set on a `Channel` to configure different behaviour. -public protocol ChannelOption: Equatable { +public protocol ChannelOption: Equatable, NIOPreconcurrencySendable { /// The type of the `ChannelOption`'s value. associatedtype Value } @@ -69,7 +69,7 @@ extension ChannelOptions { /// `SocketOption` allows users to specify configuration settings that are directly applied to the underlying socket file descriptor. /// /// Valid options are typically found in the various man pages like `man 4 tcp`. - public struct SocketOption: ChannelOption, Equatable { + public struct SocketOption: ChannelOption, Equatable, NIOSendable { public typealias Value = (SocketOptionValue) public var optionLevel: NIOBSDSocket.OptionLevel @@ -116,14 +116,14 @@ extension ChannelOptions { } /// `AllocatorOption` allows to specify the `ByteBufferAllocator` to use. - public struct AllocatorOption: ChannelOption { + public struct AllocatorOption: ChannelOption, NIOSendable { public typealias Value = ByteBufferAllocator public init() {} } /// `RecvAllocatorOption` allows users to specify the `RecvByteBufferAllocator` to use. - public struct RecvAllocatorOption: ChannelOption { + public struct RecvAllocatorOption: ChannelOption, NIOSendable { public typealias Value = RecvByteBufferAllocator public init() {} @@ -131,7 +131,7 @@ extension ChannelOptions { /// `AutoReadOption` allows users to configure if a `Channel` should automatically call `Channel.read` again once all data was read from the transport or /// if the user is responsible to call `Channel.read` manually. - public struct AutoReadOption: ChannelOption { + public struct AutoReadOption: ChannelOption, NIOSendable { public typealias Value = Bool public init() {} @@ -140,7 +140,7 @@ extension ChannelOptions { /// `WriteSpinOption` allows users to configure the number of repetitions of a only partially successful write call before considering the `Channel` not writable. /// Setting this option to `0` means that we only issue one write call and if that call does not write all the bytes, /// we consider the `Channel` not writable. - public struct WriteSpinOption: ChannelOption { + public struct WriteSpinOption: ChannelOption, NIOSendable { public typealias Value = UInt public init() {} @@ -148,14 +148,14 @@ extension ChannelOptions { /// `MaxMessagesPerReadOption` allows users to configure the maximum number of read calls to the underlying transport are performed before wait again until /// there is more to read and be notified. - public struct MaxMessagesPerReadOption: ChannelOption { + public struct MaxMessagesPerReadOption: ChannelOption, NIOSendable { public typealias Value = UInt public init() {} } /// `BacklogOption` allows users to configure the `backlog` value as specified in `man 2 listen`. This is only useful for `ServerSocketChannel`s. - public struct BacklogOption: ChannelOption { + public struct BacklogOption: ChannelOption, NIOSendable { public typealias Value = Int32 public init() {} @@ -185,20 +185,20 @@ extension ChannelOptions { /// On all other platforms, setting it has no effect. /// /// Set this option to 0 to disable vector reads and to use serial reads instead. - public struct DatagramVectorReadMessageCountOption: ChannelOption { + public struct DatagramVectorReadMessageCountOption: ChannelOption, NIOSendable { public typealias Value = Int public init() { } } /// When set to true IP level ECN information will be reported through `AddressedEnvelope.Metadata` - public struct ExplicitCongestionNotificationsOption: ChannelOption { + public struct ExplicitCongestionNotificationsOption: ChannelOption, NIOSendable { public typealias Value = Bool public init() {} } /// The watermark used to detect when `Channel.isWritable` returns `true` or `false`. - public struct WriteBufferWaterMark { + public struct WriteBufferWaterMark: NIOSendable { /// The low mark setting for a `Channel`. /// /// When the amount of buffered bytes in the `Channel`s outbound buffer drops below this value the `Channel` will be @@ -231,7 +231,7 @@ extension ChannelOptions { /// `Channel.isWritable` will return `false`. Once we were able to write some data out of the outbound buffer and the amount of bytes queued /// falls below `WriteBufferWaterMark.low` the `Channel` will become writable again. Once this happens `Channel.writable` will return /// `true` again. These writability changes are also propagated through the `ChannelPipeline` and so can be intercepted via `ChannelInboundHandler.channelWritabilityChanged`. - public struct WriteBufferWaterMarkOption: ChannelOption { + public struct WriteBufferWaterMarkOption: ChannelOption, NIOSendable { public typealias Value = WriteBufferWaterMark public init() {} @@ -239,7 +239,7 @@ extension ChannelOptions { /// `ConnectTimeoutOption` allows users to configure the `TimeAmount` after which a connect will fail if it was not established in the meantime. May be /// `nil`, in which case the connection attempt will never time out. - public struct ConnectTimeoutOption: ChannelOption { + public struct ConnectTimeoutOption: ChannelOption, NIOSendable { public typealias Value = TimeAmount? public init() {} @@ -250,14 +250,14 @@ extension ChannelOptions { /// will be closed automatically if the remote peer shuts down its send stream. If set to true, the `Channel` will /// not be closed: instead, a `ChannelEvent.inboundClosed` user event will be sent on the `ChannelPipeline`, /// and no more data will be received. - public struct AllowRemoteHalfClosureOption: ChannelOption { + public struct AllowRemoteHalfClosureOption: ChannelOption, NIOSendable { public typealias Value = Bool public init() {} } /// When set to true IP level Packet Info information will be reported through `AddressedEnvelope.Metadata` for UDP packets. - public struct ReceivePacketInfo: ChannelOption { + public struct ReceivePacketInfo: ChannelOption, NIOSendable { public typealias Value = Bool public init() {} } @@ -385,3 +385,7 @@ extension ChannelOptions { } } } + +#if swift(>=5.6) +extension ChannelOptions.Storage: @unchecked Sendable {} +#endif diff --git a/Sources/NIOCore/NIOSendable.swift b/Sources/NIOCore/NIOSendable.swift index 535e9f747..3755e79cc 100644 --- a/Sources/NIOCore/NIOSendable.swift +++ b/Sources/NIOCore/NIOSendable.swift @@ -17,3 +17,9 @@ public typealias NIOSendable = Swift.Sendable #else public typealias NIOSendable = Any #endif + +#if swift(>=5.6) +@preconcurrency public protocol NIOPreconcurrencySendable: Sendable {} +#else +public protocol NIOPreconcurrencySendable {} +#endif