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

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
This commit is contained in:
Mike Ash
2020-11-05 14:09:40 -05:00
parent 12d114713f
commit dd6c235a2d
8 changed files with 282 additions and 41 deletions

View File

@@ -114,6 +114,29 @@ bool MutexPlatformHelper::try_lock(pthread_mutex_t &mutex) {
/* returnFalseOnEBUSY = */ true);
}
#if HAS_OS_UNFAIR_LOCK
void MutexPlatformHelper::init(os_unfair_lock &lock, bool checked) {
(void)checked; // Unfair locks are always checked.
lock = OS_UNFAIR_LOCK_INIT;
}
void MutexPlatformHelper::destroy(os_unfair_lock &lock) {}
void MutexPlatformHelper::lock(os_unfair_lock &lock) {
os_unfair_lock_lock(&lock);
}
void MutexPlatformHelper::unlock(os_unfair_lock &lock) {
os_unfair_lock_unlock(&lock);
}
bool MutexPlatformHelper::try_lock(os_unfair_lock &lock) {
return os_unfair_lock_trylock(&lock);
}
#endif
void ReadWriteLockPlatformHelper::init(pthread_rwlock_t &rwlock) {
reportError(pthread_rwlock_init(&rwlock, nullptr));
}