mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
1) Enable tests that use `import Dispatch` on Linux. Add substitution `%import-libdispatch` that needs to be used for all cross-platform tests (i.e., tests that are intended to be run on other platforms than Darwin) that do `import Dispatch` or enable thread sanitizer. 2) Make sure as many existing Dispatch and TSan tests as possible run on Linux. Mark tests that would require substantial work with `UNSUPPORTED: OS=linux-gnu`. 3) Add integration-style Swift test that shows that TSan finds a simple race when using `Dispatch.async` incorrectly. A more complete test suite for TSan's libdispatch support lives on the LLVM/compiler-rt side. rdar://problem/49177535
105 lines
2.3 KiB
Swift
105 lines
2.3 KiB
Swift
// RUN: %target-build-swift %s %import-libdispatch -o %t_binary
|
|
// RUN: %target-run %t_binary
|
|
// REQUIRES: executable_test
|
|
// REQUIRES: stress_test
|
|
// REQUIRES: libdispatch
|
|
|
|
import StdlibUnittest
|
|
import Dispatch
|
|
|
|
let iterations = 200_000
|
|
|
|
class Thing {}
|
|
|
|
class WBox<T: AnyObject> {
|
|
weak var wref: T?
|
|
init(_ ref: T) { self.wref = ref }
|
|
init() { self.wref = nil }
|
|
}
|
|
|
|
let WeakReferenceRaceTests = TestSuite("WeakReferenceRaceTests")
|
|
|
|
func forwardOptional<T>(_ t: T!) -> T {
|
|
return t!
|
|
}
|
|
|
|
WeakReferenceRaceTests.test("class instance property [SR-192] (copy)") {
|
|
let q = DispatchQueue(label: "", attributes: .concurrent)
|
|
|
|
// Capture a weak reference via its container object
|
|
// "https://bugs.swift.org/browse/SR-192"
|
|
for i in 1...iterations {
|
|
let box = WBox(Thing())
|
|
let closure = {
|
|
let nbox = WBox<Thing>()
|
|
nbox.wref = box.wref
|
|
_blackHole(nbox)
|
|
}
|
|
|
|
q.async(execute: closure)
|
|
q.async(execute: closure)
|
|
}
|
|
|
|
q.async(flags: .barrier) {}
|
|
}
|
|
|
|
WeakReferenceRaceTests.test("class instance property [SR-192] (load)") {
|
|
let q = DispatchQueue(label: "", attributes: .concurrent)
|
|
|
|
// Capture a weak reference via its container object
|
|
// "https://bugs.swift.org/browse/SR-192"
|
|
for i in 1...iterations {
|
|
let box = WBox(Thing())
|
|
let closure = {
|
|
if let ref = box.wref {
|
|
_blackHole(ref)
|
|
}
|
|
}
|
|
|
|
q.async(execute: closure)
|
|
q.async(execute: closure)
|
|
}
|
|
|
|
q.async(flags: .barrier) {}
|
|
}
|
|
|
|
WeakReferenceRaceTests.test("direct capture (copy)") {
|
|
let q = DispatchQueue(label: "", attributes: .concurrent)
|
|
|
|
// Capture a weak reference directly in multiple closures
|
|
for i in 1...iterations {
|
|
weak var wref = Thing()
|
|
let closure = {
|
|
let nbox = WBox<Thing>()
|
|
nbox.wref = wref
|
|
_blackHole(nbox)
|
|
}
|
|
|
|
q.async(execute: closure)
|
|
q.async(execute: closure)
|
|
}
|
|
|
|
q.async(flags: .barrier) {}
|
|
}
|
|
|
|
WeakReferenceRaceTests.test("direct capture (load)") {
|
|
let q = DispatchQueue(label: "", attributes: .concurrent)
|
|
|
|
// Capture a weak reference directly in multiple closures
|
|
for i in 1...iterations {
|
|
weak var wref = Thing()
|
|
let closure = {
|
|
if let ref = wref {
|
|
_blackHole(ref)
|
|
}
|
|
}
|
|
|
|
q.async(execute: closure)
|
|
q.async(execute: closure)
|
|
}
|
|
|
|
q.async(flags: .barrier) {}
|
|
}
|
|
|
|
runAllTests()
|