diff --git a/Sources/NIO/Channel.swift b/Sources/NIO/Channel.swift index c00d6c534..4eeee85df 100644 --- a/Sources/NIO/Channel.swift +++ b/Sources/NIO/Channel.swift @@ -14,6 +14,7 @@ import Foundation import Sockets +import ConcurrencyHelpers #if os(Linux) import Glibc @@ -104,12 +105,17 @@ final fileprivate class PendingWrites { private var storageRefs: UnsafeMutableBufferPointer> fileprivate var waterMark: WriteBufferWaterMark = WriteBufferWaterMark(32 * 1024..<64 * 1024) - private var writable = true + private var writable: Atomic = Atomic(value: true) + fileprivate var writeSpinCount: UInt = 16 private(set) var outstanding: (chunks: Int, bytes: Int) = (0, 0) private(set) var closed = false + + var isWritable: Bool { + return writable.load() + } var isEmpty: Bool { return tail == nil @@ -128,8 +134,7 @@ final fileprivate class PendingWrites { } outstanding = (chunks: outstanding.chunks + 1, bytes: outstanding.bytes + buffer.readableBytes) - if writable && outstanding.bytes > Int(waterMark.upperBound) { - writable = false + if outstanding.bytes > Int(waterMark.upperBound) && writable.compareAndExchange(expected: true, desired: false) { // Returns false to signal the Channel became non-writable and we need to notify the user return false } @@ -148,12 +153,12 @@ final fileprivate class PendingWrites { Function that takes two closures and based on if there are more then one ByteBuffer pending calls either one or the other. */ func consume(oneBody: (UnsafePointer, Int) throws -> IOResult, multipleBody: (UnsafeBufferPointer) throws -> IOResult) throws -> (writeResult: WriteResult, writable: Bool) { - let wasWritable = writable + let wasWritable = writable.load() let result = try hasMultiple ? consumeMultiple(multipleBody) : consumeOne(oneBody) if !wasWritable { // Was not writable before so signal back to the caller the possible state change - return (result, writable) + return (result, writable.load()) } return (result, false) } @@ -183,8 +188,8 @@ final fileprivate class PendingWrites { case .processed(let written): outstanding = (outstanding.chunks, outstanding.bytes - written) - if !writable && outstanding.bytes < Int(waterMark.lowerBound) { - writable = true + if outstanding.bytes < Int(waterMark.lowerBound) { + writable.store(true) } if pending.buffer.readableBytes == written { @@ -234,8 +239,8 @@ final fileprivate class PendingWrites { case .processed(let written): outstanding = (outstanding.chunks, outstanding.bytes - written) - if !writable && outstanding.bytes < Int(waterMark.lowerBound) { - writable = true + if outstanding.bytes < Int(waterMark.lowerBound) { + writable.store(true) } var w = written @@ -379,6 +384,8 @@ final class SocketChannel : BaseSocketChannel { if bytesRead > 0 { recvAllocator.record(actualReadBytes: bytesRead) + readPending = false + pipeline.fireChannelRead0(data: .byteBuffer(buffer)) // Reset reader and writerIndex and so allow to have the buffer filled again @@ -417,7 +424,7 @@ final class SocketChannel : BaseSocketChannel { }) if result.writable { // writable again - self.pipeline.fireChannelWritabilityChanged0(writable: true) + self.pipeline.fireChannelWritabilityChanged0() } if result.writeResult == .writtenCompletely && pendingWrites.isFlushPending { // there are more buffers to process, so continue @@ -499,6 +506,8 @@ final class ServerSocketChannel : BaseSocketChannel { return } if let accepted = try self.socket.accept() { + readPending = false + do { pipeline.fireChannelRead0(data: .other(try SocketChannel(socket: accepted, eventLoop: group.next() as! SelectableEventLoop))) } catch let err { @@ -564,6 +573,8 @@ public protocol Channel : class, ChannelOutboundInvoker { func setOption(option: T, value: T.OptionType) throws func getOption(option: T) throws -> T.OptionType + var isWritable: Bool { get } + var _unsafe: ChannelCore { get } } @@ -584,6 +595,7 @@ extension Channel { return _unsafe.eventLoop } + @discardableResult public func bind(local: SocketAddress, promise: Promise) -> Future { pipeline.bind(local: local, promise: promise) return promise.futureResult @@ -642,7 +654,7 @@ class BaseSocketChannel : SelectableChannel, ChannelCore { } private let pendingWrites: PendingWrites - private var readPending = false + fileprivate var readPending = false private var neverRegistered = true private var pendingConnect: Promise? private let closePromise: Promise @@ -657,6 +669,10 @@ class BaseSocketChannel : SelectableChannel, ChannelCore { return selectableEventLoop } + public final var isWritable: Bool { + return pendingWrites.isWritable + } + public final var allocator: ByteBufferAllocator { if eventLoop.inEventLoop { return bufferAllocator @@ -830,7 +846,7 @@ class BaseSocketChannel : SelectableChannel, ChannelCore { } if let buffer = data.tryAsByteBuffer() { if !pendingWrites.add(buffer: buffer, promise: promise) { - pipeline.fireChannelWritabilityChanged0(writable: false) + pipeline.fireChannelWritabilityChanged0() } } else { // Only support ByteBuffer for now. @@ -1008,7 +1024,6 @@ class BaseSocketChannel : SelectableChannel, ChannelCore { assert(eventLoop.inEventLoop) assert(!closed) - readPending = false defer { if !closed, !readPending { unregisterForReadable() diff --git a/Sources/NIO/ChannelHandler.swift b/Sources/NIO/ChannelHandler.swift index 1d6c1e2df..12f8fa0bb 100644 --- a/Sources/NIO/ChannelHandler.swift +++ b/Sources/NIO/ChannelHandler.swift @@ -22,7 +22,7 @@ public protocol ChannelHandler : class { func channelInactive(ctx: ChannelHandlerContext) throws func channelRead(ctx: ChannelHandlerContext, data: IOData) throws func channelReadComplete(ctx: ChannelHandlerContext) throws - func channelWritabilityChanged(ctx: ChannelHandlerContext, writable: Bool) throws + func channelWritabilityChanged(ctx: ChannelHandlerContext) throws func userEventTriggered(ctx: ChannelHandlerContext, event: Any) throws func errorCaught(ctx: ChannelHandlerContext, error: Error) throws func register(ctx: ChannelHandlerContext, promise: Promise) @@ -64,8 +64,8 @@ public extension ChannelHandler { ctx.fireChannelReadComplete() } - public func channelWritabilityChanged(ctx: ChannelHandlerContext, writable: Bool) { - ctx.fireChannelWritabilityChanged(writable: writable) + public func channelWritabilityChanged(ctx: ChannelHandlerContext) { + ctx.fireChannelWritabilityChanged() } public func userEventTriggered(ctx: ChannelHandlerContext, event: Any) { diff --git a/Sources/NIO/ChannelHandlers.swift b/Sources/NIO/ChannelHandlers.swift index cbd38273d..e73b5f04e 100644 --- a/Sources/NIO/ChannelHandlers.swift +++ b/Sources/NIO/ChannelHandlers.swift @@ -35,8 +35,8 @@ public class BackPressureHandler: ChannelHandler { } } - public func channelWritabilityChanged(ctx: ChannelHandlerContext, writable: Bool) { - self.writable = writable + public func channelWritabilityChanged(ctx: ChannelHandlerContext) { + self.writable = ctx.channel!.isWritable if writable { if readPending { readPending = false @@ -47,7 +47,7 @@ public class BackPressureHandler: ChannelHandler { } // Propagate the event as the user may still want to do something based on it. - ctx.fireChannelWritabilityChanged(writable: writable) + ctx.fireChannelWritabilityChanged() } public func handlerRemoved(ctx: ChannelHandlerContext) { diff --git a/Sources/NIO/ChannelInboundInvoker.swift b/Sources/NIO/ChannelInboundInvoker.swift index cab718c05..684d55deb 100644 --- a/Sources/NIO/ChannelInboundInvoker.swift +++ b/Sources/NIO/ChannelInboundInvoker.swift @@ -26,7 +26,7 @@ public protocol ChannelInboundInvoker { func fireChannelReadComplete() - func fireChannelWritabilityChanged(writable: Bool) + func fireChannelWritabilityChanged() func fireErrorCaught(error: Error) diff --git a/Sources/NIO/ChannelPipeline.swift b/Sources/NIO/ChannelPipeline.swift index 3315f8d05..c68a0aed8 100644 --- a/Sources/NIO/ChannelPipeline.swift +++ b/Sources/NIO/ChannelPipeline.swift @@ -253,12 +253,12 @@ public final class ChannelPipeline : ChannelInboundInvoker { } } - public func fireChannelWritabilityChanged(writable: Bool) { + public func fireChannelWritabilityChanged() { if eventLoop.inEventLoop { - fireChannelWritabilityChanged0(writable: writable) + fireChannelWritabilityChanged0() } else { eventLoop.execute { - self.fireChannelWritabilityChanged0(writable: writable) + self.fireChannelWritabilityChanged0() } } } @@ -392,8 +392,8 @@ public final class ChannelPipeline : ChannelInboundInvoker { head!.invokeChannelReadComplete() } - func fireChannelWritabilityChanged0(writable: Bool) { - head!.invokeChannelWritabilityChanged(writable: writable) + func fireChannelWritabilityChanged0() { + head!.invokeChannelWritabilityChanged() } func fireUserEventTriggered0(event: Any) { @@ -503,7 +503,7 @@ private final class TailChannelHandler : ChannelHandler { // Discard } - func channelWritabilityChanged(ctx: ChannelHandlerContext, writable: Bool) { + func channelWritabilityChanged(ctx: ChannelHandlerContext) { // Discard } @@ -576,8 +576,8 @@ public final class ChannelHandlerContext : ChannelInboundInvoker, ChannelOutboun next!.invokeChannelReadComplete() } - public func fireChannelWritabilityChanged(writable: Bool) { - next!.invokeChannelWritabilityChanged(writable: writable) + public func fireChannelWritabilityChanged() { + next!.invokeChannelWritabilityChanged() } public func fireErrorCaught(error: Error) { @@ -686,11 +686,11 @@ public final class ChannelHandlerContext : ChannelInboundInvoker, ChannelOutboun } } - func invokeChannelWritabilityChanged(writable: Bool) { + func invokeChannelWritabilityChanged() { assert(inEventLoop) do { - try handler.channelWritabilityChanged(ctx: self, writable: writable) + try handler.channelWritabilityChanged(ctx: self) } catch let err { invokeErrorCaught(error: err) } diff --git a/Sources/NIO/Embedded.swift b/Sources/NIO/Embedded.swift index 2a6cbd15d..2f6307f6a 100644 --- a/Sources/NIO/Embedded.swift +++ b/Sources/NIO/Embedded.swift @@ -140,6 +140,10 @@ public class EmbeddedChannel : Channel { return _pipeline } + public var isWritable: Bool { + return true + } + private var _pipeline: ChannelPipeline! public let allocator: ByteBufferAllocator = ByteBufferAllocator() public var eventLoop: EventLoop = EmbeddedEventLoop()