Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement customization point for enqueue #190

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ let package = Package(
.library(name: "NIOTransportServices", targets: ["NIOTransportServices"]),
],
dependencies: [
.package(url: "https://github.com/apple/swift-nio.git", from: "2.58.0"),
.package(url: "https://github.com/apple/swift-nio.git", from: "2.60.0"),
.package(url: "https://github.com/apple/swift-atomics.git", from: "1.0.2"),
.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.0.0"),
],
Expand Down
17 changes: 17 additions & 0 deletions Sources/NIOTransportServices/NIOTSEventLoop.swift
Original file line number Diff line number Diff line change
Expand Up @@ -262,4 +262,21 @@ extension NIOTSEventLoop {
assert(oldChannel != nil)
}
}

// MARK: SerialExecutor conformance
#if compiler(>=5.9)
@available(macOS 14.0, *)
extension NIOTSEventLoop: NIOSerialEventLoopExecutor {
func enqueue(_ job: consuming ExecutorJob) {
let unownedJob = UnownedJob(job)
if let executorQueue = self.taskQueue as? _DispatchSerialExecutorQueue {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is currently just available as underscored API and cannot be relied on. This PR is more for demonstrating that this can be fast pathed as well.

executorQueue.enqueue(unownedJob)
} else {
self.execute {
unownedJob.runSynchronously(on: self.asUnownedSerialExecutor())
}
}
}
}
#endif
#endif
20 changes: 19 additions & 1 deletion Tests/NIOTransportServicesTests/NIOTSEventLoopTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,20 @@ import NIOCore
import NIOConcurrencyHelpers
import NIOTransportServices

@available(macOS 14.0, iOS 17.0, watchOS 10.0, tvOS 17.0, *)
private final actor CustomActor {
let executor = NIOTSEventLoopGroup(loopCount: 1).next().executor
nonisolated var unownedExecutor: UnownedSerialExecutor {
return executor.asUnownedSerialExecutor()
}

func foo() -> String {
return "foo"
}
}

@available(OSX 10.14, iOS 12.0, tvOS 12.0, *)
class NIOTSEventLoopTest: XCTestCase {
final class NIOTSEventLoopTest: XCTestCase {
func testIsInEventLoopWorks() throws {
let group = NIOTSEventLoopGroup()
let loop = group.next()
Expand Down Expand Up @@ -149,5 +160,12 @@ class NIOTSEventLoopTest: XCTestCase {
}
}
}

@available(macOS 14.0, iOS 17.0, tvOS 17.0, *)
func testSerialExecutor() async {
let actor = CustomActor()
let result = await actor.foo()
XCTAssertEqual(result, "foo")
}
}
#endif