File: actor_recursive_deinit.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 (84 lines) | stat: -rw-r--r-- 2,543 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
// RUN: %target-run-simple-swift(-enable-experimental-feature IsolatedDeinit -target %target-future-triple -parse-stdlib -parse-as-library) | %FileCheck %s

// REQUIRES: executable_test
// REQUIRES: concurrency

// REQUIRES: concurrency_runtime
// UNSUPPORTED: back_deployment_runtime
// REQUIRES: swift_feature_IsolatedDeinit

// Compiler crashes because builtin "ifdef_SWIFT_STDLIB_PRINT_DISABLED"() gets lowered as "i32 0",
// which triggers assertion in LLVM, which expects it to be i1
// XFAIL: freestanding

import Swift
import _Concurrency

#if canImport(Darwin)
import Darwin
typealias ThreadID = pthread_t
func getCurrentThreadID() -> ThreadID { pthread_self() }
func equalThreadIDs(_ a: ThreadID, _ b: ThreadID) -> Bool { pthread_equal(a, b) != 0 }
#elseif canImport(Glibc)
import Glibc
typealias ThreadID = pthread_t
func getCurrentThreadID() -> ThreadID { pthread_self() }
func equalThreadIDs(_ a: ThreadID, _ b: ThreadID) -> Bool { pthread_equal(a, b) != 0 }
#elseif os(Windows)
import WinSDK
typealias ThreadID = UInt32
func getCurrentThreadID() -> ThreadID { GetCurrentThreadId() }
func equalThreadIDs(_ a: ThreadID, _ b: ThreadID) -> Bool { a == b }
#elseif os(WASI)
typealias ThreadID = UInt32
func getCurrentThreadID() -> ThreadID { 0 }
func equalThreadIDs(_ a: ThreadID, _ b: ThreadID) -> Bool { a == b }
#endif

var mainThread: ThreadID?
func isMainThread() -> Bool {
    return equalThreadIDs(getCurrentThreadID(), mainThread!)
}

@_silgen_name("swift_task_isCurrentExecutor")
private func isCurrentExecutor(_ executor: Builtin.Executor) -> Bool

func getExecutor(_ a: any Actor) -> Builtin.Executor {
  let pack: (AnyObject, UnsafeRawPointer?) = (a, UnsafeRawPointer?.none)
  return unsafeBitCast(pack, to: Builtin.Executor.self)
}

func isCurrent(_ a: any Actor) -> Bool {
  return isCurrentExecutor(getExecutor(a))
}

actor Foo {
    let name: String
    let child: Foo?

    init(_ name: String, _ child: Foo?) {
        self.name = name
        self.child = child
    }

    isolated deinit {
      print("DEINIT: \(self.name) isolated:\(isCurrent(self)) mainThread:\(isMainThread())")
    }
}

// CHECK: DEINIT: a isolated:true mainThread:true
// CHECK: DEINIT: b isolated:true mainThread:true
// CHECK: DEINIT: c isolated:true mainThread:true
// CHECK: DEINIT: d isolated:true mainThread:true
// CHECK: DONE

@main
struct Main {
    static func main() async {
        mainThread = getCurrentThreadID()
        do {
            _ = Foo("a", Foo("b", Foo("c", Foo("d", nil))))
        }
        print("DONE")
    }
}