Skip to content

[Runtime] Use os_unfair_lock for Mutex and StaticMutex on Darwin. #34598

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 2 commits into from
Nov 11, 2020

Conversation

mikeash
Copy link
Contributor

@mikeash mikeash commented Nov 5, 2020

os_unfair_lock is much smaller than pthread_mutex_t (4 bytes versus 64) and a bit faster.

However, it doesn't support condition variables. Most of our uses of Mutex don't use condition variables, but a few do. Introduce ConditionMutex and StaticConditionMutex, which allow condition variables and continue to use pthread_mutex_t.

On all other platforms, we use the same backing mutex type for both Mutex and ConditionMutex.

rdar://problem/45412121

@mikeash mikeash requested review from compnerd and rjmccall November 5, 2020 20:04
@mikeash
Copy link
Contributor Author

mikeash commented Nov 5, 2020

@swift-ci please test

@swift-ci
Copy link
Contributor

swift-ci commented Nov 5, 2020

Build failed
Swift Test Linux Platform
Git Sha - 3385fed67cf7fba9d7f09fff716d48fc3a4b3cf2

os_unfair_lock is much smaller than pthread_mutex_t (4 bytes versus 64) and a bit faster.

However, it doesn't support condition variables. Most of our uses of Mutex don't use condition variables, but a few do. Introduce ConditionMutex and StaticConditionMutex, which allow condition variables and continue to use pthread_mutex_t.

On all other platforms, we continue to use the same backing mutex type for both Mutex and ConditionMutex.

rdar://problem/45412121
@mikeash mikeash force-pushed the os-unfair-lock-mutex branch from 3385fed to dd6c235 Compare November 6, 2020 18:05
@mikeash
Copy link
Contributor Author

mikeash commented Nov 6, 2020

@swift-ci please test

@mikeash
Copy link
Contributor Author

mikeash commented Nov 9, 2020

@swift-ci please test windows platform

Copy link
Member

@compnerd compnerd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like the name, but I also don't have a better suggestion.

friend class Mutex;
friend class StaticMutex;
friend class ConditionMutex;
friend class StaticConditionMutex;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if there is a better name for these.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update: discussed offline with @mikeash and we ended up coming up with ConditionVariable::Mutex for the "special" mutex here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made that change and nested a few other things that made sense. How's it look now?

@mikeash
Copy link
Contributor Author

mikeash commented Nov 10, 2020

@swift-ci please test

Copy link
Member

@compnerd compnerd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

Mutex(const Mutex &) = delete;
Mutex &operator=(const Mutex &) = delete;
Mutex(Mutex &&) = delete;
Mutex &operator=(Mutex &&) = delete;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These don't have to be private. The = delete will ensure that they cannot be called irrespective of access level.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This pattern (having them before the public: block with = delete) is repeated throughout this file. Is there any particular reason to change this, or should we just leave it be?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Naw, leaving it as is for this change is fine. It was more for knowledge sharing purposes. The original reason for these being private is that before C++11, the methods would be looked up by access control and rely on the missing definition to prevent usage. With the newer functionality, they can be public as the compiler now enforces the prevention no matter what the access level.

@mikeash mikeash merged commit 768a085 into swiftlang:main Nov 11, 2020
@rjmccall
Copy link
Contributor

We should probably just come up with more targeted abstractions for blocking a thread until another thread is done with work. Condition variables are a problematic design.

@mikeash
Copy link
Contributor Author

mikeash commented Nov 13, 2020

Yeah, we're vulnerable to priority inversions where we use them now, so that's not great.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants