LibCore: Add swift bindings for EventLoop as an Executor and Actor

This commit is contained in:
Andrew Kaster 2025-03-09 13:43:51 -06:00 committed by Andrew Kaster
commit c8787e6a9f
Notes: github-actions[bot] 2025-03-16 03:52:13 +00:00
10 changed files with 224 additions and 1 deletions

View file

@ -0,0 +1,53 @@
/*
* Copyright (c) 2025, Andrew Kaster <andrew@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
import AK
@_exported import CoreCxx
extension Core.EventLoop: Equatable {
func deferred_invoke(_ task: @escaping () -> Void) {
Core.deferred_invoke_block(self, task)
}
public static func == (lhs: Core.EventLoop, rhs: Core.EventLoop) -> Bool {
Unmanaged.passUnretained(lhs).toOpaque() == Unmanaged.passUnretained(rhs).toOpaque()
}
}
public class EventLoopExecutor: SerialExecutor, TaskExecutor, @unchecked Sendable {
nonisolated private let eventLoop: Core.EventLoop
public init() {
eventLoop = Core.EventLoop.current()
}
public init(eventLoop: Core.EventLoop) {
self.eventLoop = eventLoop
}
public nonisolated func enqueue(_ job: consuming ExecutorJob) {
let job = UnownedJob(job)
eventLoop.deferred_invoke { [self, job] in
job.runSynchronously(
isolatedTo: self.asUnownedSerialExecutor(),
taskExecutor: self.asUnownedTaskExecutor())
}
}
public func checkIsolated() {
precondition(Core.EventLoop.current() == eventLoop)
}
}
public protocol EventLoopActor: Actor {
nonisolated var executor: EventLoopExecutor { get } // impl with a let
}
extension EventLoopActor {
public nonisolated var unownedExecutor: UnownedSerialExecutor {
executor.asUnownedSerialExecutor()
}
}