Skip to content

Commit eb63223

Browse files
authored
[Concurrency] Additional custom executor isSame test, for common base class (#64670)
1 parent f3ea854 commit eb63223

File tree

1 file changed

+113
-0
lines changed

1 file changed

+113
-0
lines changed
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
// RUN: %target-run-simple-swift( -Xfrontend -enable-experimental-move-only -Xfrontend -disable-availability-checking %import-libdispatch -parse-as-library) | %FileCheck %s
2+
3+
// REQUIRES: concurrency
4+
// REQUIRES: executable_test
5+
// REQUIRES: libdispatch
6+
7+
// rdar://106849189 move-only types should be supported in freestanding mode
8+
// UNSUPPORTED: freestanding
9+
10+
// FIXME: rdar://107112715 test failing on iOS simulator, investigating
11+
// UNSUPPORTED: OS=ios
12+
13+
// UNSUPPORTED: back_deployment_runtime
14+
// REQUIRES: concurrency_runtime
15+
16+
@preconcurrency import Dispatch
17+
@_spi(ConcurrencyExecutors) import _Concurrency
18+
19+
class BaseExecutor: SerialExecutor {
20+
let name: String
21+
let queue: DispatchQueue
22+
23+
init(name: String, _ queue: DispatchQueue) {
24+
self.name = name
25+
self.queue = queue
26+
}
27+
28+
29+
func enqueue(_ unowned: UnownedJob) {
30+
fatalError("not implemented in base class")
31+
}
32+
33+
func asUnownedSerialExecutor() -> UnownedSerialExecutor {
34+
let ref = UnownedSerialExecutor(complexEquality: self)
35+
precondition(ref._isComplexEquality, "expected the ref to have complex equality")
36+
return ref
37+
}
38+
39+
func isSameExclusiveExecutionContext(other: BaseExecutor) -> Bool {
40+
if Set([self.name, other.name]) == Set(["left", "right"]) {
41+
// those we consider equal
42+
print("BASE \(BaseExecutor.self).isSameExclusiveExecutionContext: consider \(Self.self)('left') and \(type(of: other))('right') executors as equal context")
43+
return true
44+
} else {
45+
return false
46+
}
47+
}
48+
}
49+
50+
final class LeftExecutor: BaseExecutor {
51+
override init(name: String, _ queue: DispatchQueue) {
52+
super.init(name: name, queue)
53+
}
54+
55+
override func enqueue(_ unowned: UnownedJob) {
56+
print("\(self): enqueue")
57+
queue.sync {
58+
unowned.runSynchronously(on: self.asUnownedSerialExecutor())
59+
}
60+
}
61+
}
62+
63+
final class RightExecutor: BaseExecutor {
64+
override init(name: String, _ queue: DispatchQueue) {
65+
super.init(name: name, queue)
66+
}
67+
68+
override func enqueue(_ unowned: UnownedJob) {
69+
print("\(self): enqueue")
70+
queue.sync {
71+
unowned.runSynchronously(on: self.asUnownedSerialExecutor())
72+
}
73+
}
74+
}
75+
76+
actor MyActor {
77+
78+
nonisolated let executor: BaseExecutor
79+
nonisolated var unownedExecutor: UnownedSerialExecutor {
80+
print("Get executor of \(self): \(executor.asUnownedSerialExecutor())")
81+
return executor.asUnownedSerialExecutor()
82+
}
83+
84+
init(executor: BaseExecutor) {
85+
self.executor = executor
86+
}
87+
88+
func test(expectedExecutor: BaseExecutor, expectedQueue: DispatchQueue) {
89+
preconditionTaskOnExecutor(expectedExecutor, message: "Expected deep equality to trigger for \(expectedExecutor) and our \(self.executor)")
90+
print("\(Self.self): [\(self.executor.name)] on same context as [\(expectedExecutor.name)]")
91+
}
92+
}
93+
94+
@main struct Main {
95+
static func main() async {
96+
print("begin")
97+
let queue = DispatchQueue(label: "RootQueue")
98+
let one = LeftExecutor(name: "left", queue)
99+
let two = RightExecutor(name: "right", queue)
100+
let actor = MyActor(executor: one)
101+
await actor.test(expectedExecutor: one, expectedQueue: queue)
102+
await actor.test(expectedExecutor: two, expectedQueue: queue)
103+
print("end")
104+
}
105+
}
106+
107+
// CHECK: begin
108+
// CHECK-NEXT: Get executor of main.MyActor
109+
// CHECK-NEXT: main.LeftExecutor: enqueue
110+
// CHECK-NEXT: MyActor: [left] on same context as [left]
111+
// CHECK-NEXT: BASE BaseExecutor.isSameExclusiveExecutionContext: consider LeftExecutor('left') and RightExecutor('right') executors as equal context
112+
// CHECK-NEXT: MyActor: [left] on same context as [right]
113+
// CHECK-NEXT: end

0 commit comments

Comments
 (0)