File tree Expand file tree Collapse file tree 1 file changed +76
-0
lines changed Expand file tree Collapse file tree 1 file changed +76
-0
lines changed Original file line number Diff line number Diff line change
1
+ // RUN: %target-run-simple-swift(-parse-as-library -Xfrontend -enable-experimental-concurrency %import-libdispatch) | %FileCheck %s
2
+
3
+ // REQUIRES: executable_test
4
+ // REQUIRES: concurrency
5
+ // REQUIRES: libdispatch
6
+
7
+ // for sleep
8
+ #if canImport(Darwin)
9
+ import Darwin
10
+ #elseif canImport(Glibc)
11
+ import Glibc
12
+ #endif
13
+
14
+ class Runner {
15
+ func run( ) async {
16
+ while !Task. isCancelled {
17
+ sleep ( 1 )
18
+ }
19
+ }
20
+ }
21
+
22
+ actor Container {
23
+ var generation = 0
24
+ var runners = [ Int : Task . Handle < Void , Never > ] ( )
25
+
26
+ func build( _ n: Int ) {
27
+ for _ in 0 ..< n {
28
+ let id = generation
29
+ generation += 1
30
+ let t = Task . runDetached { [ weak self] in
31
+ let r = Runner ( )
32
+ await r. run ( )
33
+ await self ? . remove ( id)
34
+ }
35
+ runners [ id] = t
36
+ }
37
+ }
38
+
39
+ func cancelAll( ) {
40
+ var count = 0
41
+ for (_, v) in runners {
42
+ v. cancel ( )
43
+ count += 1
44
+ }
45
+ print ( " Cancelled \( count) runners. " )
46
+ }
47
+
48
+ deinit {
49
+ print ( " deinit Container with \( runners. count) runners " )
50
+ }
51
+
52
+ func remove( _ id: Int ) {
53
+ runners. removeValue ( forKey: id)
54
+ }
55
+ }
56
+
57
+ // CHECK: starting
58
+ // CHECK: Cancelled 5 runners.
59
+
60
+ // FIXME: this doesn't work until we have https://github.com/apple/swift/pull/36298
61
+ // COM: deinit Container with {{[0-9]+}} runners
62
+
63
+ @main struct RunIt {
64
+ static func startTest( ) async {
65
+ let c = Container ( )
66
+ await c. build ( 5 )
67
+ sleep ( 5 )
68
+ await c. cancelAll ( )
69
+ }
70
+
71
+ static func main( ) async {
72
+ print ( " starting " )
73
+ await RunIt . startTest ( )
74
+ sleep ( 5 )
75
+ }
76
+ }
You can’t perform that action at this time.
0 commit comments