forked from swiftlang/swift
- Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasync_task_executor_structured_concurrency.swift
222 lines (195 loc) · 7.77 KB
/
async_task_executor_structured_concurrency.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// RUN: %target-run-simple-swift( -Xfrontend -disable-availability-checking %import-libdispatch -parse-as-library )
// REQUIRES: executable_test
// REQUIRES: concurrency
// REQUIRES: libdispatch
// REQUIRES: concurrency_runtime
// UNSUPPORTED: back_deployment_runtime
// rdar://126118749
// UNSUPPORTED: CPU=arm64e
import Dispatch
import StdlibUnittest
import Dispatch
finalclassMyTaskExecutor:TaskExecutor,@uncheckedSendable{
letqueue:DispatchQueue
init(queue:DispatchQueue){
self.queue = queue
}
func enqueue(_ job:consumingExecutorJob){
letjob=UnownedJob(job)
queue.async{
job.runSynchronously(on:self.asUnownedTaskExecutor())
}
}
}
func testTaskGroup(_ firstExecutor:MyTaskExecutor,
_ secondExecutor:MyTaskExecutor)async{
// 1 level of child tasks
awaitwithTaskGroup(of:Int.self){ group in
group.addTask(executorPreference: firstExecutor){
dispatchPrecondition(condition:.onQueue(firstExecutor.queue))
dispatchPrecondition(condition:.notOnQueue(secondExecutor.queue))
return1
}
}
awaitwithThrowingTaskGroup(of:Int.self){ group in
group.addTask(executorPreference: firstExecutor){
dispatchPrecondition(condition:.onQueue(firstExecutor.queue))
dispatchPrecondition(condition:.notOnQueue(secondExecutor.queue))
return2
}
}
awaitwithDiscardingTaskGroup(){ group in
group.addTask(executorPreference: firstExecutor){
dispatchPrecondition(condition:.onQueue(firstExecutor.queue))
dispatchPrecondition(condition:.notOnQueue(secondExecutor.queue))
}
}
try!awaitwithThrowingDiscardingTaskGroup(){ group in
group.addTask(executorPreference: firstExecutor){
dispatchPrecondition(condition:.onQueue(firstExecutor.queue))
dispatchPrecondition(condition:.notOnQueue(secondExecutor.queue))
}
}
// Multiple levels of child tasks
awaitwithTaskGroup(of:Int.self){ group in
group.addTask(executorPreference: firstExecutor){
dispatchPrecondition(condition:.onQueue(firstExecutor.queue))
dispatchPrecondition(condition:.notOnQueue(secondExecutor.queue))
returnawaitwithTaskGroup(of:Int.self){ group in
group.addTask(executorPreference: secondExecutor){
dispatchPrecondition(condition:.notOnQueue(firstExecutor.queue))
dispatchPrecondition(condition:.onQueue(secondExecutor.queue))
return12
}
returnawait group.next()!
}
}
}
// Disabling task preference, in task group child task
_ =awaitwithTaskGroup(of:Int.self){ group in
dispatchPrecondition(condition:.notOnQueue(firstExecutor.queue))
dispatchPrecondition(condition:.notOnQueue(secondExecutor.queue))
group.addTask(executorPreference: secondExecutor){
dispatchPrecondition(condition:.notOnQueue(firstExecutor.queue))
dispatchPrecondition(condition:.onQueue(secondExecutor.queue))
returnawaitwithTaskGroup(of:Int.self){ inner in
inner.addTask(executorPreference: globalConcurrentExecutor){
// disabled the preference
dispatchPrecondition(condition:.notOnQueue(firstExecutor.queue))
dispatchPrecondition(condition:.notOnQueue(secondExecutor.queue))
return42
}
returnawait inner.next()!
}
}
_ = group.addTaskUnlessCancelled(executorPreference: secondExecutor){
dispatchPrecondition(condition:.notOnQueue(firstExecutor.queue))
dispatchPrecondition(condition:.onQueue(secondExecutor.queue))
returnawaitwithTaskGroup(of:Int.self){ inner in
inner.addTask(executorPreference: globalConcurrentExecutor){
// disabled the preference
dispatchPrecondition(condition:.notOnQueue(firstExecutor.queue))
dispatchPrecondition(condition:.notOnQueue(secondExecutor.queue))
return42
}
returnawait inner.next()!
}
}
group.addTask(executorPreference: secondExecutor){
dispatchPrecondition(condition:.notOnQueue(firstExecutor.queue))
dispatchPrecondition(condition:.onQueue(secondExecutor.queue))
awaitwithDiscardingTaskGroup{ inner in
inner.addTask(executorPreference: globalConcurrentExecutor){
// disabled the preference
dispatchPrecondition(condition:.notOnQueue(firstExecutor.queue))
dispatchPrecondition(condition:.notOnQueue(secondExecutor.queue))
}
_ = inner.addTaskUnlessCancelled(executorPreference: globalConcurrentExecutor){
// disabled the preference
dispatchPrecondition(condition:.notOnQueue(firstExecutor.queue))
dispatchPrecondition(condition:.notOnQueue(secondExecutor.queue))
}
}
return0
}
group.addTask(executorPreference: secondExecutor){
dispatchPrecondition(condition:.notOnQueue(firstExecutor.queue))
dispatchPrecondition(condition:.onQueue(secondExecutor.queue))
try!awaitwithThrowingDiscardingTaskGroup{ inner in
inner.addTask(executorPreference: globalConcurrentExecutor){
// disabled the preference
dispatchPrecondition(condition:.notOnQueue(firstExecutor.queue))
dispatchPrecondition(condition:.notOnQueue(secondExecutor.queue))
}
_ = inner.addTaskUnlessCancelled(executorPreference: globalConcurrentExecutor){
// disabled the preference
dispatchPrecondition(condition:.notOnQueue(firstExecutor.queue))
dispatchPrecondition(condition:.notOnQueue(secondExecutor.queue))
}
}
return0
}
returnawait group.next()!
}
}
func testAsyncLet(_ firstExecutor:MyTaskExecutor,
_ secondExecutor:MyTaskExecutor)async{
awaitwithTaskExecutorPreference(firstExecutor){
asyncletfirst:Int={
dispatchPrecondition(condition:.onQueue(firstExecutor.queue))
dispatchPrecondition(condition:.notOnQueue(secondExecutor.queue))
return12
}()
_ =await first
}
awaitwithTaskExecutorPreference(firstExecutor){
awaitwithTaskExecutorPreference(secondExecutor){
asyncletfirst:Int={
dispatchPrecondition(condition:.notOnQueue(firstExecutor.queue))
dispatchPrecondition(condition:.onQueue(secondExecutor.queue))
return12
}()
_ =await first
}
}
awaitwithTaskExecutorPreference(firstExecutor){
awaitwithTaskExecutorPreference(secondExecutor){
asyncletfirst:Int={
asyncletfirstInside:Int={
dispatchPrecondition(condition:.notOnQueue(firstExecutor.queue))
dispatchPrecondition(condition:.onQueue(secondExecutor.queue))
return12
}()
returnawait firstInside
}()
_ =await first
}
}
}
func testGroupAsyncLet(_ firstExecutor:MyTaskExecutor,
_ secondExecutor:MyTaskExecutor)async{
awaitwithTaskGroup(of:Void.self){ group in
group.addTask(executorPreference: firstExecutor){
dispatchPrecondition(condition:.onQueue(firstExecutor.queue))
dispatchPrecondition(condition:.notOnQueue(secondExecutor.queue))
asyncletfirst:()=expect(firstExecutor)
_ =await first
awaitwithTaskExecutorPreference(secondExecutor){
asyncletsecond:()=expect(secondExecutor)
_ =await second
}
}
}
}
func expect(_ expected:MyTaskExecutor){
dispatchPrecondition(condition:.onQueue(expected.queue))
}
@mainstructMain{
staticfunc main()async{
letfirstExecutor=MyTaskExecutor(queue:DispatchQueue(label:"first"))
letsecondExecutor=MyTaskExecutor(queue:DispatchQueue(label:"second"))
awaittestTaskGroup(firstExecutor, secondExecutor)
awaittestAsyncLet(firstExecutor, secondExecutor)
awaittestGroupAsyncLet(firstExecutor, secondExecutor)
}
}