Skip to content

Commit 2c27eb3

Browse files
authored
Merge pull request #38113 from apple/tsan-actor_counters.swift-cherry-pick
🍒[5.5][Concurrency] Add tsan_release edge on task creation
2 parents 842e42b + 16da639 commit 2c27eb3

File tree

5 files changed

+17
-9
lines changed

5 files changed

+17
-9
lines changed

stdlib/public/Concurrency/GlobalExecutor.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,8 @@ static void swift_task_enqueueGlobalImpl(Job *job) {
335335
}
336336

337337
void swift::swift_task_enqueueGlobal(Job *job) {
338+
_swift_tsan_release(job);
339+
338340
if (swift_task_enqueueGlobal_hook)
339341
swift_task_enqueueGlobal_hook(job, swift_task_enqueueGlobalImpl);
340342
else

stdlib/public/Concurrency/Task.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -950,7 +950,7 @@ static void resumeTaskAfterContinuation(AsyncTask *task,
950950

951951
// Make sure TSan knows that the resume call happens-before the task
952952
// restarting.
953-
_swift_tsan_release(task);
953+
_swift_tsan_release(static_cast<Job *>(task));
954954

955955
// The status should be either Pending or Awaited. If it's Awaited,
956956
// which is probably the most likely option, then we should immediately

stdlib/public/Concurrency/ThreadSanitizer.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,18 @@ TSanFunc *tsan_acquire, *tsan_release;
3131
void swift::_swift_tsan_acquire(void *addr) {
3232
if (tsan_acquire) {
3333
tsan_acquire(addr);
34+
#if SWIFT_TASK_PRINTF_DEBUG
35+
fprintf(stderr, "[%lu] tsan_acquire on %p\n", _swift_get_thread_id(), addr);
36+
#endif
3437
}
3538
}
3639

3740
void swift::_swift_tsan_release(void *addr) {
3841
if (tsan_release) {
3942
tsan_release(addr);
43+
#if SWIFT_TASK_PRINTF_DEBUG
44+
fprintf(stderr, "[%lu] tsan_release on %p\n", _swift_get_thread_id(), addr);
45+
#endif
4046
}
4147
}
4248

test/Concurrency/Runtime/actor_counters.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ actor Counter {
2828
value = value + 1
2929
return current
3030
}
31+
32+
deinit {
33+
for i in 0..<value {
34+
assert(scratchBuffer[i] == 1)
35+
}
36+
}
3137
}
3238

3339

test/Sanitizers/tsan/actor_counters.swift

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-run-simple-swift(-Xfrontend -enable-experimental-concurrency %import-libdispatch -parse-as-library -sanitize=thread) | grep -v "^Worker"
1+
// RUN: %target-run-simple-swift(-Xfrontend -enable-experimental-concurrency %import-libdispatch -parse-as-library -sanitize=thread)
22

33
// REQUIRES: executable_test
44
// REQUIRES: concurrency
@@ -8,12 +8,6 @@
88
// UNSUPPORTED: linux
99
// UNSUPPORTED: windows
1010

11-
#if canImport(Darwin)
12-
import Darwin
13-
#elseif canImport(Glibc)
14-
import Glibc
15-
#endif
16-
1711
@available(SwiftStdlib 5.5, *)
1812
actor Counter {
1913
private var value = 0
@@ -66,7 +60,7 @@ func runTest(numCounters: Int, numWorkers: Int, numIterations: Int) async {
6660
for i in 0..<numWorkers {
6761
workers.append(
6862
detach { [counters] in
69-
usleep(UInt32.random(in: 0..<100) * 1000)
63+
await Task.sleep(UInt64.random(in: 0..<100) * 1_000_000)
7064
await worker(identity: i, counters: counters, numIterations: numIterations)
7165
}
7266
)

0 commit comments

Comments
 (0)