-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[SR-946] Unify mutexes in the Swift runtime #43558
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
Comments
LLVM's mutex: http://llvm.org/docs/doxygen/html/Mutex_8h_source.html It seems like it is pthreads based and checks the error codes: bool
MutexImpl::acquire()
{
pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_);
assert(mutex != nullptr);
int errorcode = pthread_mutex_lock(mutex);
return errorcode == 0;
} I have not checked all of the entry points though. On the other hand, you are just exchanging a bool for an int. |
Comment by Shawn Erickson (JIRA) poking around at this, not yet sure I have time to attempt but looking more likely |
Comment by Shawn Erickson (JIRA) I am thinking about building a standalone swift::mutex supporting the requirements outlined. Would this live under swift/lib/basic? |
Comment by Shawn Erickson (JIRA) @gparker42 You want it to halt on error correct? I think that means I should use swift::fatalError and not swift::swift_reportError? ...or should I use reportError and then abort? I see one complication in that internally reportOnCrash (Errors.cpp) uses a pthread mutex that we likely want to cut over to the new swift::Mutex. In theory the mutex used in the crash reporting pathway could report on itself. |
Comment by Shawn Erickson (JIRA) Also should I be looking at the totality of things under the swift tree or just subsets of the swift tree for usage of alternate mutexes? I do see use of pthread read-write locks as well? Should I deal with those? |
This is for the Swift runtime only, not the compiler. It should live in, and be used in, swift/include/swift/Runtime and swift/stdlib/public/runtime. Yes, I meant swift::fatalError not swift::swift_reportError. Good catch in the error reporting. That code can continue to use a pthread mutex, with a new comment saying why it's not a swift::Mutex. (Alternative: add API to swift::Mutex that returns errors instead of crashing, and use that in the error reporting. Then swift::Mutex could be used as an OS abstraction if Swift is ever ported to a platform that doesn't have pthreads.) Other constructs like pthread rwlocks can be updated if they are prone to unchecked errors. That can be left to a separate commit. |
Comment by Shawn Erickson (JIRA) Thanks! That clears things up for me. |
Comment by Shawn Erickson (JIRA) Related pull requests: |
This is long since done, I think. Shawn, do you remember anything else to do here? |
Additional Detail from JIRA
md5: af9f82a35d32e960d03bb405bb35fd8d
Issue Description:
The Swift runtime uses two different mutexes internally. Neither one is good. We should write a new swift::mutex or adopt some LLVM mutex that solves these problems.
Some code uses C++ std::mutex. This mutex throws exceptions when errors occur. Nobody can reasonably catch and respond to these errors. Backtraces are lost when these exceptions are caught and re-thrown before the process halts (such as rdar://25058486, probably).
Some code uses pthread_mutex_t. This mutex returns non-zero when errors occur. Our code does not check these errors.
The new mutex implementation should:
use pthread_mutex_t or some LLVM mutex if a suitable one is available
implement C++ STL's BasicLockable and Lockable
halt using swift::reportError when a failure occurs
not throw exceptions
The text was updated successfully, but these errors were encountered: