-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathcancellation_handler_only_once.swift
62 lines (52 loc) · 1.6 KB
/
cancellation_handler_only_once.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
// RUN: %target-run-simple-swift( -Xfrontend -disable-availability-checking -target %target-swift-5.1-abi-triple %import-libdispatch) | %FileCheck %s
// REQUIRES: concurrency
// REQUIRES: executable_test
// rdar://76038845
// REQUIRES: concurrency_runtime
// UNSUPPORTED: back_deployment_runtime
// UNSUPPORTED: freestanding
import Synchronization
struct State {
var cancelled = 0
var continuation: CheckedContinuation<Void, Never>?
}
func testFunc(_ iteration: Int) async -> Task<Void, Never> {
let state = Mutex(State())
let task = Task {
await withTaskCancellationHandler {
await withCheckedContinuation { continuation in
let cancelled = state.withLock {
if $0.cancelled > 0 {
return true
} else {
$0.continuation = continuation
return false
}
}
if cancelled {
continuation.resume()
}
}
} onCancel: {
let continuation = state.withLock {
$0.cancelled += 1
return $0.continuation.take()
}
continuation?.resume()
}
}
// This task cancel is racing with installing the cancellation handler,
// and we may either hit the cancellation handler:
// - after this cancel was issued, and therefore the handler runs immediately
task.cancel()
_ = await task.value
let cancelled = state.withLock { $0.cancelled }
precondition(cancelled == 1, "cancelled more than once, iteration: \(iteration)")
return task
}
var ts: [Task<Void, Never>] = []
for iteration in 0..<1_000 {
let t = await testFunc(iteration)
ts.append(t)
}
print("done") // CHECK: done