mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
It could be confusing to adopters who were led to believe by the types that they should "just" implement the sharedUnownedExecutor property, but insead they have to implement the unownedExecutor on the specific actor type. Adding documentation clarify this as well as a simple test that exercises this explicitly; We seem to have much coverage of main actor, but not so much of custom executor global actors.
102 lines
2.3 KiB
Swift
102 lines
2.3 KiB
Swift
// RUN: %target-run-simple-swift( %import-libdispatch -strict-concurrency=complete -parse-as-library) | %FileCheck %s
|
|
|
|
// REQUIRES: concurrency
|
|
// REQUIRES: executable_test
|
|
// REQUIRES: libdispatch
|
|
// UNSUPPORTED: freestanding
|
|
|
|
// UNSUPPORTED: back_deployment_runtime
|
|
// REQUIRES: concurrency_runtime
|
|
|
|
import Dispatch
|
|
|
|
let globalQueue = DispatchQueue(label: "SimpleQueue")
|
|
|
|
@available(SwiftStdlib 6.0, *)
|
|
final class NaiveQueueExecutor: SerialExecutor {
|
|
public func enqueue(_ unowned: UnownedJob) {
|
|
globalQueue.sync {
|
|
unowned.runSynchronously(on: self.asUnownedSerialExecutor())
|
|
}
|
|
}
|
|
|
|
public func asUnownedSerialExecutor() -> UnownedSerialExecutor {
|
|
return UnownedSerialExecutor(ordinary: self)
|
|
}
|
|
|
|
func checkIsolated() {
|
|
// ok
|
|
}
|
|
}
|
|
|
|
@available(SwiftStdlib 6.0, *)
|
|
actor Simple {
|
|
var count = 0
|
|
let exec = NaiveQueueExecutor()
|
|
|
|
func report() {
|
|
print("simple.count == \(count)")
|
|
count += 1
|
|
}
|
|
|
|
nonisolated var unownedExecutor: UnownedSerialExecutor {
|
|
print("Simple.unownedExecutor")
|
|
return exec.asUnownedSerialExecutor()
|
|
}
|
|
}
|
|
|
|
@globalActor
|
|
@available(SwiftStdlib 6.0, *)
|
|
actor MyGlobalActor {
|
|
static let simple = Simple()
|
|
static let shared = MyGlobalActor()
|
|
|
|
static var sharedUnownedExecutor: UnownedSerialExecutor {
|
|
print("MyGlobalActor.sharedUnownedExecutor")
|
|
return simple.unownedExecutor
|
|
}
|
|
nonisolated var unownedExecutor: UnownedSerialExecutor {
|
|
print("MyGlobalActor.unownedExecutor")
|
|
return Self.simple.unownedExecutor
|
|
}
|
|
}
|
|
|
|
@MyGlobalActor
|
|
@available(SwiftStdlib 6.0, *)
|
|
final class Custom {
|
|
var count = 0
|
|
let simple = MyGlobalActor.simple
|
|
|
|
nonisolated var unownedExecutor: UnownedSerialExecutor {
|
|
return simple.unownedExecutor
|
|
}
|
|
|
|
func report() async {
|
|
simple.preconditionIsolated()
|
|
|
|
print("custom.count == \(count)")
|
|
count += 1
|
|
|
|
await simple.report()
|
|
}
|
|
}
|
|
|
|
@available(SwiftStdlib 6.0, *)
|
|
@main struct Main {
|
|
static func main() async {
|
|
print("begin")
|
|
let actor = Custom()
|
|
await actor.report()
|
|
print("end")
|
|
}
|
|
}
|
|
|
|
// CHECK: begin
|
|
// CHECK-NEXT: MyGlobalActor.unownedExecutor
|
|
// CHECK-NEXT: Simple.unownedExecutor
|
|
// CHECK-NEXT: Simple.unownedExecutor
|
|
// CHECK-NEXT: custom.count == 0
|
|
// CHECK-NEXT: Simple.unownedExecutor
|
|
// CHECK-NEXT: simple.count == 0
|
|
// CHECK-NEXT: end
|