Skip to content

[Concurrency] Harden async_task_locals_copy_to_sync #39621

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 7, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions test/Concurrency/Runtime/async_task_locals_copy_to_sync.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,13 @@ import Darwin
import Glibc
#endif

@available(SwiftStdlib 5.5, *)
enum TL {
@TaskLocal
static var number: Int = 0
@TaskLocal
static var other: Int = 0
}

@available(SwiftStdlib 5.5, *)
@discardableResult
func printTaskLocal<V>(
_ key: TaskLocal<V>,
Expand All @@ -45,9 +43,10 @@ func printTaskLocal<V>(

// ==== ------------------------------------------------------------------------

@available(SwiftStdlib 5.5, *)
func copyTo_sync_noWait() {
print(#function)
let sem = DispatchSemaphore(value: 0)

TL.$number.withValue(1111) {
TL.$number.withValue(2222) {
TL.$other.withValue(9999) {
Expand All @@ -57,26 +56,29 @@ func copyTo_sync_noWait() {
TL.$number.withValue(3333) {
printTaskLocal(TL.$number) // CHECK: TaskLocal<Int>(defaultValue: 0) (3333)
printTaskLocal(TL.$other) // CHECK: TaskLocal<Int>(defaultValue: 0) (9999)
sem.signal()
}
}
}
}
}

sleep(1)
sem.wait()
}

@available(SwiftStdlib 5.5, *)
func copyTo_sync_noValues() {
print(#function)
let sem = DispatchSemaphore(value: 0)

Task {
printTaskLocal(TL.$number) // CHECK: TaskLocal<Int>(defaultValue: 0) (0)
sem.signal()
}

sleep(1)
sem.wait()
}

/// Similar to tests in `async_task_locals_copy_to_async_ but without any task involved at the top level.
@available(SwiftStdlib 5.5, *)
@main struct Main {
static func main() {
copyTo_sync_noWait()
Expand Down