mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-08-05 15:49:11 +00:00
LibCore: Add swift bindings for EventLoop as an Executor and Actor
This commit is contained in:
parent
3af63bc5a3
commit
c8787e6a9f
Notes:
github-actions[bot]
2025-03-16 03:52:13 +00:00
Author: https://github.com/ADKaster
Commit: c8787e6a9f
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/3880
10 changed files with 224 additions and 1 deletions
53
Libraries/LibCore/EventLoopExecutor.swift
Normal file
53
Libraries/LibCore/EventLoopExecutor.swift
Normal 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()
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue