File: execute_child_on_await.swift

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (109 lines) | stat: -rw-r--r-- 2,588 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 -Xfrontend -disable-availability-checking -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()
  }
}