-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Introduce ASRecursiveUnfairLock and tests #858
Conversation
if (gMutex_unfair) { | ||
// nop | ||
} else { | ||
AS_POSIX_ASSERT_NOERR(pthread_mutex_destroy (&_m)); |
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.
I renamed this assert macro just because it was so dang long.
// non-copyable. | ||
ReadWriteLockWriteLocker(const ReadWriteLockWriteLocker&) = delete; | ||
ReadWriteLockWriteLocker &operator=(const ReadWriteLockWriteLocker&) = delete; | ||
}; |
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.
Removed unused C++ classes from this header.
This is a seminal contribution. Texture (and AsyncDisplayKit) developers have always dreamed of a faster locking primitive than the unnecessarily-fair pthread mutex, but it has felt just out of reach with so much else to do. Thank you @Adlai-Holler for jumping on this opportunity! Some background reading for folks unfamiliar with os_unfair_lock: |
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.
Looks good to me. I don't see any issues or obvious optimization opportunities.
@@ -10,6 +10,8 @@ | |||
// http://www.apache.org/licenses/LICENSE-2.0 | |||
// | |||
|
|||
/// Note this has to be public because it's imported by public header ASThread.h =/ |
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.
But only temporarily for the lifetime of the exp_unfair_lock
experiment?
// Just a cache for pthread_self so that we never call it twice. | ||
pthread_t s = NULL; | ||
|
||
// Try to lock without blocking. If we fail, check what thread owns it. |
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.
grammar nit: s/what/which/
Source/Details/ASThread.h
Outdated
if (!recursive) { | ||
AS_POSIX_ASSERT_NOERR(pthread_mutex_init (&_m, NULL)); | ||
} else { | ||
// Fall back to recursive mutex. |
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.
Is the indentation here off one level?
Generated by 🚫 Danger |
Thanks Jon for reviewing. Merging! |
We should be on the lookout for deadlocks due to priority inversion when this lands. Of course if all clients and Texture itself are built correctly, no priority inversion cases will come up 🤞. I'm definitely excited for the perf gains, reducing locking contention will be huge! |
We good, unfair locks protect against priority inversion! All that they lack is a guarantee that you will get the lock first if you tried to lock first: From https://opensource.apple.com/source/libplatform/libplatform-125/include/os/lock_private.h.auto.html
|
Amazing :) |
* Introduce ASRecursiveUnfairLock and tests * Document it and put underscores to scare people away * Increment changelog * Integrate it with ASDN::Mutex behind experiment * Rename the experiment * Love these license headers oh so much * Move internal header because we have to * Address Jon's feedback
- Followup to TextureGroup#1742 - At Pinterest this shipped with D516974 in late 02/2020 - As discussed in TextureGroup#858 this is iOS10 or later, so the runtime `gMutex_unfair` check is still necessary for Texture.
- Followup to TextureGroup#1742 - At Pinterest this shipped with D516974 in late 02/2020 - As discussed in TextureGroup#858 this is iOS10 or later, so the runtime `gMutex_unfair` check is still necessary for Texture.
exp_unfair_lock
that makes allASDN::Mutex
instances use eitheros_unfair_lock
orASRecursiveUnfairLock
under the hood, if we're on iOS/tvOS >= 10.Mutex
isn't totally appropriate now but it's fine. If this works well we can move it.Here's the perf on my 2016 MBP in release mode. Take synthetic tests with a grain of salt. "Uncontested" (less contested) = 2 threads, contested = 16 threads. See
ASRecursiveUnfairLockTests.m
.Takeaways:
Data: