|
| 1 | +// RUN: %empty-directory(%t) |
| 2 | +// RUN: %target-build-swift %import-libdispatch -Xfrontend -disable-availability-checking -enable-actor-data-race-checks -parse-as-library -I %t %s -o %t/a.out -module-name main |
| 3 | +// RUN: %target-codesign %t/a.out |
| 4 | +// RUN: env SWIFT_UNEXPECTED_EXECUTOR_LOG_LEVEL=1 %target-run %t/a.out 2>&1 | %FileCheck %s |
| 5 | + |
| 6 | +// Make sure that without downgrade the program crashes by default |
| 7 | +// RUN: not --crash %target-run %t/a.out 2>&1 |
| 8 | + |
| 9 | +// REQUIRES: executable_test |
| 10 | +// REQUIRES: concurrency |
| 11 | +// REQUIRES: libdispatch |
| 12 | + |
| 13 | +// rdar://76038845 |
| 14 | +// REQUIRES: concurrency_runtime |
| 15 | +// UNSUPPORTED: back_deployment_runtime |
| 16 | +// UNSUPPORTED: single_threaded_concurrency |
| 17 | + |
| 18 | +// Simulators don't pass `env` through to simulators even with `%env-` |
| 19 | +// REQUIRES: OS=macosx || OS=linux-gnu |
| 20 | + |
| 21 | +import _Concurrency |
| 22 | +import Dispatch |
| 23 | + |
| 24 | +// For sleep |
| 25 | +#if canImport(Darwin) |
| 26 | +import Darwin |
| 27 | +#elseif canImport(Glibc) |
| 28 | +import Glibc |
| 29 | +#endif |
| 30 | + |
| 31 | +@MainActor func onMainActor() { |
| 32 | + print("I'm on the main actor!") |
| 33 | +} |
| 34 | + |
| 35 | +func promiseMainThread(_ fn: @escaping @MainActor () -> Void) -> (() -> Void) { |
| 36 | + typealias Fn = () -> Void |
| 37 | + return unsafeBitCast(fn, to: Fn.self) |
| 38 | +} |
| 39 | + |
| 40 | +func launchTask(_ fn: @escaping () -> Void) { |
| 41 | + if #available(macOS 10.10, iOS 8.0, watchOS 2.0, tvOS 8.0, *) { |
| 42 | + DispatchQueue.global().async { |
| 43 | + fn() |
| 44 | + } |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +func launchFromMainThread() { |
| 49 | + launchTask(promiseMainThread(onMainActor)) |
| 50 | +} |
| 51 | + |
| 52 | +actor MyActor { |
| 53 | + var counter = 0 |
| 54 | + |
| 55 | + func onMyActor() { |
| 56 | + counter = counter + 1 |
| 57 | + } |
| 58 | + |
| 59 | + func getTaskOnMyActor() -> (() -> Void) { |
| 60 | + return { |
| 61 | + self.onMyActor() |
| 62 | + } |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +@main |
| 67 | +struct Runner { |
| 68 | + static func main() async { |
| 69 | + print("Launching a main-actor task") |
| 70 | + // CHECK: warning: data race detected: @MainActor function at main/data_race_detection.swift:28 was not called on the main thread |
| 71 | + launchFromMainThread() |
| 72 | + sleep(1) |
| 73 | + |
| 74 | + let actor = MyActor() |
| 75 | + let actorFn = await actor.getTaskOnMyActor() |
| 76 | + print("Launching an actor-instance task") |
| 77 | + // CHECK: warning: data race detected: actor-isolated function at main/data_race_detection.swift:57 was not called on the same actor |
| 78 | + launchTask(actorFn) |
| 79 | + |
| 80 | + sleep(1) |
| 81 | + } |
| 82 | +} |
0 commit comments