From 75ad4d70e56f41cd3af1549badc624d02b75142b Mon Sep 17 00:00:00 2001 From: Cory Benfield Date: Thu, 17 Oct 2024 16:36:13 +0100 Subject: [PATCH] Further tweaks --- Sources/NIOCore/EventLoop.swift | 6 ++++-- Sources/NIOCore/EventLoopFuture.swift | 12 ++++++++++-- Sources/NIOCore/NIOScheduledCallback.swift | 3 ++- Sources/NIOEmbedded/AsyncTestingEventLoop.swift | 6 ++++-- Sources/NIOEmbedded/Embedded.swift | 3 ++- 5 files changed, 22 insertions(+), 8 deletions(-) diff --git a/Sources/NIOCore/EventLoop.swift b/Sources/NIOCore/EventLoop.swift index ddcfcaf030..5732a989be 100644 --- a/Sources/NIOCore/EventLoop.swift +++ b/Sources/NIOCore/EventLoop.swift @@ -368,20 +368,22 @@ public protocol EventLoop: EventLoopGroup { /// /// - NOTE: Event loops that provide a custom scheduled callback implementation **must** also implement /// `cancelScheduledCallback`. Failure to do so will result in a runtime error. + @preconcurrency @discardableResult func scheduleCallback( at deadline: NIODeadline, - handler: some NIOScheduledCallbackHandler + handler: some (NIOScheduledCallbackHandler & Sendable) ) throws -> NIOScheduledCallback /// Schedule a callback after given time. /// /// - NOTE: Event loops that provide a custom scheduled callback implementation **must** also implement /// `cancelScheduledCallback`. Failure to do so will result in a runtime error. + @preconcurrency @discardableResult func scheduleCallback( in amount: TimeAmount, - handler: some NIOScheduledCallbackHandler + handler: some (NIOScheduledCallbackHandler & Sendable) ) throws -> NIOScheduledCallback /// Cancel a scheduled callback. diff --git a/Sources/NIOCore/EventLoopFuture.swift b/Sources/NIOCore/EventLoopFuture.swift index b5b4b4352f..ab90364407 100644 --- a/Sources/NIOCore/EventLoopFuture.swift +++ b/Sources/NIOCore/EventLoopFuture.swift @@ -1457,8 +1457,12 @@ extension EventLoopFuture { // value across event loops. future.whenComplete { result in let voidResult = result.map { _ in } - future.eventLoop.execute { + if eventLoop.inEventLoop { processResult(index, voidResult) + } else { + eventLoop.execute { + processResult(index, voidResult) + } } } } @@ -1684,8 +1688,12 @@ extension EventLoopFuture { // value across event loops. future.whenComplete { result in let voidResult = result.map { _ in } - future.eventLoop.execute { + if eventLoop.inEventLoop { processResult(index, voidResult) + } else { + eventLoop.execute { + processResult(index, voidResult) + } } } } diff --git a/Sources/NIOCore/NIOScheduledCallback.swift b/Sources/NIOCore/NIOScheduledCallback.swift index b5ac26431f..b2af415aad 100644 --- a/Sources/NIOCore/NIOScheduledCallback.swift +++ b/Sources/NIOCore/NIOScheduledCallback.swift @@ -141,11 +141,12 @@ extension EventLoop { } /// Default implementation of `scheduleCallback(in amount:handler:)`: calls `scheduleCallback(at deadline:handler:)`. + @preconcurrency @discardableResult @inlinable public func scheduleCallback( in amount: TimeAmount, - handler: some NIOScheduledCallbackHandler + handler: some (NIOScheduledCallbackHandler & Sendable) ) throws -> NIOScheduledCallback { try self.scheduleCallback(at: .now() + amount, handler: handler) } diff --git a/Sources/NIOEmbedded/AsyncTestingEventLoop.swift b/Sources/NIOEmbedded/AsyncTestingEventLoop.swift index 52d56bf9ca..14e5242df0 100644 --- a/Sources/NIOEmbedded/AsyncTestingEventLoop.swift +++ b/Sources/NIOEmbedded/AsyncTestingEventLoop.swift @@ -192,9 +192,10 @@ public final class NIOAsyncTestingEventLoop: EventLoop, @unchecked Sendable { self.scheduleTask(deadline: self.now + `in`, task) } + @preconcurrency public func scheduleCallback( at deadline: NIODeadline, - handler: some NIOScheduledCallbackHandler + handler: some (NIOScheduledCallbackHandler & Sendable) ) throws -> NIOScheduledCallback { /// The default implementation of `scheduledCallback(at:handler)` makes two calls to the event loop because it /// needs to hook the future of the backing scheduled task, which can lead to lost cancellation callbacks when @@ -213,10 +214,11 @@ public final class NIOAsyncTestingEventLoop: EventLoop, @unchecked Sendable { } } + @preconcurrency @discardableResult public func scheduleCallback( in amount: TimeAmount, - handler: some NIOScheduledCallbackHandler + handler: some (NIOScheduledCallbackHandler & Sendable) ) throws -> NIOScheduledCallback { /// Even though this type does not implement a custom `scheduleCallback(at:handler)`, it uses a manual clock so /// it cannot rely on the default implementation of `scheduleCallback(in:handler:)`, which computes the deadline diff --git a/Sources/NIOEmbedded/Embedded.swift b/Sources/NIOEmbedded/Embedded.swift index a6c2b33b5b..3105f3af85 100644 --- a/Sources/NIOEmbedded/Embedded.swift +++ b/Sources/NIOEmbedded/Embedded.swift @@ -160,10 +160,11 @@ public final class EmbeddedEventLoop: EventLoop, CustomStringConvertible { scheduleTask(deadline: self._now + `in`, task) } + @preconcurrency @discardableResult public func scheduleCallback( in amount: TimeAmount, - handler: some NIOScheduledCallbackHandler + handler: some (NIOScheduledCallbackHandler & Sendable) ) -> NIOScheduledCallback { /// Even though this type does not implement a custom `scheduleCallback(at:handler)`, it uses a manual clock so /// it cannot rely on the default implementation of `scheduleCallback(in:handler:)`, which computes the deadline