-
Notifications
You must be signed in to change notification settings - Fork 441
SyntaxArena: thread safety part 2 #857
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
Conversation
@swift-ci test |
CC @jpsim |
Swift-CI had a hiccup this morning @swift-ci test |
@swift-ci test |
@@ -37,7 +37,18 @@ struct PlatformMutex { | |||
let storage = allocator.allocate(Primitive.self, count: 1).baseAddress! | |||
storage.initialize(to: Primitive()) | |||
#if canImport(Darwin) || canImport(Glibc) | |||
pthread_mutex_init(storage, nil) | |||
#if canImport(Darwin) | |||
// HACK: On Darwin, the value of PTHREAD_MUTEX_INITIALIZER installs |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wait, what?!
I'm still seeing a race with the latest commit (2556643):
And with TSan enabled:
|
Here's the fork I'm using btw which has Rintaro's original changes applied on top of If I may be so bold as to make a suggestion: if you're going to bump the macOS deployment target soon, and the end goal is to move macOS to use os_unfair_lock, just live with the races in older macOS versions in the interim. That's how |
Use mutex to guard concurrent mutation to the arena. swiftlang#785 rdar://99812330
Passing `os_unfair_lock` with inout expression is considered "mutation" by thread sanitizer. Instead, keep the lock object with `let UnsafeMutablePointer` form, and pass it.
609d26b
to
5b7bdc3
Compare
@jpsim Could you try your local tests one last time with this PR? |
@swift-ci test |
Use pthread_mutex_t on Darwin platforms
Did you know that pthread_mutex_init can _fail_? On macOS, the value of PTHREAD_MUTEX_INITIALIZER is not just zeroes - there's optional signature bits in there. POSIX says that you're allowed to check for those signature bits in the API and issue EINVAL if they don't match. By default, pthread_mutex_t() in swift will zero-fill through those signature bits, which results in an invalid mutex variable - even WRT pthread_mutex_init. Once that fails, all subsequent calls to lock and unlock will fail too and leave all of your critical sections completely unguarded. Thank you POSIX On Linuxes this is (often) not the case, and they tend to just use zeroes here, don't check the signature, or both. This allows compilers to allocate lock variables in .bss, which is kinda neat. So, on macOS, we need to install those signature bits, BUT Swift cannot import PTHREAD_MUTEX_INITIALIZER since it's a non-trivial macro that uses brace initialization. Really what we care about is just the signature bits, so we'll install those by hand.
5c1aabe
to
3dd7635
Compare
@swift-ci test |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Races are gone in my tests, TSan is happy too. 👍
Restore Rintaro's original patch with a few adjustments: