File: execute_child_on_await.swift

package info (click to toggle)
swiftlang 6.1.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,791,532 kB
  • sloc: cpp: 9,901,743; ansic: 2,201,431; asm: 1,091,827; python: 308,252; objc: 82,166; f90: 80,126; lisp: 38,358; pascal: 25,559; sh: 20,429; ml: 5,058; perl: 4,745; makefile: 4,484; awk: 3,535; javascript: 3,018; xml: 918; fortran: 664; cs: 573; ruby: 396
file content (109 lines) | stat: -rw-r--r-- 2,583 bytes parent folder | download
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
// RUN: %target-run-simple-swift(-parse-as-library -target %target-swift-5.1-abi-triple -Xfrontend -concurrency-model=task-to-thread -g -Xlinker -object_path_lto -Xlinker /tmp/abc.o)

// REQUIRES: concurrency
// REQUIRES: executable_test
// REQUIRES: freestanding
// REQUIRES: concurrency_runtime

@_spi(_TaskToThreadModel) import _Concurrency
import StdlibUnittest
import Darwin

var a = 0;

func foo() async -> Int {
  a += 1
  print("a is now \(a)")
  return a
}

func bar() async -> Int {
  a += 2
  print("a is now \(a)")
  return a
}

func asyncFib(_ n: Int, _ expected_thread: pthread_t) async -> Int {
  expectEqual(pthread_self(), expected_thread);

  if n == 0 || n == 1 {
    return n
  }

  async let first = asyncFib(n-2, expected_thread)
  async let second = asyncFib(n-1, expected_thread)

  return (await second) + (await first)
}

func fib(_ n: Int) -> Int {
  var first = 0
  var second = 1
  for _ in 0..<n {
    let temp = first
    first = second
    second = temp + first
  }
  return first
}

@main struct Main {
  static func main() {

    let tests = TestSuite("Execute child task on await")
    tests.test("Basic async let only execute on await") {
      Task.runInline {
         async let a = foo()
         async let b = bar()

         expectEqual(await b, 2)
         expectEqual(await a, 3)
         // Re-querying a completed task should return original result and not
         // reexecute it
         expectEqual(await b, 2)
      }
    }

    tests.test("Nested async lets that only execute on await") {
      Task.runInline {
        let result = await asyncFib(10, pthread_self())
        expectEqual(result, fib(10))
      }
    }

    tests.test("Task group execute child task on await simple") {
      a = 0
      Task.runInline {
        await withTaskGroup(of: Int.self, body : { group in
          let currentThread = pthread_self()
          group.addTask {
            expectEqual(pthread_self(), currentThread);
            return await foo()
          }

          group.addTask {
            expectEqual(pthread_self(), currentThread);
            return await bar()
          }
        })
      }
    }

    tests.test("Task groups with nested structured concurrency") {
      Task.runInline {
        await withTaskGroup(of: Int.self, body : { group in
          let currentThread = pthread_self()
          group.addTask {
            return await asyncFib(10, currentThread);
          }

          group.addTask {
            return await asyncFib(5, currentThread);
          }
        })
      }
    }

    runAllTests()
  }
}